With factors selected following mpg ~ am + hp + wt + qsec (the code below is evaluated in the next slide)
data(mtcars)
library(car)
library(knitr)
mf <- lm(mpg ~ am + hp + wt + qsec, data = mtcars)
r.squared <- round(summary(mf)$r.squared, 2)
adj.r.squared <- round(summary(mf)$adj.r.squared, 2)
rse <- round(summary(mf)$sigma, 2)
st <- shapiro.test(mf$residuals)
method <- st$method
W <- round(st$statistic, 2)
p.value <- round(st$p.value, 4)
dfprint <- data.frame(RSE = rse, R.sq = r.squared, Adj.R.sq = adj.r.squared,
Method = method, W = W, p.value = p.value)
kable(dfprint, format = "markdown", row.names = FALSE, align="c")
resid <- residuals(mf)
fitted <- fitted.values(mf)
par(mfrow = c(3,2), mar = c(2, 2, 2, 2))
plot(density(resid), xlab = "Residuals", ylab = "Density", main = "")
plot(fitted, resid, xlab = "Predicted values", ylab = "Residuals")
abline(h = 0, col = "red", lty = "dashed")
plot(mf, cex = 0.8)