First, the set of observations is randomly divided into k groups, or folds, of approximately equal size. From these, The first fold is treated as a validation set, and the method is fit on the remaining k − 1 folds. The mean squared error is then computed on the observations in the held-out fold. This procedure is repeated k times, with a different group of observations is treated as a validation set for each iteration. As a result, we are left with k estimates of the test error; these values are then averaged to arrive at the k-fold CV estimate.
(b.i) What are the advantages and disadvantages of k-fold cross-validation relative to the validation set approach?
Though he validation set approach is conceptually simple and easy to implement, it has a disadvantage of leading to overestimates of the test error rate, since in this approach the training set used to fit the statistical learning method contains only half the observations of the entire data set. Additionally, its estimate of the test error rate can be highly variable.
(b.ii) What are the advantages and disadvantages of k-fold cross-validation relative to LOOCV?
A disadvantage of LOOCV is that it requires fitting the statistical learning method n times which has the potential to be computationally expensive. In addition, since the mean of many highly correlated quantities has higher variance than does the mean of many quantities that are not as highly correlated, the test error estimate resulting from LOOCV tends to have higher variance than does the test error estimate resulting from k-fold CV. This is because When performing LOOCV, we are essentially averaging the outputs of n fitted models that are trained on an almost identical set of observations. As a result, these outputs are highly correlated with each other.
Alternatively to the validation set approach, LOOCV will give approximately unbiased estimates of the test error, since each training set contains n − 1 observations, which is almost as many as the number of observations in the full data set. Therefore, from the perspective of bias reduction, LOOCV is to be preferred to k-fold CV.
Ultimately, there is a bias-variance trade-off associated with the choice of k in k-fold cross-validation. In turn, k-fold cross-validation using k = 5 or k = 10 is typically performed, as these values have been shown empirically to yield test error rate estimates that suffer neither from excessively high bias nor from very high variance.
library(ISLR)
set.seed(4)
fit_g1 <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(fit_g1)
##
## 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
(b.i) Split the sample set into a training set and a validation set.
set.seed(1)
training_data <- sample(nrow(Default), nrow(Default) / 2)
(b.ii) Fit a multiple logistic regression model using only the training observations.
fit_g2 <- glm(default ~ income + balance, data = Default, family = "binomial", subset = training_data)
summary(fit_g2)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = training_data)
##
## 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
(b.iii) 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.
post_prob <- predict(fit_g2, newdata = Default[-training_data,], type = "response")
g2_prediction <- rep("No", length(post_prob))
g2_prediction[post_prob > 0.5] <- "Yes"
(b.iv) Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
As shown below, the resultant validation set error is 2.54%.
mean(g2_prediction != Default[-training_data,]$default)
## [1] 0.0254
Comments: as mentioned within the textbook, the variability of the validation set error associated with this approach can be observed. This variability results from the changes of which observations are included in the training and validation set during each iteration of the below for loop.
for(i in 1:3){
set.seed(i)
tmp_training_data <- sample(nrow(Default), nrow(Default) / 2)
tmp_fit <- glm(default ~ income + balance, data = Default, family = "binomial", subset = tmp_training_data)
tmp_post_prob <- predict(tmp_fit, newdata = Default[-tmp_training_data,], type = "response")
tmp_prediction <- rep("No", length(tmp_post_prob))
tmp_prediction[tmp_post_prob > 0.5] <- "Yes"
print(mean(tmp_prediction != Default[-tmp_training_data,]$default))
}
## [1] 0.0254
## [1] 0.0238
## [1] 0.0264
Comment: as shown below, including a dummy variable for student does not appear to lead to a reduction in the test error rate. In fact, when the following code was ran for seeds 1-3, the test error rate in each case increased.
set.seed(3)
tmp_training_data <- sample(nrow(Default), nrow(Default) / 2)
tmp_fit <- glm(default ~ income + balance + student, data = Default, family = "binomial", subset = tmp_training_data)
tmp_post_prob <- predict(tmp_fit, newdata = Default[-tmp_training_data,], type = "response")
tmp_prediction <- rep("No", length(tmp_post_prob))
tmp_prediction[tmp_post_prob > 0.5] <- "Yes"
mean(tmp_prediction != Default[-tmp_training_data,]$default)
## [1] 0.0272
The estimated standard errors for the coefficients are shown below:
fit_q6a <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(fit_q6a)
##
## 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
boot.fn <- function(data, index) {
tmp_fit <- glm(default ~ income + balance, data = data, family = "binomial", subset = index)
return (coef(tmp_fit))
}
The outputted estimate of the standard errors of the logistic regression coefficients are shown below:
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 -4.138024e-02 4.381705e-01
## t2* 2.080898e-05 2.071209e-07 4.860557e-06
## t3* 5.647103e-03 1.975295e-05 2.310331e-04
As can be seen from the above two outputs, there was consistency between the estimated standard errors obtained from glm() and boot.fn(), and ultimately were similar.
library(MASS)
mu_hat = mean(Boston$medv)
mu_hat
## [1] 22.53281
Interpretation: the standard error of \(\hat\mu\) shown below is arrived at by dividing the sample standard deviation by the square root of the amount of observations.
mu_hat_err = sd(Boston$medv) / sqrt(nrow(Boston))
mu_hat_err
## [1] 0.4088611
Comment: as outputted below, the bootstrap estimated standard error of 0.4186383 follows suit with 0.4088611 outputted in part (b).
set.seed(4)
boot.fn <- function(data, index){
mean(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* 22.53281 -0.01136423 0.4186383
As subsequently portrayed below, the confidence interval given by t.test() is similar to that of the bootstrap estimate.
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
c_interval = c(22.53281 - (2 * 0.4186383), 22.53281 + (2 * 0.4186383))
c_interval
## [1] 21.69553 23.37009
mu_hat_med <- median(Boston$medv)
mu_hat_med
## [1] 21.2
Comments: the estimated median below matches that outputted in part (e) above. Further, we have a relatively small standard error with respect to the median value.
set.seed(4)
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.00455 0.3830777
mu_hat_tenth_perc <- quantile(Boston$medv, c(0.1))
mu_hat_tenth_perc
## 10%
## 12.75
Comment: as shown below, an estimated tenth percentile value of 12.75 was obtained, which matches that outputted in part (g). A corresponding standard error of 0.5048959 was given which was slightly higher than that associated with the mean and median values above, though still relatively reasonable.
set.seed(4)
boot.fn<-function(data, index){
quantile(data[index], c(0.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.005 0.5048959