3.) We now review k-fold cross-validation.
The k-fold cross validation is implemented by taking the number of observations (n) and randomly dividing them into k groups of roughly equal size. These groups act as a validation set, and the remainder acts as a training set. The test error is then estimated by averaging the k resulting MSE estimates.
An advantage k-fold cross validation has over the validation set is the validation estimate of the test error rate can be highly variable, depending on precisely which observations are included in the training set and which observations are included in the validation set. And the validation set error rate may tend to overestimate the test error rate for the model fit on the entire data set.
A disadvantage of k-fold cross validation has compared to the validation set is the validation set approach is much simpler and easier to implement.
One advantage the k-fold cross validation has over LOOCV is LOOCV requires fitting the statistical learning method n times. This has the potential to complicate models when n reaches very high numbers. Also, k-fold cross validation often gives more accurate estimates of the test error rate than LOOCV.
A disadvantage that k-fold cross validation has to LOOCV is LOOCV tends to have less bias than k-fold cross validation.
There is a bias-variance trade-off associated with the choice of k in k-fold cross-validation. Typically using k=5 or k=10 results in test error rate estimates that don’t suffer from excessively high bias or from high variance.
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(ISLR)
## Warning: package 'ISLR' was built under R version 4.1.3
Default = Default
set.seed(20)
fit.def = glm(default ~ income + balance, data = Default, family = "binomial")
summary(fit.def)
##
## 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
Split the sample set into a training set and a validation set.
Fit a multiple logistic regression model using only the training observations.
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.
Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
#i
def.train = sample(dim(Default)[1], dim(Default)[1] / 2)
#ii
fit.def.train = glm(default ~ income + balance, data = Default, family = "binomial", subset = def.train)
summary(fit.def.train)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = def.train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.6003 -0.1285 -0.0481 -0.0170 3.7909
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.222e+01 6.764e-01 -18.074 < 2e-16 ***
## income 2.440e-05 7.382e-06 3.305 0.00095 ***
## balance 5.999e-03 3.537e-04 16.958 < 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: 1382.01 on 4999 degrees of freedom
## Residual deviance: 716.55 on 4997 degrees of freedom
## AIC: 722.55
##
## Number of Fisher Scoring iterations: 8
#iii
probs = predict(fit.def, newdata = Default[-def.train, ], type = "response")
pred.glm = rep("No", length(probs))
pred.glm[probs > 0.5] = "Yes"
#iv
mean(pred.glm != Default[-def.train, ]$default)
## [1] 0.0288
#2.86% test error rate with validation set approach.
train = sample(dim(Default)[1], dim(Default)[1] / 2)
fit.glm = glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
probs = predict(fit.glm, newdata = Default[-train, ], type = "response")
pred.glm = rep("No", length(probs))
pred.glm[probs > 0.5] = "Yes"
mean(pred.glm != Default[-train, ]$default)
## [1] 0.0256
train = sample(dim(Default)[1], dim(Default)[1] / 3)
fit.glm = glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
probs = predict(fit.glm, newdata = Default[-train, ], type = "response")
pred.glm = rep("No", length(probs))
pred.glm[probs > 0.5] = "Yes"
mean(pred.glm != Default[-train, ]$default)
## [1] 0.02684866
train = sample(dim(Default)[1], dim(Default)[1] / 4)
fit.glm = glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
probs = predict(fit.glm, newdata = Default[-train, ], type = "response")
pred.glm = rep("No", length(probs))
pred.glm[probs > 0.5] = "Yes"
mean(pred.glm != Default[-train, ]$default)
## [1] 0.02733333
The validation estimate of the test error rate can be variable, depending on the split determining which observations are included in the training set and which observations are included in the validation set.
set.seed(6)
subset = sample(nrow(Default), nrow(Default)*0.7)
default.train = Default[subset,]
default.test = Default[-subset,]
dum = glm(default ~ income + balance + student, family = binomial, data=default.train)
summary(dum)
##
## Call:
## glm(formula = default ~ income + balance + student, family = binomial,
## data = default.train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.2048 -0.1318 -0.0503 -0.0179 3.7413
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.125e+01 6.069e-01 -18.533 <2e-16 ***
## income 2.394e-06 9.888e-06 0.242 0.8087
## balance 5.976e-03 2.886e-04 20.709 <2e-16 ***
## studentYes -7.037e-01 2.841e-01 -2.477 0.0133 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2064.0 on 6999 degrees of freedom
## Residual deviance: 1067.9 on 6996 degrees of freedom
## AIC: 1075.9
##
## Number of Fisher Scoring iterations: 8
predict.dum = predict(dum, default.test, type="response")
class.dum = ifelse(predict.dum>0.7,"Yes","No")
table(default.test$default, class.dum, dnn=c("Actual","Predicted"))
## Predicted
## Actual No Yes
## No 2900 3
## Yes 83 14
round(mean(class.dum!=default.test$default),4)
## [1] 0.0287
Adding the “student” dummy variable doesn’t lead to a reduction in the validation set estimate of the test error rate.
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.
set.seed(32)
attach(Default)
fit.glm = glm(default ~ income + balance, data = Default, family = "binomial")
summary(fit.glm)
##
## 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
SE estimates for coefficients are respectively 0.4348, 4.985e-6, and 2.274e-04.
boot.fn = function(data, index)
{fit = glm(default ~ income + balance, data = data, family = "binomial", subset = index)
return (coef(fit))}
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 -2.093229e-02 4.157888e-01
## t2* 2.080898e-05 7.080066e-08 4.760232e-06
## t3* 5.647103e-03 9.980680e-06 2.161297e-04
SE estimates for coefficients are respectively 0.4158, 4.760e-06, and 2.161e-04.
Estimated standard errors obtained by the two methods are very similar.
9.) We will now consider the Boston housing data set, from the ISLR2 library.
library(ISLR2)
## Warning: package 'ISLR2' was built under R version 4.1.3
##
## Attaching package: 'ISLR2'
## The following objects are masked from 'package:ISLR':
##
## Auto, Credit
attach(Boston)
mu.hat = mean(medv)
mu.hat
## [1] 22.53281
se.hat = sd(medv) / sqrt(dim(Boston)[1])
se.hat
## [1] 0.4088611
set.seed(999)
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.008379249 0.4101731
Bootstrap estimated standard error of μˆ of 0.4101 is very close to the estimate found in (b) of 0.4089.
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.hat = c(22.53 - 2 * 0.4101, 22.53 + 2 * 0.4101)
CI.mu.hat
## [1] 21.7098 23.3502
Bootstrap confidence interval is very close to the one provided by the t.test() function.
med.hat = median(medv)
med.hat
## [1] 21.2
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.0015 0.3677608
Estimated median value of 21.2 which is equal to the value obtained in (e), with a standard error of 0.3678 which is relatively small compared to median value.
perc.hat = quantile(medv, c(0.1))
perc.hat
## 10%
## 12.75
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.00335 0.511486
Estimated tenth percentile value of 12.75 which is again equal to the value obtained in (g), with a standard error of 0.5115 which is relatively small compared to percentile value.