Graphical model validation
R makes it easy to graphically explore the validity of your model assumptions. If you give a linear model object as the first argument to the plot()
function, the function automatically assumes you want diagnostic plots and will produce them. You can check the help page of plotting an lm object by typing ?plot.lm
or help(plot.lm)
to the R console.
In the plot function you can then use the argument which
to choose which plots you want. which
must be an integer vector corresponding to the following list of plots:
which | graphic |
---|---|
1 | Residuals vs Fitted values |
2 | Normal QQ-plot |
3 | Standardized residuals vs Fitted values |
4 | Cook's distances |
5 | Residuals vs Leverage |
6 | Cook's distance vs Leverage |
We will focus on plots 1, 2 and 5: Residuals vs Fitted values, Normal QQ-plot and Residuals vs Leverage.
This exercise is part of the course
Helsinki Open Data Science
Exercise instructions
- Create the linear model object
my_model2
- Produce the following diagnostic plots using the
plot()
function: Residuals vs Fitted values, Normal QQ-plot and Residuals vs Leverage using the argumentwhich
. - Before the call to the
plot()
function, add the following:par(mfrow = c(2,2))
. This will place the following 4 graphics to the same plot. Execute the code again to see the effect.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# learning2014 is available
# create a regression model with multiple explanatory variables
my_model2 <- lm(points ~ attitude + stra, data = learning2014)
# draw diagnostic plots using the plot() function. Choose the plots 1, 2 and 5