Practical 9 worksheet
Open your project for this week in RStudio. Then, open a new Markdown file with HTML output and save it in the r_docs
folder. (Give it a sensible name, like worksheet_09
or similar!)
For each of the tasks in the Analysis section, write your code to complete the task in a new code chunk.
Remember, you can add new code chunks by:
```
{r}
, press ↵ Enter, then ```
again.The theme for today’s task is inspired by a weird Internet thing that happened in the spring of 2014, called Twitch Plays Pokémon. A programmer in Australia hooked up the classic Pokémon Red game to the chat room on video game streaming site Twitch. By typing commands in the chat, viewers could control what the character in the game did - but on the scale of thousands or tens of thousands of participants at once. The game turned into a massive social experiment and even spawned a minor cult before the cumulative 1.16 million viewers beat the game in about two and a half weeks1.
If you aren’t really familiar with Pokémon, all you need to know for today is that it’s a series of video games (later TV show, graphic novels, etc. etc.) that take place in an alternate universe with magical animals called Pokémon. Pokémon battling (sort of like magical dogfighting?) is a core part of this universe, with children setting out at a young age to travel the world, capture many different types of Pokémon, and compete in massive championships, with the winner being crowned a Pokémon Master. It’s a bit ethically murky because at least some Pokémon seem to be sentient, and they can all talk, but only to say their own species name. You know, as I’m writing this description, it just keeps sounding weirder…
Figure 1 A Pokémon called Pikachu. You may have heard of him. Source
For this week’s stats practical, we’re doing a Twitch-Plays-Pokémon-style walkthrough of a dataset of Pokémon characteristics to practice the linear model. You don’t need to know anything about Pokémon to do this practical besides what’s in the box above.
Load the tidyverse
package and read in the pokemon
dataset straight from https://and.netlify.app/datasets/pokemon.csv (we took the data from this fun website).
You should always have a look at what variables you have in your data. Do it now by asking R
to either give you the names of the variables or by looking at a rought summary of the data.
As you can see there are quite a few variables but most of them will not be relevant to us.
First we need to choose a research question to investigate.
As future Pokémon Masters, we want to work out which Pokémon is the strongest. So, we’ll use attack
as our outcome variable, which quantifies a Pokémon’s offensive capabilities.
For the predictor, let’s choose the hp
variable.
HP or hit points is a geek gaming term name for health of your character/creature:
The more HP a thing has, the more damage it can withstand.
In your RMarkdown file, write down your prediction about the relationship between the predictor – hp
– and the outcome, attack
.
Next up, we should have a look at our data.
Create a scatterplot of hp
as the predictor and attack
as the outcome.
Label the axes better and clean up the plot with a theme.
Stop and have a look at your plot. How would you draw the line of best fit? Is this the direction of relationship you expected?
Add a line of best fit to your plot. Is this what you expected?
Optionally, add another line to your plot that represents the null model.
Have a look at geom_hline
, or the code for the plots in the lecture!
Now that we have some idea of what to expect, we can create our linear model!
Use the lm()
function to run your analysis and save the model in a new object called poke_lm
.
Call this poke_lm
object in the Console to view it, then write out the linear model from the output.
How can you interpret the value of b1 for this model? Write down your thoughts in your RMarkdown.
Using your equation, what attack
value would you predict a Pokémon with 86 HP to have?
Now we have the model parameters, but we don’t want to just describe the line - we want to be able to say something about the population, not just our sample. For this, we need some more info!
Use broom::tidy()
to get p-values for your bs. Is your predictor significant?
Remember, that in the so-called scientific notation, the number xe-n
, where x
and n
are numbers means x × 10−n.
For example 2.3e-4
means 2.3 × 10−4 which is the same as 2.3/10000 or 0.00023.
Add the conf.int = T
argument to broom::tidy()
to get confidence intervals. How can you interpret these? Do they “agree” with the p-value?
Use broom::glance()
to get R2 and adjusted R2 for this model.
How much of the variance of the outcome is explained by the model for this sample? What would we expect this to be in the population?
Finally, we can get all this same information - except for CIs - from the summary()
function.
I (Jennifer) like summary()
because you can get a good overview of a lot of information quickly, but it’s very inconvenient for reporting, so it’s good to know how to use the broom
functions as well.
Get a summary of your model. What do the asterisks (stars) mean?
Report the results of your model. Specifically, you should clearly report the coefficient of interest (b1) and the fit of the model (R2), including all the important details (see Tutorial 8).
That’s as far as we’ll go today, well done!
The Linear Model is all of our destinies for the next year or so, so it’s important to get comfortable working with it.