We now review k-fold cross-validation.
(a) Explain how k-fold cross-validation is implemented.
K-fold cross validation is implemented by first dividing the the observations into k groups, otherwise known as folds, of equal size. The first fold is used as the validation set and the remaining folds minus 1 (k-1) are used to fit the model. From the observations in the withheld fold, the mean squared error (MSE) is computed. This process is then repeated k times using a different fold as the validation set each iteration. This produces k estimates of the test error and the k-fold CV estimate is computed by averaging these values.
(b) What are the advantages and disadvantages of k-fold crossvalidation relative to:
i. The validation set approach?
The main advantage of the validation set approach is that it is simple and easy to implement.
The disadvantage is that the MSE can be highly variable and only a subset portion of the data is being used to fit the model so there is potential for inconsistency.
ii. LOOCV?
The advantage of the LOOCV is that bias is minimized by splitting the data randomly. Results of the LOOCV will be more consistent as the data are being split based on one observation each iteration.
The disadvantage of LOOCV is that it requires much more exhaustive computational processes.
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.
(a) Fit a logistic regression model that uses
income and balance to predict
default.
attach(Default)
log_default <- glm(default ~ balance + income, data = Default, family = binomial)
summary(log_default)
##
## Call:
## glm(formula = default ~ balance + income, 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 ***
## balance 5.647e-03 2.274e-04 24.836 < 2e-16 ***
## income 2.081e-05 4.985e-06 4.174 2.99e-05 ***
## ---
## 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) Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:
i. Split the sample set into a training set and a validation set.
dftrain = sample(dim(Default)[1], dim(Default)[1]*0.50)
dftest = Default[-dftrain, ]
ii. Fit a multiple logistic regression model using only the training observations.
dflog = glm(default ~ balance + income, data = Default, family = binomial, subset = dftrain)
summary(dflog)
##
## Call:
## glm(formula = default ~ balance + income, family = binomial,
## data = Default, subset = dftrain)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.1775 -0.1488 -0.0602 -0.0218 3.7176
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.137e+01 6.099e-01 -18.640 <2e-16 ***
## balance 5.572e-03 3.179e-04 17.529 <2e-16 ***
## income 1.784e-05 7.069e-06 2.524 0.0116 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1443.44 on 4999 degrees of freedom
## Residual deviance: 794.02 on 4997 degrees of freedom
## AIC: 800.02
##
## Number of Fisher Scoring iterations: 8
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.
log.prob_def = predict(dflog, dftest, type = "response")
log.pred_def = rep("No", dim(Default)[1]*0.50)
log.pred_def[log.prob_def > 0.5] = "Yes"
table(log.pred_def, dftest$default)
##
## log.pred_def No Yes
## No 4819 114
## Yes 12 55
iv. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
mean(log.pred_def !=dftest$default)
## [1] 0.0252
(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.
#Training split one: 60/40
dftrain = sample(dim(Default)[1], dim(Default)[1]*0.60)
dftest = Default[-dftrain, ]
dflog = glm(default ~ balance + income, data = Default, family = binomial, subset = dftrain)
summary(dflog)
##
## Call:
## glm(formula = default ~ balance + income, family = binomial,
## data = Default, subset = dftrain)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.7672 -0.1383 -0.0530 -0.0182 3.7786
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.191e+01 5.799e-01 -20.537 < 2e-16 ***
## balance 5.898e-03 3.062e-04 19.260 < 2e-16 ***
## income 2.049e-05 6.512e-06 3.146 0.00166 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1767.19 on 5999 degrees of freedom
## Residual deviance: 933.73 on 5997 degrees of freedom
## AIC: 939.73
##
## Number of Fisher Scoring iterations: 8
log.prob_def = predict(dflog, dftest, type = "response")
log.pred_def = rep("No", dim(Default)[1]*0.40)
log.pred_def[log.prob_def > 0.5] = "Yes"
table(log.pred_def, dftest$default)
##
## log.pred_def No Yes
## No 3848 88
## Yes 21 43
mean(log.pred_def !=dftest$default)
## [1] 0.02725
#Training split 2: 70/30
dftrain = sample(dim(Default)[1], dim(Default)[1]*0.70)
dftest = Default[-dftrain, ]
dflog = glm(default ~ balance + income, data = Default, family = binomial, subset = dftrain)
summary(dflog)
##
## Call:
## glm(formula = default ~ balance + income, family = binomial,
## data = Default, subset = dftrain)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4173 -0.1447 -0.0602 -0.0226 3.6981
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.136e+01 5.157e-01 -22.029 < 2e-16 ***
## balance 5.497e-03 2.675e-04 20.548 < 2e-16 ***
## income 2.154e-05 6.109e-06 3.526 0.000423 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1962.3 on 6999 degrees of freedom
## Residual deviance: 1086.8 on 6997 degrees of freedom
## AIC: 1092.8
##
## Number of Fisher Scoring iterations: 8
log.prob_def = predict(dflog, dftest, type = "response")
log.pred_def = rep("No", dim(Default)[1]*0.30)
log.pred_def[log.prob_def > 0.5] = "Yes"
table(log.pred_def, dftest$default)
##
## log.pred_def No Yes
## No 2879 80
## Yes 9 32
mean(log.pred_def !=dftest$default)
## [1] 0.02966667
#Training split 3: 80/20
dftrain = sample(dim(Default)[1], dim(Default)[1]*0.80)
dftest = Default[-dftrain, ]
dflog = glm(default ~ balance + income, data = Default, family = binomial, subset = dftrain)
summary(dflog)
##
## Call:
## glm(formula = default ~ balance + income, family = binomial,
## data = Default, subset = dftrain)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.5221 -0.1404 -0.0548 -0.0200 3.7493
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.181e+01 4.920e-01 -23.997 < 2e-16 ***
## balance 5.741e-03 2.553e-04 22.488 < 2e-16 ***
## income 2.421e-05 5.607e-06 4.318 1.58e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2380.8 on 7999 degrees of freedom
## Residual deviance: 1250.0 on 7997 degrees of freedom
## AIC: 1256
##
## Number of Fisher Scoring iterations: 8
log.prob_def = predict(dflog, dftest, type = "response")
log.pred_def = rep("No", dim(Default)[1]*0.20)
log.pred_def[log.prob_def > 0.5] = "Yes"
table(log.pred_def, dftest$default)
##
## log.pred_def No Yes
## No 1937 45
## Yes 3 15
mean(log.pred_def !=dftest$default)
## [1] 0.024
Changing the splits on the training set and validation set did not have much of an impact on the validation set error. The original error was 0.0262, when changing the splits to 60/40, 70/30, and 80/20, the error rates were 0.0255, 0.026, and 0.026, respectively.
(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.
dftrain = sample(dim(Default)[1], dim(Default)[1]*0.50)
dftest = Default[-dftrain, ]
dflog = glm(default ~ balance + income + student, data = Default, family = binomial, subset = dftrain)
summary(dflog)
##
## Call:
## glm(formula = default ~ balance + income + student, family = binomial,
## data = Default, subset = dftrain)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.0449 -0.1519 -0.0621 -0.0233 3.6071
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.042e+01 6.629e-01 -15.725 <2e-16 ***
## balance 5.458e-03 3.076e-04 17.742 <2e-16 ***
## income 3.380e-06 1.155e-05 0.293 0.7698
## studentYes -6.783e-01 3.332e-01 -2.036 0.0417 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1497.19 on 4999 degrees of freedom
## Residual deviance: 825.61 on 4996 degrees of freedom
## AIC: 833.61
##
## Number of Fisher Scoring iterations: 8
log.prob_def = predict(dflog, dftest, type = "response")
log.pred_def = rep("No", dim(Default)[1]*0.50)
log.pred_def[log.prob_def > 0.5] = "Yes"
table(log.pred_def, dftest$default)
##
## log.pred_def No Yes
## No 4826 115
## Yes 13 46
mean(log.pred_def !=dftest$default)
## [1] 0.0256
Including the dummy variable student produced the same
test error rate of 0.026 as using the training splits of 70/30 and
80/20. So, there was no reduction in error rate.
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.
set.seed(1)
fit.model <- glm(default ~ balance + income, data = Default, family = "binomial")
summary(fit.model)
##
## Call:
## glm(formula = default ~ balance + income, 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 ***
## balance 5.647e-03 2.274e-04 24.836 < 2e-16 ***
## income 2.081e-05 4.985e-06 4.174 2.99e-05 ***
## ---
## 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
The estimates for the standard error are 4.348e-01 for the intercept,
2.274e-04 for balance and 4.985e-06 for
income.
(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)
{fit <- glm(default ~ income + balance, data = data, family = "binomial", subset = index)
return (coef(fit))}
(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.
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.945460e-02 4.344722e-01
## t2* 2.080898e-05 1.680317e-07 4.866284e-06
## t3* 5.647103e-03 1.855765e-05 2.298949e-04
(d) Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.
The estimated standard errors obtained using the glm() function were
very close to the estimated standard errors obtained using the bootstrap
function. The estimates for the standard error using glm() are 4.348e-01
for the intercept, 2.274e-04 for balance and 4.985e-06 for
income. The estimates for the standard error using the
bootstrap function are 4.493911e-01, 2.362239e-04, and 4.767307e-06.
We will now consider the Boston housing data set, from the ISLR2 library.
(a) Based on this data set, provide an estimate for the population mean of medv. Call this estimate ˆµ.
detach(Default)
attach(Boston)
mu <- mean(medv)
mu
## [1] 22.53281
(b) 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.
se <- sd(medv) / sqrt(dim(Boston)[1])
se
## [1] 0.4088611
(c) Now estimate the standard error of ˆµ using the bootstrap. How does this compare to your answer from (b)?
set.seed(1)
boot.fn <- function(data, index) {
mu <- mean(data[index])
return (mu)
}
boot(medv, boot.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 0.007650791 0.4106622
The estimated standard error from part (b) is 0.4088611 and the estimated standard error using the bootstrap function is 0.4106622. The bootstrap function does an excellent job of estimating the standard error.
(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). Hint: You can approximate a 95 % confidence interval using the formula [ˆµ − 2SE(ˆµ), µˆ + 2SE(ˆµ)].
t.test(medv)
##
## One Sample t-test
##
## data: 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
ci.mu <- c(22.53 - 2 * 0.4106622, 22.53 + 2 * 0.4106622)
ci.mu
## [1] 21.70868 23.35132
From the t test the 95% confidence interval was calculated at 21.72953 to 23.33608. The bootstrap confidence interval was calculated at 21.70868 to 23.35132, giving it very similar values to the t test.
(e) Based on this data set, provide an estimate, ˆµmed, for the median value of medv in the population.
mumed <- median(medv)
mumed
## [1] 21.2
(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 <- function(data, index)
{ mu <- median(data[index])
return (mu)}
boot(medv, boot.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 -0.0386 0.3770241
In part (e) the median value was calculated at 21.2, which is the same value returned from the bootstrap function. The bootstrap function provides an estimated error of 0.3770241.
(g) 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.)
muhat0.1 <- quantile(medv, c(0.1))
muhat0.1
## 10%
## 12.75
(h) Use the bootstrap to estimate the standard error of ˆµ0.1. Comment on your findings.
boot.fn <- function(data, index)
{mu <- quantile(data[index], c(0.1))
return (mu)}
boot(medv, boot.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 12.75 0.0186 0.4925766
The estimated tenth percentile value using the bootstrap function is 12.75, which is the same value obtained in part (g). The standard error estimate provided by the bootstrap function is 0.4925766.