Visualizing model fit

Introduction to Regression in R

Richie Cotton

Data Evangelist at DataCamp

Hoped for properties of residuals

  • Residuals are normally distributed
  • The mean of the residuals is zero
Introduction to Regression in R

Bream and perch again

Bream: the "good" model

mdl_bream <- lm(mass_g ~ length_cm, data = bream)

The scatter plot of bream masses versus their lengths, with a trend line, that has been shown previously.

Perch: the "bad" model

mdl_perch <- lm(mass_g ~ length_cm, data = perch)

The scatter plot of perch masses versus their lengths, with a trend line, that has been shown previously.

Introduction to Regression in R

Residuals vs. fitted values

Bream

A scatter plot of bream model residuals versus fitted values, with a LOESS trend line. The trend line stays close to the x-axis.

Perch

A scatter plot of perch model residuals versus fitted values, with a LOESS trend line. The trend line forms a V shape.

Introduction to Regression in R

Q-Q plot

Bream

A Q-Q plot of bream model standardized residuals versus theoretical quantiles. The points closely follow the line where residuals and quantiles are equal, except for two outliers.

Perch

A Q-Q plot of perch model standardized residuals versus theoretical quantiles. The points don't closely follow the line where residuals and quantiles are equal, particularly on the right-hand side of the plot.

Introduction to Regression in R

Scale-location

A scatter plot of bream model square root standardized residuals versus fitted values, with a LOESS trend line. The trend line moves slightly upwards as fitted values increase.

A scatter plot of perch model square root standardized residuals versus fitted values, with a LOESS trend line. The trend line moves up and down several times as fitted values increase.

Introduction to Regression in R

autoplot()

library(ggplot2)
library(ggfortify)

autoplot(model_object, which = ???)

Values for which

  • 1 residuals vs. fitted values
  • 2 Q-Q plot
  • 3 scale-location
Introduction to Regression in R

autoplot() with the perch model

autoplot(
  mdl_perch, 
  which = 1:3, 
  nrow = 3, 
  ncol = 1
)

The three diagnostic plots for the perch model shown in a single plot with three panels.

Introduction to Regression in R

'Autoplots, roll out!' -Plotimus Prime

Introduction to Regression in R

Preparing Video For Download...