We now review k-fold cross-validation.
(a) Explain how k-fold cross-validation is implemented.
k-Fold CV randomly divides a set of observations into equally sized k folds. Each group is in turn used as a validation set while the remaining folds act as training sets. The MSE from these k folds train/test are used to compute the average test error rate.
(b) What are the advantages and disadvantages of k-fold crossvalidation relative to:
i. The validation set approach?
The validation ser approach is easy to compute but can have high variability based upon selected observations and is likely to “overestimate” the test error rate on the entire data set.
ii. LOOCV?
Leave-One-Out Cross-Validation has less bias than the validation set approach because it uses n-1 to select the observations vs cutting the data in half. Another benefit of LOOCV’s use of all observations is that it always generates the same results, unlike the randomness used in the validation set approach.
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(ISLR)
summary(Default)
## default student balance income
## No :9667 No :7056 Min. : 0.0 Min. : 772
## Yes: 333 Yes:2944 1st Qu.: 481.7 1st Qu.:21340
## Median : 823.6 Median :34553
## Mean : 835.4 Mean :33517
## 3rd Qu.:1166.3 3rd Qu.:43808
## Max. :2654.3 Max. :73554
attach(Default)
dim(Default)
## [1] 10000 4
(a) Fit a logistic regression model that uses income and balance top redict default.
set.seed(42)
fit_glm_1 <- glm(default ~ income + balance, data = Default, family = binomial)
(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. ii. Fit a multiple logistic regression model using only the training observations. 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. iv. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
VSA.1 = function() {
train <- sample(dim(Default)[1], dim(Default)[1]*.5)
fit.glm.VSA <- glm(default ~ income + balance, data = Default, family = binomial, subset = train)
pred.glm.VSA <- rep("No", dim(Default)[1]/2)
probs.glm.VSA <- predict(fit.glm.VSA, Default[-train, ], type = "response")
pred.glm.VSA[probs.glm.VSA > 0.5] = "Yes"
return(mean(pred.glm.VSA != Default[-train, ]$default))
}
VSA.1()
## [1] 0.026
There is a 2.6% error rate for this validation set.
(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.
After running this several times, the average test error rate is ~2.65%.
vsa1.1 <- VSA.1()
vsa1.2 <- VSA.1()
vsa1.3 <- VSA.1()
vsa1.1
## [1] 0.026
vsa1.2
## [1] 0.0294
vsa1.3
## [1] 0.0258
c(mean(vsa1.1, vsa1.2, vsa1.3))
## [1] 0.026
(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.
Amending the function above with a dummy variable for student does not appear to improve the test error rate, even when we take the average of three splits.
VSA.2 = function() {
train <- sample(dim(Default)[1], dim(Default)[1]*.5)
fit.glm.VSA2 <- glm(default ~ income + balance + student, data = Default, family = binomial, subset = train)
pred.glm.VSA2 <- rep("No", dim(Default)[1]/2)
probs.glm.VSA2 <- predict(fit.glm.VSA2, Default[-train, ], type = "response")
pred.glm.VSA2[probs.glm.VSA2 > 0.5] = "Yes"
return(mean(pred.glm.VSA2 != Default[-train, ]$default))
}
VSA.2()
## [1] 0.0278
vsa2.1 <- VSA.2()
vsa2.2 <- VSA.2()
vsa2.3 <- VSA.2()
vsa2.1
## [1] 0.0256
vsa2.2
## [1] 0.025
vsa2.3
## [1] 0.0272
(vsa2.1 + vsa2.2 + vsa2.3)/3
## [1] 0.02593333
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.
library(boot)
## Warning: package 'boot' was built under R version 3.6.3
set.seed(42)
fit.glm.VSA.6a <- glm(default ~ income + balance, data = Default, family = binomial)
summary(fit.glm.VSA.6a)
##
## 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) 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) return(coef(glm(default ~ income + balance, data = data, family = binomial, subset = index)))
(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, 100)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Default, statistic = boot.fn, R = 100)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* -1.154047e+01 8.325503e-04 4.106915e-01
## t2* 2.080898e-05 -3.709342e-07 5.320364e-06
## t3* 5.647103e-03 6.482769e-06 2.003151e-04
(d) Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.
The standard errors are difference for ˆ β0, ˆ β1, and ˆ β2, which is a good thing. GLM depends upon the noise variance and a fixed xi. To quote the ISLR book, “The bootstrap approach does not rely on any of these assumptions, and so it is likely giving a more accurate estimate of the standard errors”.
detach(Default)
We will now consider the Boston housing data set, from the MASS library.
library(MASS)
## Warning: package 'MASS' was built under R version 3.6.3
attach(Boston)
set.seed(42)
names(Boston)
## [1] "crim" "zn" "indus" "chas" "nox" "rm" "age"
## [8] "dis" "rad" "tax" "ptratio" "black" "lstat" "medv"
(a) Based on this data set, provide an estimate for the population mean of medv. Call this estimate ˆμ.
mean.medv <- mean(medv)
mean.medv
## [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.
error.medv <- sd(medv)/sqrt(length(medv))
error.medv
## [1] 0.4088611
(c) Now estimate the standard error of ˆμ using the bootstrap. How does this compare to your answer from (b)?
library(boot)
boot.fn <- function(data, index) return(mean(data[index]))
boot.strap.1 <- boot(medv, boot.fn, 1000)
boot.strap.1
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 0.02671186 0.4009216
The estimates are comparable to the hundredths, ~.409 for (b) and ~.401 for (c).
(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
c(boot.strap.1$t0 - 2 * 0.4008319, boot.strap.1$t0 + 2 * 0.4008319)
## [1] 21.73114 23.33447
The t test estimate falls close to the middle of the bootstrap’s 95% confidence interval.
(e) Based on this data set, provide an estimate, ˆμmed, for the median value of medv in the population.
median.medv <- median(medv)
median.medv
## [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.med <- function(data, index) return(median(data[index]))
boot.strap.2 <- boot(medv, boot.fn.med, 1000)
boot.strap.2
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn.med, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 -0.0129 0.3766902
The standard error, in relation to the median, is low. The median is 21.2 and a standard error is ~.377.
(g) Based on this data set, provide an estimate for the tenth percentile of medv in Boston suburbs. Call this quantity ˆμ0.1. (You can use the quantile() function.)
medv.10th <- quantile(medv,c(0.1))
medv.10th
## 10%
## 12.75
(h) Use the bootstrap to estimate the standard error of ˆμ0.1. Comment on your findings.
boot.fn.10th <- function(data, index) return(quantile(data[index], c(0.1)))
boot.strap.3 <- boot(medv, boot.fn.10th, 1000)
boot.strap.3
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn.10th, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 12.75 0.0166 0.5011942
The standard error for the tenth-percentile is ~.501. While larger than the SE relationship for the median, it is still a relatively small error.