1. Learn
  2. /
  3. Courses
  4. /
  5. Helsinki Open Data Science

Connected

Exercise

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.

Instructions

100 XP
  • 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 argument which.
  • 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.