1. 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.
  1. Fit a logistic regression model that uses income and balance to predict default.
# Load necessary library and data
library(ISLR2)
set.seed(42)

# Fit logistic regression model
fit <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(fit)
## 
## 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
  1. 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 train ing 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.
# Split data into training and validation sets
train <- sample(nrow(Default), nrow(Default) / 2)

# Fit logistic regression model on training data
fit <- glm(default ~ income + balance, data = Default, family = "binomial", subset = train)

# Predict on validation set
pred <- ifelse(predict(fit, newdata = Default[-train, ], type = "response") > 0.5, "Yes", "No")

# Compute validation set error
table(pred, Default$default[-train])
##      
## pred    No  Yes
##   No  4817  110
##   Yes   20   53
validation_error <- mean(pred != Default$default[-train])
validation_error
## [1] 0.026
  1. 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.
# Repeat the process 3 times using replicate
set.seed(42)
errors <- replicate(3, {
  train <- sample(nrow(Default), nrow(Default) / 2)
  fit <- glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
  pred <- ifelse(predict(fit, newdata = Default[-train, ], type = "response") > 0.5, "Yes", "No")
  mean(pred != Default$default[-train])
})

errors
## [1] 0.0260 0.0260 0.0294
  1. 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 val idation set approach. Comment on whether or not including a dummyvariable for student leads to a reduction in the test error rate.
# Logistic regression with student variable
errors_with_student <- replicate(3, {
  train <- sample(nrow(Default), nrow(Default) / 2)
  fit <- glm(default ~ income + balance + student, data = Default, family = "binomial", subset = train)
  pred <- ifelse(predict(fit, newdata = Default[-train, ], type = "response") > 0.5, "Yes", "No")
  mean(pred != Default$default[-train])
})

errors_with_student
## [1] 0.0268 0.0278 0.0256

Including the student variable does not significantly reduce the test error compared to the previous model.


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.

  1. 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.
# Fit logistic regression model
fit <- glm(default ~ income + balance, data = Default, family = "binomial")

# Summary to get standard errors
summary(fit)$coefficients
##                  Estimate   Std. Error    z value      Pr(>|z|)
## (Intercept) -1.154047e+01 4.347564e-01 -26.544680 2.958355e-155
## income       2.080898e-05 4.985167e-06   4.174178  2.990638e-05
## balance      5.647103e-03 2.273731e-04  24.836280 3.638120e-136
  1. 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) {
  fit <- glm(default ~ income + balance, data = data[index, ], family = "binomial")
  return(coef(fit))
}
  1. Use the boot() function together with your boot.fn() function to estimate the standard errors of the logistic regression coefficients for income and balance.
library(boot)

# Set seed for reproducibility
set.seed(42)

# Bootstrap to estimate standard errors
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* -1.154047e+01 -2.292405e-02 4.435269e-01
## t2*  2.080898e-05  2.737444e-08 5.073444e-06
## t3*  5.647103e-03  1.176249e-05 2.299133e-04
  1. Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.

The standard errors obtained via bootstrapping are comparable to those provided by glm(), validating the results.


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).

  1. Fit a logistic regression model that predicts Direction using Lag1 and Lag2.
# Load Weekly data and fit logistic regression model
fit <- glm(Direction ~ Lag1 + Lag2, data = Weekly, family = "binomial")
summary(fit)
## 
## 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
  1. Fit a logistic regression model that predicts Direction using Lag1 and Lag2 using all but the first observation.
# Fit model using all but the first observation
fit <- glm(Direction ~ Lag1 + Lag2, data = Weekly[-1, ], family = "binomial")
  1. 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?
# Predict direction for the first observation
prediction <- predict(fit, newdata = Weekly[1, ], type = "response") > 0.5
prediction
##    1 
## TRUE
  1. Write a for loop from i =1to 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.
# Initialize error vector
n <- nrow(Weekly)
errors <- rep(NA, n)

# Perform LOOCV using a for loop
for (i in 1:n) {
  fit <- glm(Direction ~ Lag1 + Lag2, data = Weekly[-i, ], family = "binomial")
  prob <- predict(fit, newdata = Weekly[i, ], type = "response")
  pred <- ifelse(prob > 0.5, "Up", "Down")
  errors[i] <- (pred != Weekly$Direction[i])
}

# Compute LOOCV test error
loocv_error <- mean(errors)
loocv_error
## [1] 0.4499541
  1. 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.

The LOOCV test error is approximately 45%, indicating that the model’s predictions are only marginally better than random guessing.


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

  1. Based on this data set, provide an estimate for the population mean of medv. Call this estimate ˆ µ.
# Load Boston dataset and estimate population mean
mu_hat <- mean(Boston$medv)
mu_hat
## [1] 22.53281
  1. Provide an estimate of the standard error of ˆ µ. Interpret this result. Hint: We can compute the standard error of the sample mean by dividing the sample standard deviation by the square root of the number of observations.
# Standard error of the mean
se_mu <- sd(Boston$medv) / sqrt(length(Boston$medv))
se_mu
## [1] 0.4088611
  1. Now estimate the standard error of ˆ µ using the bootstrap. How does this compare to your answer from (b)?
# Bootstrap to estimate standard error
set.seed(42)
boot_mean <- boot(Boston$medv, function(data, index) mean(data[index]), R = 1000)
boot_mean
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = Boston$medv, statistic = function(data, index) mean(data[index]), 
##     R = 1000)
## 
## 
## Bootstrap Statistics :
##     original     bias    std. error
## t1* 22.53281 0.02671186   0.4009216
# Standard error from bootstrap
boot_se <- sd(boot_mean$t)
boot_se
## [1] 0.4009216

The standard error obtained using the bootstrap is close to the one calculated manually, confirming the validity of both methods.

  1. 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). Hint: You can approximate a 95% confidence interval using the formula [ˆ µ− 2SE(ˆ µ), ˆ µ + 2SE(ˆ µ)].

Using the bootstrap standard error obtained in part (c), we can compute a 95% confidence interval for the population mean of medv as:

# Bootstrap standard error for the mean
se_boot <- sd(boot_mean$t)

# 95% confidence interval
ci_bootstrap <- c(mu_hat - 2 * se_boot, mu_hat + 2 * se_boot)
ci_bootstrap
## [1] 21.73096 23.33465

To compare with the results obtained using t.test():

# Using t.test to get 95% confidence interval
ci_ttest <- t.test(Boston$medv)$conf.int
ci_ttest
## [1] 21.72953 23.33608
## attr(,"conf.level")
## [1] 0.95

The confidence intervals obtained using both methods should be close, but the bootstrap method might slightly differ due to random sampling. Both methods should provide a reasonable range for the population mean.

  1. Based on this data set, provide an estimate, ˆ µmed, for the median value of medv in the population.
# Estimate for the population median of medv
mu_med <- median(Boston$medv)
mu_med
## [1] 21.2
  1. Wenowwouldlike 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.
# Bootstrap to estimate the standard error of the median
set.seed(42)
boot_median <- boot(Boston$medv, function(v, i) median(v[i]), 10000)

# Standard error of the median
se_median <- sd(boot_median$t)
se_median
## [1] 0.3744634

The standard error of the median is typically lower than the standard error of the mean. In this case, the standard error of the median is approximately 0.374, which indicates a smaller variability for the median than for the mean.

  1. Based on this data set, provide an estimate for the tenth percentile of medv in Boston census tracts. Call this quantity ˆ µ0.1. (You can use the quantile() function.)
# Estimate for the 10th percentile of medv
mu_0_1 <- quantile(Boston$medv, 0.1)
mu_0_1
##   10% 
## 12.75
  1. Use the bootstrap to estimate the standard error of ˆ µ0.1. Comment on your findings
# Bootstrap to estimate the standard error of the 10th percentile
set.seed(42)
boot_percentile <- boot(Boston$medv, function(v, i) quantile(v[i], 0.1), 10000)

# Standard error of the 10th percentile
se_percentile <- sd(boot_percentile$t)
se_percentile
## [1] 0.497298

The standard error of the 10th percentile is higher than that of the median. In this case, it is approximately 0.5, which suggests that there is more variability in the lower tail of the medv distribution.