Regression diagnostics examples

An example data set[regrex3.csv]

attach(regrex3)

# matrix plot of the data
plot(regrex3)

# set up for plotting
oldpar <- par(mfrow = c(2, 2))

# regression diagnostics examples
# first regression
regr.y1x1 <- lm(y1 ~ x1)
summary(regr.y1x1)
plot(regr.y1x1, which=c(1,2,3,4), main="First Regression")

# add a second predictor
regr.y1x1x2 <- lm(y1 ~ x1 + x2)
summary(regr.y1x1x2)
plot(regr.y1x1x2, which=c(1,2,3,4), main="Two Predictors")

# outliers
regr.y2x1 <- lm(y2 ~ x1)
summary(regr.y2x1)
plot(regr.y2x1, which=c(1,2,3,4), main="Outliers")

# no help for outliers
regr.y2x1x2 <- lm(y2 ~ x1 + x2)
summary(regr.y2x1x2)
plot(regr.y2x1x2, which=c(1,2,3,4), main="Still Outliers")

# heteroscedasticity
regr.y3x1 <- lm(y3 ~ x1)
summary(regr.y3x1)
plot(regr.y3x1, which=c(1,2,3,4), main="Heteroscedasticity")

# nonlinear relationship
regr.y4x1 <- lm(y4 ~ x1)
summary(regr.y4x1)
plot(regr.y4x1, which=c(1,2,3,4), main="Nonlinear Relationship")

# try another predictor
regr.y4x1x2 <- lm(y4 ~ x1+x2)
summary(regr.y4x1x2)
plot(regr.y4x1x2, which=c(1,2,3,4), main="Additional Predictor")

# alternative nonlinear y4 ~ log(x1)
regr.y4logx1 <- lm(y4 ~ log(x1))
plot(regr.y4logx1, which=c(1,2,3,4), main="Transformation")

# alternative nonlinear (quadratic) y4 ~ x1, x1^2
regr.y4x1x1 <- lm(y4 ~ x1 +I(x1^2))
plot(regr.y4x1x1, which=c(1,2,3,4), main="Quadratic Polynomial")

# model inadequacy, missing predictor
regr.y5x1 <- lm(y5 ~ x1)
summary(regr.y5x1)
plot(regr.y5x1, which=c(1,2,3,4), main="Missing Predictor")

# check for correlation between residuals and other predictors
par(oldpar)
plot(regr.y5x1$residual ~ x2)
oldpar <- par(mfrow = c(2, 2))

# last model, y5 ~ x1 and x2
regr.y5x1x2 <- lm(y5 ~ x1+x2)
summary(regr.y5x1x2)
plot(regr.y5x1x2, which=c(1,2,3,4), main="Two Predictors")

par(oldpar)