library(ISLR2)
library(boot)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

5. In Chapter 4, we used logistic regression to predict the probability of default using income and balance on the Default data set. We will now estimate the test error of this logistic regression model using the validation set approach. Do not forget to set a random seed before beginning your analysis.

# Load data
default <- read.csv("/Users/saransh/Downloads/Statistical_Learning_Resources/Default.csv", na.strings = "?")
default <- na.omit(default)
default$default <- as.factor(default$default)

# Set seed
set.seed(1)

(a) Fit a logistic regression model that uses income and balance to predict default.

model_default <- glm(default ~ income + balance, data = default, family = "binomial")
summary(model_default)
## 
## Call:
## glm(formula = default ~ income + balance, family = "binomial", 
##     data = default)
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -1.154e+01  4.348e-01 -26.545  < 2e-16 ***
## income       2.081e-05  4.985e-06   4.174 2.99e-05 ***
## balance      5.647e-03  2.274e-04  24.836  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 2920.6  on 9999  degrees of freedom
## Residual deviance: 1579.0  on 9997  degrees of freedom
## AIC: 1585
## 
## Number of Fisher Scoring iterations: 8

Observation:

- Both income and balance are statistically significant predictors of default.
- The positive coefficient for balance suggests that higher balances are associated with higher default probabilities, while the smaller, but still significant, coefficient for income implies that income has a less impactful relationship with default status.

(b) Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:

  1. Split the sample set into a training set and a validation set.
  2. Fit a multiple logistic regression model using only the training observations.
  3. Obtain a prediction of default status for each individual in the validation set by computing the posterior probability of default for that individual, and classifying the individual to the default category if the posterior probability is greater than 0.5.
  4. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
set.seed(1)
train <- sample(nrow(default), nrow(default)/2)
train_data <- default[train, ]
test_data <- default[-train, ]

model_val <- glm(default ~ income + balance, data = train_data, family = "binomial")
probs <- predict(model_val, test_data, type = "response")
preds <- ifelse(probs > 0.5, "Yes", "No")
mean(preds != test_data$default)
## [1] 0.0254

Observation:

- The validation error is approximately 2.54%, indicating good model accuracy.
- This low error rate suggests the model generalizes well to unseen data when using these predictors.

(c) Repeat the process in (b) three times, using three different splits of the observations into a training set and a validation set. Comment on the results obtained.

set.seed(2)
train2 <- sample(nrow(default), nrow(default)/2)
model2 <- glm(default ~ income + balance, data = default[train2, ], family = "binomial")
probs2 <- predict(model2, default[-train2, ], type = "response")
preds2 <- ifelse(probs2 > 0.5, "Yes", "No")
error2 <- mean(preds2 != default[-train2, ]$default)

set.seed(3)
train3 <- sample(nrow(default), nrow(default)/2)
model3 <- glm(default ~ income + balance, data = default[train3, ], family = "binomial")
probs3 <- predict(model3, default[-train3, ], type = "response")
preds3 <- ifelse(probs3 > 0.5, "Yes", "No")
error3 <- mean(preds3 != default[-train3, ]$default)

error2
## [1] 0.0238
error3
## [1] 0.0264

Comment:

- Validation errors from different splits: 0.0238 and 0.0264, close to the original error of 0.0254.
- Confirms the model’s robustness and consistency across different random training/validation splits.

(d) Now consider a logistic regression model that predicts the probability of default using income, balance, and a dummy variable for student. Estimate the test error for this model using the validation set approach. Comment on whether or not including a dummy variable for student leads to a reduction in the test error rate.

set.seed(4)
train4 <- sample(nrow(default), nrow(default)/2)
model4 <- glm(default ~ income + balance + student, data = default[train4, ], family = "binomial")
probs4 <- predict(model4, default[-train4, ], type = "response")
preds4 <- ifelse(probs4 > 0.5, "Yes", "No")
error4 <- mean(preds4 != default[-train4, ]$default)
error4
## [1] 0.0262

Observation:

- Including student does not meaningfully reduce test error (0.0262).
- This suggests student status may not add much additional predictive power beyond income and balance.

6. We continue to consider the use of a logistic regression model to predict the probability of default using income and balance on the Default data set. In particular, we will now compute estimates for the standard errors of the income and balance logistic regression coefficients in two different ways: (1) using the bootstrap, and (2) using the standard formula for computing the standard errors in the glm() function. Do not forget to set a random seed before beginning your analysis.

(a) Using the summary() and glm() functions, determine the estimated standard errors for the coefficients associated with income and balance in a multiple logistic regression model that uses both predictors.

model_se <- glm(default ~ income + balance, data = default, family = "binomial")
summary(model_se)
## 
## Call:
## glm(formula = default ~ income + balance, family = "binomial", 
##     data = default)
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -1.154e+01  4.348e-01 -26.545  < 2e-16 ***
## income       2.081e-05  4.985e-06   4.174 2.99e-05 ***
## balance      5.647e-03  2.274e-04  24.836  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 2920.6  on 9999  degrees of freedom
## Residual deviance: 1579.0  on 9997  degrees of freedom
## AIC: 1585
## 
## Number of Fisher Scoring iterations: 8

Observation:

- Standard errors for income and balance are approximately 4.985e-06 and 2.274e-04 respectively.

(b) Write a function, boot.fn(), that takes as input the Default data set as well as an index of the observations, and that outputs the coefficient estimates for income and balance in the multiple logistic regression model.

boot.fn <- function(data, index) {
  coef(glm(default ~ income + balance, data = data, family = binomial, subset = index))[2:3]
}

(c) Use the boot() function together with your boot.fn() function to estimate the standard errors of the logistic regression coefficients for income and balance.

set.seed(5)
boot_results <- boot(default, boot.fn, R = 1000)
boot_results
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = default, statistic = boot.fn, R = 1000)
## 
## 
## Bootstrap Statistics :
##         original       bias     std. error
## t1* 2.080898e-05 2.733337e-07 4.909003e-06
## t2* 5.647103e-03 1.512143e-05 2.332406e-04

(d) Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.

boot_results$t0
##       income      balance 
## 2.080898e-05 5.647103e-03
apply(boot_results$t, 2, sd)
## [1] 4.909003e-06 2.332406e-04

Comment:

- Bootstrap SE for income: ~4.909e-06, for balance: ~2.332e-04.
- These closely match the standard GLM estimates.
- Indicates that both methods provide reliable SEs, though bootstrap may better capture data-specific variability.

7. In Sections 5.3.2 and 5.3.3, we saw that the cv.glm() function can be used in order to compute the LOOCV test error estimate. Alternatively, one could compute those quantities using just the glm() and predict.glm() functions, and a for loop. You will now take this approach in order to compute the LOOCV error for a simple logistic regression model on the Weekly data set. Recall that in the context of classification problems, the LOOCV error is given in (5.4).

weekly <- read.csv("/Users/saransh/Downloads/Statistical_Learning_Resources/Weekly.csv", na.strings = "?")
weekly <- na.omit(weekly)
weekly$Direction <- as.factor(weekly$Direction)

(a) Fit a logistic regression model that predicts Direction using Lag1 and Lag2.

logit_weekly <- glm(Direction ~ Lag1 + Lag2, data = weekly, family = binomial)
summary(logit_weekly)
## 
## Call:
## glm(formula = Direction ~ Lag1 + Lag2, family = binomial, data = weekly)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  0.22122    0.06147   3.599 0.000319 ***
## Lag1        -0.03872    0.02622  -1.477 0.139672    
## Lag2         0.06025    0.02655   2.270 0.023232 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 1496.2  on 1088  degrees of freedom
## Residual deviance: 1488.2  on 1086  degrees of freedom
## AIC: 1494.2
## 
## Number of Fisher Scoring iterations: 4

Observation:

- Lag2 is significant (p < 0.05), but Lag1 is not.
- Indicates Lag2 may be more predictive of weekly market movement.

(b) Fit a logistic regression model that predicts Direction using Lag1 and Lag2 using all but the first observation.

logit_loocv <- glm(Direction ~ Lag1 + Lag2, data = weekly[-1, ], family = binomial)

(c) Use the model from (b) to predict the direction of the first observation. You can do this by predicting that the first observation will go up if P(Direction = “Up”|Lag1, Lag2) > 0.5. Was this observation correctly classified?

prob1 <- predict(logit_loocv, newdata = weekly[1, ], type = "response")
pred1 <- ifelse(prob1 > 0.5, "Up", "Down")
actual1 <- weekly$Direction[1]
pred1 == actual1
## [1] FALSE

Observation:

- The model misclassifies the first observation (FALSE).
- LOOCV examines how well each observation is predicted by all others.

(d) Write a for loop from i = 1 to i = n, where n is the number of observations in the data set, that performs each of the following steps:

  1. Fit a logistic regression model using all but the ith observation to predict Direction using Lag1 and Lag2.
  2. Compute the posterior probability of the market moving up for the ith observation.
  3. Use the posterior probability for the ith observation in order to predict whether or not the market moves up.
  4. Determine whether or not an error was made in predicting the direction for the ith observation. If an error was made, then indicate this as a 1, and otherwise indicate it as a 0. ## (e) Take the average of the n numbers obtained in (d)iv in order to obtain the LOOCV estimate for the test error. Comment on the results.
n <- nrow(weekly)
errors <- rep(0, n)

for (i in 1:n) {
  fit_loo <- glm(Direction ~ Lag1 + Lag2, data = weekly[-i, ], family = binomial)
  prob <- predict(fit_loo, newdata = weekly[i, ], type = "response")
  pred <- ifelse(prob > 0.5, "Up", "Down")
  errors[i] <- ifelse(pred != weekly$Direction[i], 1, 0)
}

mean(errors)  # LOOCV error estimate
## [1] 0.4499541

Comment:

- The LOOCV error is approximately 0.45 (45%), indicating moderate predictive ability.
- Despite being computationally expensive, LOOCV gives a reliable, low-bias error estimate for classification models.

9. We will now consider the Boston housing data set, from the ISLR2 library.

boston <- read.csv("/Users/saransh/Downloads/Statistical_Learning_Resources/Boston.csv", na.strings = "?")
boston <- na.omit(boston)

(a) Based on this data set, provide an estimate for the population mean of medv. Call this estimate ˆμ.

mu_hat <- mean(boston$medv)
mu_hat
## [1] 22.53281

(b) Provide an estimate of the standard error of ˆμ. Interpret this result.

se_formula <- sd(boston$medv) / sqrt(nrow(boston))
se_formula
## [1] 0.4088611

Interpretation:

- This standard error of approximately 0.4089 represents the average variability of the sample mean if we repeatedly sampled from the Boston population.
- A smaller value indicates that the sample mean is a stable and reliable estimate of the true population mean.

(c) Now estimate the standard error of ˆμ using the bootstrap. How does this compare to your answer from (b)?

boot.fn.mean <- function(data, index) {
  mean(data$medv[index])
}

set.seed(6)
boot_mu <- boot(boston, statistic = boot.fn.mean, R = 1000)
boot_mu
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = boston, statistic = boot.fn.mean, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original     bias    std. error
## t1* 22.53281 0.01886976   0.4176042

Comparison:

- The bootstrap estimate of standard error (≈ 0.4176) is very close to the analytical standard error from (b) (≈ 0.4089), reinforcing that the bootstrap method provides a reliable alternative when the formula is unknown or infeasible.

(d) Based on your bootstrap estimate from (c), provide a 95 % confidence interval for the mean of medv. Compare it to the results obtained using t.test(Boston$medv).

mu_se_boot <- sd(boot_mu$t)
ci_boot <- c(mu_hat - 2 * mu_se_boot, mu_hat + 2 * mu_se_boot)
ci_boot
## [1] 21.69760 23.36801
# Compare with t.test()
t.test(boston$medv) 
## 
##  One Sample t-test
## 
## data:  boston$medv
## t = 55.111, df = 505, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  21.72953 23.33608
## sample estimates:
## mean of x 
##  22.53281

Observation:

- The bootstrap CI is [21.698, 23.368], and the t-test CI is [21.73, 23.34]. Both intervals are nearly identical, indicating consistent inference regardless of the method.
- This demonstrates that bootstrap confidence intervals can serve as valid alternatives to classical methods.

(e) Based on this data set, provide an estimate, ˆμmed, for the median value of medv in the population.

mu_med <- median(boston$medv)
mu_med
## [1] 21.2

Observation:

- The estimated median house value is 21.2, which is slightly lower than the mean.
- This suggests that the distribution of medv is right-skewed with a few higher values pulling the mean upward.

(f) We now would like to estimate the standard error of ˆμmed. Unfortunately, there is no simple formula for computing the standard error of the median. Instead, estimate the standard error of the median using the bootstrap. Comment on your findings.

boot.fn.median <- function(data, index) {
  median(data$medv[index])
}

boot_median <- boot(boston, statistic = boot.fn.median, R = 1000)
sd(boot_median$t)
## [1] 0.3889205

Comment:

- The bootstrap estimate of the standard error of the median is approximately 0.389. Since there’s no closed-form expression for the standard error of the median, this shows how useful bootstrapping can be for estimating uncertainty in robust statistics.

(g) Based on this data set, provide an estimate for the tenth percentile of medv in Boston census tracts. Call this quantity ˆμ0.1.

mu_10 <- quantile(boston$medv, 0.1)
mu_10
##   10% 
## 12.75

Observation:

- The 10th percentile of medv is 12.75, meaning 10% of Boston census tracts have a median home value below this amount.
- This is useful for identifying the lower end of the housing market distribution.

(h) Use the bootstrap to estimate the standard error of ˆμ0.1. Comment on your findings.

boot.fn.q10 <- function(data, index) {
  quantile(data$medv[index], 0.1)
}

boot_q10 <- boot(boston, statistic = boot.fn.q10, R = 1000)
sd(boot_q10$t)
## [1] 0.4992374

Comment:

- The bootstrap standard error of the 10th percentile is approximately 0.499. This reflects greater variability in estimating the tails of a distribution, emphasizing the uncertainty in the lower end of home values.
- This insight is important when evaluating housing affordability and extremes in real estate data.