Skip to content

Instantly share code, notes, and snippets.

@lwaldron
Created June 20, 2022 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwaldron/54b60f0c71b8100d24d89fa1ef38c44a to your computer and use it in GitHub Desktop.
Save lwaldron/54b60f0c71b8100d24d89fa1ef38c44a to your computer and use it in GitHub Desktop.
Residuals plots of the Anscombe datasets
---
title: "Anscombe residuals plots"
author: "Levi Waldron"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Raw data
```{r, echo=FALSE, message=FALSE}
par(mfrow = c(2, 2))
ff <- y ~ x
mods <- setNames(as.list(1:4), paste0("lm", 1:4))
for (i in 1:4) {
ff[2:3] <- lapply(paste0(c("y", "x"), i), as.name)
## or ff[[2]] <- as.name(paste0("y", i))
## ff[[3]] <- as.name(paste0("x", i))
mods[[i]] <- lmi <- lm(ff, data = anscombe)
plot(ff, data = anscombe, xlim = c(3, 20), ylim = c(3, 15))
abline(mods[[i]], lw = 2)
}
```
# Coefficients
```{r, echo=FALSE, message=FALSE}
for (i in 1:4) print(coef(summary(mods[[i]])))
```
# Residuals vs Fitted
```{r, echo=FALSE, message=FALSE}
par(mfrow = c(2, 2))
for (i in 1:4) plot(mods[[i]], 1)
```
# Normal Q-Q
```{r, echo=FALSE, message=FALSE}
par(mfrow = c(2, 2))
for (i in 1:4) plot(mods[[i]], 2)
```
# Scale-Location
```{r, echo=FALSE, message=FALSE}
par(mfrow = c(2, 2))
for (i in 1:4) plot(mods[[i]], 3)
```
# Residuals vs Leverage
```{r, echo=FALSE, message=FALSE}
par(mfrow = c(2, 2))
for (i in 1:4) plot(mods[[i]], 4)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment