# Applied Statistical Learning in R and Python, Chapter 1 lab (R version).
#
# Chapter 1 of James et al. (2021) has no lab of its own. This script is a first tour of R on the
# three data sets the chapter introduces: Wage, Smarket and NCI60. It loads each one, summarises it,
# draws a first plot, and reproduces the two numbers quoted in the chapter (the 60% of Figure 1.3 and
# the principal components of Figure 1.4). The Python version, ch01_lab.py, does the same steps in the
# same order, and the lecture notes show the two side by side.
#
# Run from this folder:  Rscript ch01_lab.R
# Needs the packages ISLR2 (the book's data) and MASS (for qda); both are on CRAN.
# Plots are written to ../img/ch01/lab-r-*.png

suppressPackageStartupMessages({
  library(ISLR2)
  library(MASS)
})
out_dir <- file.path("..", "img", "ch01")
dir.create(out_dir, showWarnings = FALSE, recursive = TRUE)

## step: vectors
x <- c(1, 6, 2)
y <- c(1, 4, 3)
length(x)
x + y                       # elementwise
x[1]                        # R counts from 1

## step: matrices
A <- matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
B <- matrix(c(5, 6, 7, 8), nrow = 2, byrow = TRUE)
A %*% B                     # matrix product: 19 22 / 43 50
A * B                       # elementwise product: 5 12 / 21 32
t(A)                        # transpose
A[2, 1]                     # row 2, column 1: the entry a_21 = 3

## step: wage-load
dim(Wage)                   # n = 3000 rows, 11 columns
names(Wage)
head(Wage[, c("year", "age", "education", "wage")], 3)

## step: wage-summary
summary(Wage$wage)
table(Wage$education)
round(tapply(Wage$wage, Wage$education, mean), 2)

## step: wage-line
fit_year <- lm(wage ~ year, data = Wage)
coef(fit_year)
6 * coef(fit_year)["year"]  # fitted change from 2003 to 2009, in thousands of dollars

## step: wage-plot
png(file.path(out_dir, "lab-r-wage.png"), width = 900, height = 420, res = 110)
par(mfrow = c(1, 2), mar = c(4.2, 4.2, 1, 1))
plot(Wage$age, Wage$wage, pch = 20, cex = 0.4, col = "steelblue",
     xlab = "Age", ylab = "Wage")
smooth <- lowess(Wage$age, Wage$wage, f = 0.3)
lines(smooth, col = "darkblue", lwd = 2)
boxplot(wage ~ as.integer(education), data = Wage, col = "lightblue",
        xlab = "Education level", ylab = "Wage")
invisible(dev.off())

## step: smarket-load
dim(Smarket)
table(Smarket$Direction)
round(cor(Smarket[, -9]), 2)

## step: smarket-lags
aggregate(cbind(Lag1, Lag2, Lag3) ~ Direction, data = Smarket, FUN = mean)
t.test(Lag1 ~ Direction, data = Smarket)$statistic

## step: smarket-plot
png(file.path(out_dir, "lab-r-smarket.png"), width = 900, height = 420, res = 110)
par(mfrow = c(1, 2), mar = c(4.2, 4.2, 1, 1))
boxplot(Lag1 ~ Direction, data = Smarket, col = c("deepskyblue", "chocolate"),
        xlab = "Today's direction", ylab = "Yesterday's % change")
plot(Smarket$Volume, pch = 20, cex = 0.4, col = "grey40",
     xlab = "Day (2001 to 2005)", ylab = "Volume (billions of shares)")
invisible(dev.off())

## step: smarket-qda
train <- Smarket$Year < 2005
qda_fit <- qda(Direction ~ Lag1 + Lag2, data = Smarket, subset = train)
qda_pred <- predict(qda_fit, Smarket[!train, ])
p_down <- qda_pred$posterior[, "Down"]
table(predicted = qda_pred$class, actual = Smarket$Direction[!train])
mean(qda_pred$class == Smarket$Direction[!train])

## step: nci-load
nci_labs <- NCI60$labs
nci_data <- NCI60$data
dim(nci_data)               # 64 cell lines, 6830 genes
table(nci_labs)

## step: nci-pca
pr_out <- prcomp(nci_data, scale = TRUE)
pve <- pr_out$sdev^2 / sum(pr_out$sdev^2)
round(pve[1:3], 4)
round(head(pr_out$x[, 1:2], 3), 3)

## step: nci-plot
png(file.path(out_dir, "lab-r-nci60.png"), width = 900, height = 420, res = 110)
par(mfrow = c(1, 2), mar = c(4.2, 4.2, 1, 1))
cols <- rainbow(length(unique(nci_labs)))[as.integer(factor(nci_labs))]
plot(pr_out$x[, 1:2], col = cols, pch = 19, xlab = "Z1", ylab = "Z2")
plot(100 * cumsum(pve), type = "o", pch = 20, col = "brown3", ylim = c(0, 100),
     xlab = "Number of components", ylab = "Cumulative % of variance")
invisible(dev.off())
