Linear Models & Regression

28.1 Formulas

main effects + interaction (x1 + x2 + x1:x2) all other columns y ~ x + I(x^2) polynomial term (I() = "as is") y ~ poly(x, 3) y ~ 0 + x / y ~ x - 1 y ~ factor(g) categorical predictor (dummy coding) smooth term (mgcv)

28.2 Fitting & interpreting

fit <- lm(mpg ~ wt + hp + factor(cyl), data = mtcars)
summary(fit)          # coefficients, SE, t, p, R², F-test
coef(fit); confint(fit); residuals(fit); fitted(fit); vcov(fit)
anova(fit)            # sequential ANOVA table
AIC(fit); BIC(fit); logLik(fit)
predict(fit, newdata = data.frame(wt = 3, hp = 120, cyl = 6), interval = "prediction")
plot(fit)             # 4 diagnostic plots: residuals vs fitted, Q-Q, scale-location, leverage
broom::tidy(fit, conf.int = TRUE); broom::glance(fit); broom::augment(fit)

28.3 Model comparison & selection

fit1 <- lm(mpg ~ wt, mtcars); fit2 <- lm(mpg ~ wt + hp, mtcars)
anova(fit1, fit2)                       # nested model F-test
step(lm(mpg ~ ., mtcars), direction = "both")   # stepwise AIC (use with caution)
update(fit2, . ~ . + qsec)

28.4 Diagnostics & assumptions

Linearity, independence, homoscedasticity, normality of residuals, no influential outliers, low multicollinearity.

car::vif(fit)                     # multicollinearity
car::ncvTest(fit)                 # heteroscedasticity; lmtest::bptest(fit)
car::durbinWatsonTest(fit)        # autocorrelation
cooks.distance(fit); hatvalues(fit); car::influencePlot(fit)
performance::check_model(fit)     # all-in-one diagnostic panel
sandwich::vcovHC(fit, type = "HC3") |> lmtest::coeftest(x = fit, vcov. = _)   # robust SEs

28.5 Categorical predictors & contrasts

contrasts(penguins$species)
penguins$species <- relevel(penguins$species, ref = "Gentoo")
emmeans::emmeans(fit, pairwise ~ cyl)   # estimated marginal means and pairwise comparisons

28.6 Regularized & robust regression

library(glmnet)
X <- model.matrix(mpg ~ ., mtcars)[, -1]; y <- mtcars$mpg
cv <- cv.glmnet(X, y, alpha = 1)        # LASSO (alpha = 0 ridge, between = elastic net)
coef(cv, s = "lambda.min")
MASS::rlm(mpg ~ wt, data = mtcars)      # robust regression
quantreg::rq(mpg ~ wt, tau = 0.5, data = mtcars)   # quantile regression