Skip to content

Instantly share code, notes, and snippets.

@apreshill
Last active July 12, 2020 21:57
Show Gist options
  • Save apreshill/9d33891b5f9be4669ada20f76f101baa to your computer and use it in GitHub Desktop.
Save apreshill/9d33891b5f9be4669ada20f76f101baa to your computer and use it in GitHub Desktop.
Shows us why visualizing residuals from a model is important
library(broom)
library(ggplot2)
# read in the data
# others available here: http://www4.stat.ncsu.edu/~stefanski/NSF_Supported/Hidden_Images/stat_res_plots.html
owl <- read.table("http://www4.stat.ncsu.edu/~stefanski/NSF_Supported/Hidden_Images/orly_owl_files/orly_owl_Lin_4p_5_flat.txt",
header = FALSE)
# fit the linear model
fit <- lm(V1 ~ . - 1, data = owl)
# save the residuals via broom
owl_vars <- augment(fit)
# plot fitted vs residuals
ggplot(owl_vars, aes(x = .fitted, y = .resid)) +
geom_point(size = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment