Time to interact: Random Intercept and Random Slope Model with interaction
Finally, we can fit a random intercept and slope model that allows for a group × time interaction.
This exercise is part of the course
Helsinki Open Data Science
Exercise instructions
- Write the same model as in the previous exercise but add
Time
*Group
interaction. - Print out the summary of the model
- Compute the analysis of variance tables of the models
RATS_ref2
andRATS_ref1
- Again pay attention to the likelihood ratio test chi-squared value and the according p-value. The lower the value the better the fit against the comparison model.
- Draw the plot of observed values of RATSL (this is the same plot drawn earlier)
- Create a vector of the fitted values of the model using the function
fitted()
- Use for example
mutate()
to add the vectorFitted
as a new column to RATSL - Draw the plot of fitted values of RATSL
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# dplyr, tidyr, lme4, ggplot2, RATS and RATSL are available
# create a random intercept and random slope model with the interaction
RATS_ref2 <- "Write the model here"
# print a summary of the model
# perform an ANOVA test on the two models
anova(RATS_ref2, RATS_ref1)
# draw the plot of RATSL with the observed Weight values
ggplot(RATSL, aes(x = Time, y = Weight, group = ID)) +
geom_line(aes(linetype = Group)) +
scale_x_continuous(name = "Time (days)", breaks = seq(0, 60, 20)) +
scale_y_continuous(name = "Observed weight (grams)") +
theme(legend.position = "top")
# Create a vector of the fitted values
Fitted <- "Change me!"
# Create a new column fitted to RATSL
# draw the plot of RATSL with the Fitted values of weight
ggplot(RATSL, aes(x = Time, y = "Change me!", group = ID)) +
geom_line(aes(linetype = Group)) +
scale_x_continuous(name = "Time (days)", breaks = seq(0, 60, 20)) +
scale_y_continuous(name = "Fitted weight (grams)") +
theme(legend.position = "top")