Question 3

We now review k-fold cross-validation.

  1. Explain how k-fold cross-validation is implemented. It involves randomly k-fold CV dividing the set of observations into k groups, or folds, of approximately equal size. The first fold is treated as a validation set, and the method is fit on the remaining k − 1 folds. The mean squared error, MSE1, is then computed on the observations in the held-out fold. This procedure is repeated k times; each time, a different group of observations is treated as a validation set.

  2. What are the advantages and disadvantages of k-fold crossvalidation relative to:

  1. The validation set approach? The advantage of the k-fold cv to the validation set approach is that the validation set approach often overestimates the test error rate. The disadvantages of the k-fold cv to the validation set approach is that validation set approach is much simpler.

  2. LOOCV? The advantages of k-fold CV to the LOOCV is that it reults in less computational problems. Also, often gives more accurate estimates of the test error rate rather than LOOCV. The disadvantage of the k-fold to the LOOCV is that the k-fold is more random.

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

library(ISLR2)
data("Default")
  1. Fit a logistic regression model that uses income and balance to predict default.
logreg= glm(default~income+balance, data=Default, family="binomial")
summary(logreg)
## 
## Call:
## glm(formula = default ~ income + balance, family = "binomial", 
##     data = Default)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.4725  -0.1444  -0.0574  -0.0211   3.7245  
## 
## 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.
set.seed(1)
train= sample(10000, 5000)
  1. Fit a multiple logistic regression model using only the training observations.
logreg2= glm(default~income+balance, data= Default, subset=train, family="binomial")
summary(logreg2)
## 
## Call:
## glm(formula = default ~ income + balance, family = "binomial", 
##     data = Default, subset = train)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.5830  -0.1428  -0.0573  -0.0213   3.3395  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -1.194e+01  6.178e-01 -19.333  < 2e-16 ***
## income       3.262e-05  7.024e-06   4.644 3.41e-06 ***
## balance      5.689e-03  3.158e-04  18.014  < 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: 1523.8  on 4999  degrees of freedom
## Residual deviance:  803.3  on 4997  degrees of freedom
## AIC: 809.3
## 
## Number of Fisher Scoring iterations: 8
  1. 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.
prob <- predict(logreg2, Default[-train, ], type = "response")
pred <- rep("No", length(prob))
pred[prob > 0.5] <- "Yes"
  1. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
mean(pred != Default[-train, ]$default)
## [1] 0.0254

2.54% of the observations are missclassified from the validation set.

  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.
train=sample(10000, 7500)
prob <- predict(logreg2, Default[-train, ], type = "response")
pred <- rep("No", length(prob))
pred[prob > 0.5] <- "Yes"
mean(pred != Default[-train, ]$default)
## [1] 0.0256
train=sample(10000, 7500)
prob <- predict(logreg2, Default[-train, ], type = "response")
pred <- rep("No", length(prob))
pred[prob > 0.5] <- "Yes"
mean(pred != Default[-train, ]$default)
## [1] 0.0252
train=sample(10000, 7500)
prob <- predict(logreg2, Default[-train, ], type = "response")
pred <- rep("No", length(prob))
pred[prob > 0.5] <- "Yes"
mean(pred != Default[-train, ]$default)
## [1] 0.0288

Their is some variation between the 3 different splits.

  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 validation set approach. Comment on whether or not including a dummy variable for student leads to a reduction in the test error rate.
train=sample(10000, 7500)
logreg3= glm(default~income+balance+student, data= Default, subset=train, family="binomial")
prob <- predict(logreg2, Default[-train, ], type = "response")
pred <- rep("No", length(prob))
pred[prob > 0.5] <- "Yes"
mean(pred != Default[-train, ]$default)
## [1] 0.0256

Adding the dummy variable did not reduce the test error rate.

Question 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.
logreg3= glm(default~income+balance, data=Default, family="binomial")
summary(logreg3)$coefficients[ ,2]
##  (Intercept)       income      balance 
## 4.347564e-01 4.985167e-06 2.273731e-04
  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=1:nrow(data)) {
    logreg4 <- glm(default ~ income + balance, data = data, family = "binomial", subset = index)
    return (coef(logreg4))
}
  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)
boot(Default, boot.fn, 1000)
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = Default, statistic = boot.fn, R = 1000)
## 
## 
## Bootstrap Statistics :
##          original        bias     std. error
## t1* -1.154047e+01 -3.956706e-02 4.358002e-01
## t2*  2.080898e-05  1.667950e-07 4.855307e-06
## t3*  5.647103e-03  1.860487e-05 2.306029e-04
  1. Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.

The bootstrap estimated standard errors are 4.809310^(-6) and 2.328810^(-4).

Question 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 ˆµ.
mean(Boston$medv)
## [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.
sd(Boston$medv)/sqrt(length(Boston$medv))
## [1] 0.4088611
  1. Now estimate the standard error of ˆµ using the bootstrap. How does this compare to your answer from (b)?
boot.fn=function(data, index){
  mean(data[index])
}
b=boot(Boston$medv, boot.fn, 1000)
b
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = Boston$medv, statistic = boot.fn, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original     bias    std. error
## t1* 22.53281 0.01691976   0.4185628

The standard errors are similar off by .002.

  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(ˆµ)].
round(c(mean(Boston$medv) - 2*sd(b$t), mean(Boston$medv) + 2*sd(b$t)), 4)
## [1] 21.6957 23.3699
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

They are similar off by .01.

  1. Based on this data set, provide an estimate, ˆµmed, for the median value of medv in the population.
median(Boston$medv)
## [1] 21.2
  1. 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=function(data, index){
  median(data[index])
}
boot(Boston$medv, boot.fn, 1000)
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = Boston$medv, statistic = boot.fn, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original  bias    std. error
## t1*     21.2 -0.0256   0.3687351

The standard error is .38828355.

  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.)
quantile(Boston$medv, .1)
##   10% 
## 12.75
  1. Use the bootstrap to estimate the standard error of ˆµ0.1. Comment on your findings.
boot.fn=function(data, index){
  quantile(data[index], .1)
}
boot(Boston$medv, boot.fn, 1000)
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = Boston$medv, statistic = boot.fn, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original  bias    std. error
## t1*    12.75  0.0378   0.4903944

The standard error is slightly high at .5031813.