# Applied Statistical Learning in R and Python, Chapter 3 lab (R version): linear regression.
#
# The same analyses as the R lab of James et al. (2021), Section 3.6, and the Python lab of James
# et al. (2023), Section 3.6, written independently: simple regression of medv on lstat in the
# Boston data (estimates, confidence intervals, confidence and prediction intervals, diagnostic
# plots, leverage), multiple regression, variance inflation factors, an interaction, polynomial
# and log transformations compared by F-tests, qualitative predictors in the Carseats data, and a
# first function. The Python version, ch03_lab.py, does the same steps in the same order and gives
# the same numbers; the lecture notes show the two side by side.
#
# Run from this folder:  Rscript ch03_lab.R
# Needs the packages ISLR2 (the book's data) and car (vif); both are on CRAN.
# Plots are written to ../img/ch03/lab-r-*.png

suppressWarnings(suppressPackageStartupMessages({
  library(ISLR2)
  library(car)
}))
out_dir <- file.path("..", "img", "ch03")
dir.create(out_dir, showWarnings = FALSE, recursive = TRUE)
options(width = 100)

## ==== step: boston
dim(Boston)                      # 506 census tracts, 12 predictors and the response medv
names(Boston)

## ==== step: simple-fit
lm.fit <- lm(medv ~ lstat, data = Boston)
summary(lm.fit)

## ==== step: simple-extract
coef(lm.fit)
confint(lm.fit)                  # 95% intervals: estimate -/+ t(0.975, 504) * SE
c(RSE = summary(lm.fit)$sigma, R2 = summary(lm.fit)$r.squared)

## ==== step: simple-predict
new <- data.frame(lstat = c(5, 10, 15))
predict(lm.fit, new, interval = "confidence")
predict(lm.fit, new, interval = "prediction")

## ==== step: simple-plot
png(file.path(out_dir, "lab-r-fit.png"), width = 640, height = 480, res = 110)
plot(Boston$lstat, Boston$medv, pch = 20, col = "grey40", xlab = "lstat", ylab = "medv")
abline(lm.fit, lwd = 3, col = "red")
invisible(dev.off())

## ==== step: simple-diagnostics
png(file.path(out_dir, "lab-r-diagnostics.png"), width = 900, height = 800, res = 110)
par(mfrow = c(2, 2))
plot(lm.fit)                     # residuals, normal Q-Q, scale-location, residuals vs leverage
invisible(dev.off())

## ==== step: simple-leverage
h <- hatvalues(lm.fit)
which.max(h)                     # observation 375 (R counts from 1)
max(h)
head(sort(abs(rstudent(lm.fit)), decreasing = TRUE), 3)

## ==== step: multiple
lm.fit <- lm(medv ~ lstat + age, data = Boston)
summary(lm.fit)$coefficients

## ==== step: multiple-all
lm.fit <- lm(medv ~ ., data = Boston)
summary(lm.fit)
c(R2 = summary(lm.fit)$r.squared, RSE = summary(lm.fit)$sigma)

## ==== step: vif
round(vif(lm.fit), 3)

## ==== step: minus-age
lm.fit1 <- update(lm.fit, ~ . - age)          # the same as lm(medv ~ . - age, data = Boston)
round(summary(lm.fit1)$coefficients, 4)

## ==== step: interaction
summary(lm(medv ~ lstat * age, data = Boston))$coefficients

## ==== step: quadratic
lm.fit <- lm(medv ~ lstat, data = Boston)
lm.fit2 <- lm(medv ~ lstat + I(lstat^2), data = Boston)
summary(lm.fit2)$coefficients
anova(lm.fit, lm.fit2)

## ==== step: quadratic-age
lm.fit.a <- lm(medv ~ lstat + age, data = Boston)
lm.fit.q <- lm(medv ~ poly(lstat, 2) + age, data = Boston)
summary(lm.fit.q)$coefficients
anova(lm.fit.a, lm.fit.q)

## ==== step: quadratic-diagnostics
png(file.path(out_dir, "lab-r-diagnostics-quad.png"), width = 900, height = 800, res = 110)
par(mfrow = c(2, 2))
plot(lm.fit2)
invisible(dev.off())

## ==== step: poly5
lm.fit5 <- lm(medv ~ poly(lstat, 5), data = Boston)
summary(lm.fit5)$coefficients
raw5 <- lm(medv ~ poly(lstat, 5, raw = TRUE), data = Boston)
max(abs(fitted(lm.fit5) - fitted(raw5)))      # same fitted values, different coefficients

## ==== step: log
summary(lm(medv ~ log(rm), data = Boston))$coefficients

## ==== step: carseats
dim(Carseats)
lm.fit <- lm(Sales ~ . + Income:Advertising + Price:Age, data = Carseats)
summary(lm.fit)

## ==== step: contrasts
contrasts(Carseats$ShelveLoc)

## ==== step: function
LoadLibraries <- function() {
  library(ISLR2)
  library(car)
  print("The libraries have been loaded.")
}
LoadLibraries()
