AE 18: Pokemon Inference
Today we’ll explore the question “Are taller Pokémon speedier than shorter Pokémon?”
Packages
Data
The data for this application exercise was originally gathered using the Complete Pokémon Dataset from Kaggle.
First, let’s load the data:
pokemon <- read_csv("data/pokemon.csv")To keep things simple, we’ll work with a subset of the data, the generation 4 Pokémon.
Rows: 121
Columns: 17
$ name <chr> "Turtwig", "Grotle", "Torterra", "Chimchar", "Monferno…
$ generation <dbl> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
$ type_1 <chr> "Grass", "Grass", "Grass", "Fire", "Fire", "Fire", "Wa…
$ type_2 <chr> NA, NA, "Ground", NA, "Fighting", "Fighting", NA, NA, …
$ height_m <dbl> 0.4, 1.1, 2.2, 0.5, 0.9, 1.2, 0.4, 0.8, 1.7, 0.3, 0.6,…
$ weight_kg <dbl> 10.2, 97.0, 310.0, 6.2, 22.0, 55.0, 5.2, 23.0, 84.5, 2…
$ total_points <dbl> 318, 405, 525, 309, 405, 534, 314, 405, 530, 245, 340,…
$ hp <dbl> 55, 75, 95, 44, 64, 76, 53, 64, 84, 40, 55, 85, 59, 79…
$ attack <dbl> 68, 89, 109, 58, 78, 104, 51, 66, 86, 55, 75, 120, 45,…
$ defense <dbl> 64, 85, 105, 44, 52, 71, 53, 68, 88, 30, 50, 70, 40, 6…
$ sp_attack <dbl> 45, 55, 75, 58, 78, 104, 61, 81, 111, 30, 40, 50, 35, …
$ sp_defense <dbl> 55, 65, 85, 44, 52, 71, 56, 76, 101, 30, 40, 60, 40, 6…
$ speed <dbl> 31, 36, 56, 61, 81, 108, 40, 50, 60, 60, 80, 100, 31, …
$ catch_rate <dbl> 45, 45, 45, 45, 45, 45, 45, 45, 45, 255, 120, 45, 255,…
$ base_friendship <dbl> 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70…
$ base_experience <dbl> 64, 142, 236, 62, 142, 240, 63, 142, 239, 49, 119, 218…
$ is_legendary <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE…
We are interested in whether a Pokémon’s log height (measured in meters) can predict its speed (how fast it is).
Visualize
- Review! Create a new variable for the Pokémon’s log height.
- Your turn: Plot the data and the line of best fit.
# add code herePoint Estimation
- Your turn: Fit the linear model to these data:
observed_fit <- pokemon |>
specify(speed ~ log_height) |>
fit()
observed_fit# A tibble: 2 × 2
term estimate
<chr> <dbl>
1 intercept 72.4
2 log_height 7.39
This gives the exact same numbers that you get if you use linear_reg() |> fit(), but we need this new syntax because it plays nice with the tools we have for confidence intervals and hypothesis tests.
- Your turn: Typeset the equation for the model fit:
\[ \widehat{\text{speed}} = \_\_\_ + \_\_\_ \times \log(\text{height}) \]
-
Your turn: Interpret the slope and the intercept estimates:
- Add your answer here.
- Add your answer here.
Hypothesis Testing
Let’s consider the hypotheses:
\[ H_0:\beta_1=0\quad vs\quad H_A: \beta_1\neq 0. \]
The null hypothesis corresponds to the claim that log_height and speed are uncorrelated.
- Simulate and plot the null distribution for the slope:
set.seed(24601)
null_dist <- pokemon |>
specify(speed ~ log_height) |>
hypothesize(null = "independence") |>
generate(reps = 500, type = "permute") |>
fit()
# add code to plot here- Add a vertical line to your plot indicating the point estimate of the slope from your original data analysis:
# add code here- Compute the \(p\)-value for this test and interpret it:
# add code hereAdd your interpretation here.
Interval Estimation
-
Demo: Using seed
24601, generate500bootstrap samples, and store them in a new data frame calledbstrap_samples.
set.seed(24601)
bstrap_samples <- pokemon |>
specify(speed ~ log_height) |>
generate(reps = 500, type = "bootstrap")-
Demo: Fit a linear model to each of these bootstrap samples and store the estimates in a new data frame called
bstrap_fits.
bstrap_fits <- bstrap_samples |>
fit()-
Your turn: Use
linear_reg() |> fit(...)to fit a linear model to bootstrap sample number 347, and verify that you get the same estimates as the ones contained inbstrap_fits.
# add code hereThe only point I’m making here is that this new bootstrap code is not performing a fundamentally new task. It’s performing an old task (fitting the linear model), but it’s repeating it A LOT.
-
Demo: Compute 95% confidence intervals for the slope and the intercept using the
get_confidence_intervalcommand.
ci_95 <- get_confidence_interval(
bstrap_fits,
point_estimate = observed_fit,
level = 0.95,
type = "percentile"
)
ci_95# A tibble: 2 × 3
term lower_ci upper_ci
<chr> <dbl> <dbl>
1 intercept 67.4 76.6
2 log_height 0.942 13.3
-
Your turn: Verify that you get the same numbers when you manually calculate the quantiles of the slope estimates using
summarizeandquantile. Pay attention to the grouping.
# add code hereSame point as before. There’s no magic here. get_confidence_interval is just a convenient way of doing something that you already knew how to do.
- BONUS: You can visualize the confidence interval:
visualize(bstrap_fits) +
shade_confidence_interval(ci_95)