Load relevant packages and create the predictor.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
n <- 100
x <- seq(-5, 50, length = n)
Create the response as a linear function of the predictor plus some
random error.
## Error with constant variance, Normally distributed
y <- 3*x -1 + rnorm(n, mean = 0, sd = 2)
## Error with constant variance, not Normally distributed
#y <- 3*x -1 + ?
## Error with non-constant variance, Normally distributed
#y1 <- 3*x -1 + ?
#y2 <- 3*x -1 + ?
#y <- c(y1, y2)
## Error with non-constant variance , not Normally distributed
#y1 <- 3*x -1 + ?
#y2 <- 3*x -1 + ?
#y <- c(y1, y2)
Fit a SLR model and look at the residuals
regmod <- lm(y~x)
my_data <- tibble(response = y, predictor = x, resids = regmod$residuals, fits = regmod$fitted.values)
ggplot(my_data, aes(x=fits, y=resids)) +
geom_point() +
labs(title="Residual plot", subtitle="Normal/non-Normal error with constant/non-constant variance") ## Edit the subtitle accordingly
ggplot(my_data, aes(sample=resids)) +
geom_qq() +
geom_qq_line() +
labs(title="Normal quantile plot of residuals", subtitle="Normal/non-Normal error with constant/non-constant variance") ## Edit the subtitle accordingly