The goal for this tutorial is to:
By the end of this tutorial you will learn how to:
{dplyr}
{ggplot2}
:
If you're interested, source code can be found on GitHub
Now that we've got the hang of some R functions, let's do some real analysis!
linear regression takes the linear equation of:
\[ Y = mX + b \] But we're going to use it more like:
\[ \hat{y_{i}} = \hat{\beta_{0}} + \hat{\beta_{1}}x_{i} + \epsilon_{i}\]
where:
\(\hat{y_{i}} =\) predicted response for an experimental unit \(i\)
\(x_{i} =\) predictor (or independent variable) of experimental unit \(i\)
\(\hat{\beta_{0}} =\) is expected value when \(x_{i} = 0\) (intercept)
\(\hat{\beta_{1}} =\) slope
\(\epsilon =\) some error, because models are never perfect!
\(\beta_{0}\) and \(\beta_{1}\) are unknown. We will estimate these coefficients from known data. To do this, we need to estimate a line of best fit than minimizes error... this is where linear regression comes in!
We have options for loading data.
read.csv()
to read in data from a file.Today we'll use a built in dataset called the palmerpenguins dataset.
# install the package, normally required
# install.packages("palmerpenguins")
# load the package, normally required
#library(palmerpenguins)
# call the data from the package, normally required but is already loaded for you today!
#data(package = 'palmerpenguins')
head(penguins)
Sometimes we need to access specific data from the data set because we aren't interested in the entire thing. R let's us manipulate data a number of different ways but we'll go over some of the most important ones.
data[rows,columns]
data[1:20, c(2,5,6)]
would give us rows 1-20 and columns 2, 5, 6, of "data"data$column_name
data$species
would give us the column names "species" in "data"Play around with your penguins
data set in here with these functions. I'll give you a head start
### Use this space to complete your exercise.
penguins[1:4,c(2:4)]
penguins$island
## your turn! :)
We will focus on two penguin measurements for today:
Ok, let's look at the data to see if a linear model looks reasonable...
plot(penguins$bill_depth_mm, penguins$bill_length_mm,
main="Bill length vs bill depth", xlab = "Bill depth (mm)", ylab = "
Bill lengh (mm)")
Hmmm... not sure yet. But! Remember! There are three types of penguins in the data. Maybe we'll see some patterns later on...
Let's estimate the coefficients we were talking about earlier.
lm()
is the linear model function.formula
and data
are parametersTry using this code. What does summary()
give us? What does abline()
do?
lm1 <- lm(formula = bill_length_mm ~ bill_depth_mm, data = penguins)
summary(lm1)
plot(penguins$bill_depth_mm, penguins$bill_length_mm,
main="Bill length vs bill depth", xlab = "Bill depth (mm)", ylab = "
Bill length (mm)")
abline(lm1, col="purple")
Our summary output gives us a lot of information:
Let's refer back to the linear model equation.
\[ \hat{y_{i}} = \hat{\beta_{0}} + \hat{\beta_{1}}x_{i} + \epsilon_{i}\]
Here, \(\hat{y_{i}}\) is bill length, \(x_{i}\) is bill depth. Now we've estimated:
Note: \(\hat{\beta_{0}}\), the intercept, is the intercept of the linear model when \(x=0\), so we don't see it on this graph.
We can re-write out equation as:
\[ \hat{y_{i}} = 55.0674 - 0.6598x_{i} + \epsilon_{i}\]
OK, so maybe all penguins don't follow the same bill length to depth patterns. How about we test if the type of penguin has an effect on this pattern? We can add this information to our linear model using a different formula=
parameter, but we have to formulate it on our own.
We have some options on possible formulas for the linear regression:
We're going to be more interested in 1. and 3. as 2. is not commonly used.
OK, so we can run all of these functions, but how do we know what the output is? How can we look at coefficients, error estimate, etc?
Here's your chance to show off! Type how you would print out this information we need and then click on Run Code.
lm2 <- lm(formula = bill_length_mm ~ bill_depth_mm + species, data = penguins)
#lm3 <- lm(formula = bill_length_mm ~ bill_depth_mm:species, data = penguins)
## This is not common
lm4 <- lm(formula =bill_length_mm ~ bill_depth_mm*species, data = penguins)
lm2 <- lm(formula = bill_length_mm ~ bill_depth_mm + species, data = penguins)
#lm3 <- lm(formula = bill_length_mm ~ bill_depth_mm:species, data = penguins)
## This is not common
lm4 <- lm(formula = bill_length_mm ~ bill_depth_mm*species, data = penguins)
summary(lm2)
#summary(lm3)
summary(lm4)
How can we interpret these linear models? These get a bit more confusing, but we will go through them individually, staring with lm2.
lm2 will find different slopes for each species in our data, while maintaining the same intercept. This is a linear model without interactions. The confusing part is when we do these multiple regression models, there is always a "reference" variable. In this case, our reference will always be Adelie penguins. This reference was chosen automatically because it starts with 'A'.
We've estimated more coefficients this time, so our equation is getting a bit longer. Now it looks like:
\[ \hat{y_{i}} = 13.2164 + 1.3948x_{i} + 9.939\text{Chinstrap} + 13.4033 \text{Gentoo} + \epsilon_{i}\]
"Chinstrap" and "Gentoo" are in there to make our equation "easier" on us. If the data we are analysing are from a Chinstrap penguin, then Chinstrap will become a "1" and Gentoo a "0" and vice versa. If the data are from an Adelie penguin, both will be 0.
This is what the Adelie equation looks like:
\[ \hat{y_{i}} = 13.2164 + 1.3940x_{i} + 9.939(0) + 13.4033(0) + \epsilon_{i}\] \[ \hat{y_{i}} = 13.2164 + 1.3940x_{i} + \epsilon_{i} \] Chinstrap:
\[ \hat{y_{i}} = 13.2164 + 1.3940x_{i} + 9.939(1) + 13.4033(0) + \epsilon_{i}\] \[ \hat{y_{i}} = 13.2164 + 1.3940x_{i} + 9.939 \epsilon_{i}\] \[ \hat{y_{i}} = 23.1554 + 1.3940x_{i} +\epsilon_{i}\]
Gentoo:
\[ \hat{y_{i}} = 13.2164 + 1.3940x_{i} + 9.939(0) + 13.4033(1) + \epsilon_{i}\] \[ \hat{y_{i}} = 13.2164 + 1.3940x_{i} + 13.4033(1) + \epsilon_{i}\] \[ \hat{y_{i}} = 26.6197 + 1.3940x_{i} + \epsilon_{i}\]
What if we used a different penguin as the reference? Let's try it out
penguins$species <- relevel(as.factor(penguins$species), ref="Chinstrap")
lm2_relevel <- lm(formula = bill_length_mm ~ bill_depth_mm + species, data = penguins)
summary(lm2_relevel)
Notice how the slope doesn't change? So far, we've only told R to look for different slopes for each species.
In this regression, lm4 is giving us a model that includes: - different intercepts per species - different bill length coefficients per species
Again, Adelie is the reference penguin, so everything is compared to Adelie. But this time we have MORE coefficients since we're looking at how the slope is affected.
Full equation
\[ \hat{y_{i}} = 23.0681 + 0.857x_{i} + (-9.6402\text{Chinstrap}) + (-5.8386 \text{Gentoo}) + 1.0651x_{i}\text{Chinstrap} + 1.1637x_{i}\text{Gentoo} + \epsilon_{i}\]
Adelie's equation: \[ \hat{y_{i}} = 23.0681 + 0.857x_{i} -9.6402(0) -5.8386(0) + 1.0651x_{i}(0) + 1.1637x_{i}(0) + \epsilon_{i}\] \[ \hat{y_{i}} = 23.0681 + 0.857x_{i} + \epsilon_{i}\] Chinstrap's equation: \[ \hat{y_{i}} = 23.0681 + 0.857x_{i} -9.6402(1)-5.8386(0) + 1.0651x_{i}(1) + 1.1637x_{i}(0) + \epsilon_{i}\] \[ \hat{y_{i}} = 23.0681 + 0.857x_{i} -9.6402 + 1.0651x_{i} + \epsilon_{i}\] \[ \hat{y_{i}} = 13.4279 + 0.857x_{i} + 1.0651x_{i} + \epsilon_{i}\] \[ \hat{y_{i}} = 13.4279 + 1.9221x_{i} + \epsilon_{i}\]
What would Gentoo's equation look like?
Before we get into the fun part (data visualization!) , how about we try some more simple stats.
Let's say that we wanted to show a professor that two penguin species differ in mean body weight. We hypothesize that the Chinstrap penguins have a different mean body weight than the Gentoo penguins.
We have a pretty big dataset of info on penguins. We don't really need all of it, do we? Right now we're only interested in body weight info of Chinstrap and Gentoo penguins.
We're going to have to subset our data so that we can focus on what we're interested in right now. We are going to be using the function filter()
. To do this, we're also going to have to learn about "logical operators".
You will use logical operators often in R programming. Here are some examples:
==
: equals!=
: does not equal&
: AND|
: OR>
: greater than>
: less than>=
: greater or equal to<=
: less than or equal tohote: We're using functions filter()
and select()
. These functions are from the tidyverse
and use special "syntax". Most tidyverse
functions always consider the first argument as the data, so we don't need explicitly write data = penguins
. Fun!
Today we want to filter for Chinstrap OR (|
) Gentoo penguins. It might seem like we're interested in Chinstrap AND (&
) Gentoo penguins, but in programming logic, that is impossible. Let's try both and see what happens..
## loading ALL tidyverse packages at once because we'll be using many of them, normally required
#library(tidyverse)
## filtering for Chinstrap AND Gentoo penguins from the species column of penguins
penguin_chin_gent <- filter(penguins, species == "Chinstrap" & species == "Gentoo")
head(penguin_chin_gent)
## filtering for Chinstrap OR Gentoo penguins from the species column of penguins
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
# print the first couple rows
head(penguin_chin_gent)
Now, we're only interested in the bodyweight of the penguins. We should make an even smaller dataset that only includes the body weight information of Gentoo and Chinstrap penguins.
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
## what are the column names of our dataset again? Let's check
colnames(penguin_chin_gent)
## OK, so body weight is callled "body_mass_g". Let's select body_mass_g and species so that we
## can calculate the mean body weight of each species.
penguin_chin_gent_bw <- select(penguin_chin_gent, species, body_mass_g)
head(penguin_chin_gent_bw)
tail(penguin_chin_gent_bw)
## what does this function do?
count(penguin_chin_gent_bw, species)
Now that we have our dataset, how about we plot some data???
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
penguin_chin_gent_bw <- select(penguin_chin_gent, species, body_mass_g)
ggplot2
ggplot2
is a great package for visualizing all types of data. I think it's more useful to learn how to use ggplot2
than the "built-in" plotting functions in R that we used briefly in 3. Linear regression...with penguins
. In my opinion, it is more intuitive.... but that's all up for debate.
We're going to learn how to use `ggplot2
to plot our penguin data.
First we need to initialize a ggplot
. This tells ggplot which data we'd like to plot and which features we want to plot on the x and y axes.
# ggplot2 is already loaded with tidyverse
#library(ggplot2)
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
penguin_chin_gent_bw <- select(penguin_chin_gent, species, body_mass_g)
ggplot(data=penguin_chin_gent_bw, aes(x=species, y=body_mass_g))
We haven't told ggplot()
what kind of plot we want. So far, we've just told ggplot()
that we're ready to plot and which data we want it to use.
ggplot()
uses "geom" layers to plot. "Geoms" are ways to represent your data points, Here is a nice reference for ggplot()
plotting, but it can be overwhelming at first.
Some geoms that you might be interested in are:
geom_bar()
: Bar plotgeom_point()
, geom_jitter()
: Scatter plotsgeom_boxplot()
: Boxplotgeom_line()
: Line plotgeom_line()
:Let's try plotting our body weight data as a boxplot.
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
penguin_chin_gent_bw <- select(penguin_chin_gent, species, body_mass_g)
ggplot(data=penguin_chin_gent_bw, aes(x=species, y=body_mass_g)) +
geom_boxplot()
Nice! Looking at the data in this way, do you think these two penguins have a significantly different body mass?
We should test it so that we can give the professor a p-value. Let's use that t-test we discussed before. The t-test function is t.test()
PS. The Student's t-test was invented by a chemist and statistician named William Sealy Gosset at Guinness in the early 1900s. His boss wouldn't let him use his real name when he published the t-test... and so he published under the pseudonym "Student"!
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
penguin_chin_gent_bw <- select(penguin_chin_gent, species, body_mass_g)
## Is body_mass_g influenced by species?
## We can use a similar formula idea as we did for the linear model
ttest_result <- t.test(body_mass_g ~ species, data=penguin_chin_gent_bw)
ttest_result
Our prof likes things to look aesthetically pleasing when we give them the penguin info. Let's make our box plot look nice!
First we add colour=species
into the aes()
section of the ggplot()
initialization. In this case, we want the colours of the boxplots to change with the species colours.
Why do we add colour=
inside aes()
? Everything inside aes()
is influenced by our actual data. Try moving colour=species
outside aes()
. What happens?
Hint: This would look something like:
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
penguin_chin_gent_bw <- select(penguin_chin_gent, species, body_mass_g)
ggplot(data=penguin_chin_gent_bw, aes(x=species, y=body_mass_g), colour=species) +
...
penguin_chin_gent <- filter(penguins, species == "Chinstrap" | species == "Gentoo")
penguin_chin_gent_bw <- select(penguin_chin_gent, species, body_mass_g)
## Let's add some colours to the box plot, and fix the axes labels!
ggplot(data=penguin_chin_gent_bw, aes(x=species, y=body_mass_g, colour=species)) +
geom_boxplot() +
xlab("Species") + # changes the x axis label to anything
ylab("Body mass (g)") # changes the y axis label to anything
Great! Now we know how to do some plots using ggplot()
. How about we learn how to plot our original linear model data?
We originally looked at the relationship of penguin bill length with bill depth. We had plotted the data in a very "simple" way and we didn't really see any patterns. But now we know how to use ggplot to colour plots with info from each species...maybe there's a pattern within each species that we can see?
Let's use a scatter plot to explore our data. Modify this code by updating the ????
to make a scatter plot of penguin bill length by bill depth that has points coloured in by the penguin species.
ggplot(data=penguins,
y=????,
colour=????)
) +
geom_????()
ggplot(data=penguins,
aes(x=bill_length_mm,
y=bill_depth_mm,
colour=species)
) +
geom_point()
Looking at our plot above, there may be some patterns that can be explained by species. We also found this when we completed our linear models. Cool! So, let's add our linear models to our scatter plot. And while we're at it, let's add custom colours to our plots.
Let's go back to our ggplot2
cheatsheet. Under the Two Variables section, check out geom_smooth()
. This geom can calculate a linear model and plot it for us, kind of like the abline()
we used previously. In this case, we can plot it all at once. Let's try it out!
ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = species)
) +
geom_point() +
geom_smooth(method = "lm") # method = "lm" tells ggplot we want to use the linear model function lm()
Looks good so far, except for a few things.
geom_smooth
adds an error estimate from the linear model by default (those grey ribbons around the lines). It's not really helpful for us here, so we should remove that ribbon for now.Let's attack these points one at a time.
To remove these unnecessary ribbons, we need to tell geom_smooth()
that we don't want them.
(no_ribbon <- ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = species)
) +
geom_point() +
geom_smooth(method = "lm", se=FALSE)) #se = FALSE tells ggplot
One thing I love about making plots is making them look fun and pretty. Problem is...I'm not very artistically inclined. I use a lot of different websites for plot color theme inspiration. One of my favourites is coolors.co. It randomly generates complementary colour palettes and I use this all the time. Coolors gives us colours in HEX codes that we can use, or we can type in colours manually (here's a link to all the colours R understands)
Let's make our plot pretty both ways: using HEX codes and the names of colours. We're going to use scale_colour_manual()
to tell ggplot that we want to manually change the colours.
(new_colours <- ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = species)
) +
geom_point() +
geom_smooth(method = "lm", se=FALSE) + #se = FALSE tells ggplot
scale_colour_manual(values=c("royalblue", "orange", "limegreen")))
I like the blue and orange...but I'm not so sure that the green goes. I'm going to use coolors.co to help me out!
I like this palette that coolors suggested. That combination of letters and numbers is the colour's HEX code! We have so many more colour options using these codes. We can input these codes instead of the colour name using the exact same ggplot function. Feel free to edit these HEX codes as you'd like!
(new_colours <- ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = species)
) +
geom_point() +
geom_smooth(method = "lm", se=FALSE) + #se = FALSE tells ggplot
scale_colour_manual(values=c("#1E3888", "#FFAD69", "#47A8BD"))) # hex codes start with a "#"
It's useful to not only change the colours of the points, but also have shapes reflect the data as well. This makes our figures more accessible! We have to add an extra option to aes()
. In this case, we add shape = species
.
(new_colours <- ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = species,
shape = sex,
group = species)
) +
geom_point() +
geom_smooth(method = "lm", se=FALSE)) + #se = FALSE tells ggplot
scale_colour_manual(values=c("#1E3888", "#FFAD69", "#47A8BD")) # hex codes start with a "#"
However, what if we're interested in adding more information to our plot? What about if we wanted to look at how the penguin's sex is reflected in these bill measurements? We would tell ggplot to change either the colour or the shape from species
to sex
. Try it out in the above plot. What happens?
Yikes.I bet something didn't look so right when we added that extra grouping. That's because our original ggplot(data = penguins,aes(x = bill_length_mm, y = bill_depth_mm,colour = species,shape = sex)
call told ggplot to use of that information for the rest of the plot. Our linear model is now trying to add information for both sex and species, making it a little fuzzy. We can force ggplot to group any statistical functions by species while allowing us to colour/shape the rest of the data how we want. To do this, we add group = species
to our aes()
function. (This may be useful for your mini-homework)
(new_colours <- ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = sex,
shape = species,
group = species)
) +
geom_point() +
geom_smooth(method = "lm", se=FALSE)) + #se = FALSE tells ggplot
scale_colour_manual(values=c("#1E3888", "#FFAD69", "#47A8BD")) # hex codes start with a "#"
Now we can see how ignoring those warnings is coming back to haunt us... What do you think is happening here? Let's discuss
We're already learned how to do this hint xlab()
might ring a bell.... I'll leave it up to you to rename our axes to appropriate names.
(axes <- ggplot(data = penguins,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = species,
shape = sex,
group = species)
) +
geom_point() +
geom_smooth(method = "lm", se=FALSE) + #se = FALSE tells ggplot
scale_colour_manual(values=c("#1E3888", "#FFAD69", "#47A8BD"))) # hex codes start with a "#"
Back to those warnings... ggplot is telling us that our dataset is incomplete and contains NA
. Researchers use NA
when recording data if they were unable to take the measurement, or the measurement is missing for some reason. Keeping these NA
is not always helpful to us. Thankfully we have a function that can help us remove any rows (observations) with NA
. This function is drop_na()
.
penguins_removeNA <- drop_na(penguins)
(axes <- ggplot(data = penguins_removeNA,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = species,
shape = sex,
group = species)
) +
geom_point() +
geom_smooth(method = "lm", se=FALSE) + #se = FALSE tells ggplot
scale_colour_manual(values=c("#1E3888", "#FFAD69", "#47A8BD"))) # hex codes start with a "#"
Yay! We've removed our warnings!
Here's an example of a final plot that went a bit further than what we did in our steps above.
What has been added to this plot? If you change some options around, what happens? You might want to use some of these options for your "homework".
(final <- ggplot(data = penguins_removeNA,
aes(x = bill_length_mm,
y = bill_depth_mm,
colour = species,
shape = species,
group = species)) +
geom_point(size = 3, alpha = 0.8) +
geom_smooth(method = "lm", se=FALSE) + #se = FALSE tells ggplot
scale_colour_manual(values=c("#1E3888", "#FFAD69", "#47A8BD")) + # hex codes start with a "#"
labs(x = "Bill length (mm)",
y = "Bill depth (mm)",
color = "Penguin species",
shape = "Penguin species")+
theme_bw())
Now that we've learned how to:
It's time for you to complete some of this on your own!
Your task: Come up with your own hypothesis using the penguin dataset and write it out. Choose a statistical test (hint: it can be one we used already). Complete your statistical test. Visualize your data using ggplot2
.
Some ideas to get you started: - Does an island have an influence on penguin species? For example, do Adelie penguins that live on different islands vary in some sort of measurement? - Does the sex of a penguin influence a measurement?
### Use this space to complete your exercise.