AE 11: Midterm Pokémon Practice

Suggested answers

In this AE, you’ll work through some variety of tasks to prepare for the midterm while learning about the Pokémon universe!

pokemon <- read_csv("data/pokemon.csv")
Rows: 924 Columns: 17
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (3): name, type_1, type_2
dbl (13): generation, height_m, weight_kg, total_points, hp, attack, defense...
lgl  (1): is_legendary

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
pokemon_go <- read_csv("data/pokemon_go.csv")
Rows: 1007 Columns: 24
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (5): pokemon_name, type, rarity, charged_moves, fast_moves
dbl (13): pokemon_id, base_attack, base_defense, base_stamina, candy_require...
lgl  (6): found_egg, found_evolution, found_wild, found_research, found_raid...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Question 1

Take a look at the data with an appropriate function. Report the number of rows and colmns in each one. Note that each row refers to a single Pokémon.

glimpse(pokemon)
Rows: 924
Columns: 17
$ name            <chr> "Bulbasaur", "Ivysaur", "Venusaur", "Mega Venusaur", "…
$ generation      <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
$ type_1          <chr> "Grass", "Grass", "Grass", "Grass", "Fire", "Fire", "F…
$ type_2          <chr> "Poison", "Poison", "Poison", "Poison", NA, NA, "Flyin…
$ height_m        <dbl> 0.7, 1.0, 2.0, 2.4, 0.6, 1.1, 1.7, 1.7, 1.7, 0.5, 1.0,…
$ weight_kg       <dbl> 6.9, 13.0, 100.0, 155.5, 8.5, 19.0, 90.5, 110.5, 100.5…
$ total_points    <dbl> 318, 405, 525, 625, 309, 405, 534, 634, 634, 314, 405,…
$ hp              <dbl> 45, 60, 80, 80, 39, 58, 78, 78, 78, 44, 59, 79, 79, 45…
$ attack          <dbl> 49, 62, 82, 100, 52, 64, 84, 130, 104, 48, 63, 83, 103…
$ defense         <dbl> 49, 63, 83, 123, 43, 58, 78, 111, 78, 65, 80, 100, 120…
$ sp_attack       <dbl> 65, 80, 100, 122, 60, 80, 109, 130, 159, 50, 65, 85, 1…
$ sp_defense      <dbl> 65, 80, 100, 120, 50, 65, 85, 85, 115, 64, 80, 105, 11…
$ speed           <dbl> 45, 60, 80, 80, 65, 80, 100, 100, 100, 43, 58, 78, 78,…
$ catch_rate      <dbl> 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 25…
$ base_friendship <dbl> 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70…
$ base_experience <dbl> 64, 142, 236, 281, 62, 142, 240, 285, 285, 63, 142, 23…
$ is_legendary    <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE…
glimpse(pokemon_go)
Rows: 1,007
Columns: 24
$ pokemon_id                   <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13…
$ pokemon_name                 <chr> "Bulbasaur", "Ivysaur", "Venusaur", "Char…
$ base_attack                  <dbl> 118, 151, 198, 116, 158, 223, 94, 126, 17…
$ base_defense                 <dbl> 111, 143, 189, 93, 126, 173, 121, 155, 20…
$ base_stamina                 <dbl> 128, 155, 190, 118, 151, 186, 127, 153, 1…
$ type                         <chr> "['Grass', 'Poison']", "['Grass', 'Poison…
$ rarity                       <chr> "Standard", "Standard", "Standard", "Stan…
$ charged_moves                <chr> "['Sludge Bomb', 'Seed Bomb', 'Power Whip…
$ fast_moves                   <chr> "['Vine Whip', 'Tackle']", "['Razor Leaf'…
$ candy_required               <dbl> NA, 25, 100, NA, 25, 100, NA, 25, 100, NA…
$ distance                     <dbl> 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1,…
$ max_cp                       <dbl> 1275, 1943, 3112, 1121, 1891, 3305, 1082,…
$ attack_probability           <dbl> 0.1, 0.1, 0.2, 0.1, 0.1, 0.2, 0.1, 0.1, 0…
$ base_capture_rate            <dbl> -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -…
$ base_flee_rate               <dbl> -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -…
$ dodge_probability            <dbl> 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15,…
$ max_pokemon_action_frequency <dbl> 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1…
$ min_pokemon_action_frequency <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0…
$ found_egg                    <lgl> TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, T…
$ found_evolution              <lgl> FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FAL…
$ found_wild                   <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,…
$ found_research               <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,…
$ found_raid                   <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,…
$ found_photobomb              <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,…

Question 2

Using the first data set, create a density plot of all water type’s hp. Note that a Pokémon is considered a water type if either its type_1 or type_2 is water. Describe the distribution with appropriate summary statistics.

water_poke <- pokemon |> 
  filter(type_1 == "Water" | type_2 == "Water")

ggplot(water_poke, aes(x = hp)) +
  geom_density() +
  labs(x = "HP",
       title = "HP of Water Type Pokémon")

water_poke |> 
  summarise(mean = mean(hp),
            median = median(hp),
            sd = sd(hp),
            iqr = IQR(hp))
# A tibble: 1 × 4
   mean median    sd   iqr
  <dbl>  <dbl> <dbl> <dbl>
1  69.7     70  26.3    35

The distribution of water type hp is unimodal and right skewed, with potential outliers on the right tail past 180, with a mean of 69.7 and median or 70 and a standard deviation of 26.26 and iqr of 35.

Question 3

Create a table that displays the number of legendary Pokémon (is_legendary) from each (type_1) in descending order.

leg_tab <- pokemon |> 
  filter(is_legendary == TRUE) |> 
  group_by(type_1) |> 
  summarise(n = n()) |> 
  arrange(desc(n))

Question 4

Using the table above, make a barplot with n on the x-axis, but make sure that the order of the types in the first column is preserved

ggplot(leg_tab, aes(x = n, y = fct_reorder(type_1, n))) +
  geom_col() +
  labs(y = "Type",
       title = "Number of Legendary Pokémon by Type")

Question 5

Pokémon and Pokémon Go are actually very different games. You want to investigate if there is any relationship between the attack variable in Pokémon and the base_attack in Pokémon Go. The variables lie in different datasets however.

In a single pipeline, create a scatterplot that visualizes the relationship above. Only include Pokémon who are present in both games. Describe the distribution

pokemon |> 
  inner_join(
    pokemon_go,
    by = join_by(name == pokemon_name)
  ) |> 
  ggplot(aes(x = attack, y = base_attack)) +
  geom_point() +
  labs(x = "Attack",
       y = "Base Attack",
       title = "Base Attack by Attack of Pokémon in Both Games")

There is a strong, positive, roughly linear relationship between the base attack (present in Pokémon Go) and attack (present in Pokémon) of Pokémon found in both games.