We now review k-fold cross-validation.
(a) Explain how k-fold cross-validation is implemented.
(b) What are the advantages and disadvantages of k-fold cross validation relative to:
i. The validation set approach?
There is larger variability in MSE with validation set. k-fold can have slightly different CV error rates based on how many times it is ran but they are all pretty similar. Therefore k-fold appears to be more stable.
ii. LOOCV?
k-fold can have slightly different CV error rates based on how many times it is ran. LOOCV is a special case of k-fold, where k = n. They are both stable, but LOOCV is more computationally intensive.
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)
attach(Default)
set.seed(1)
(a) Fit a logistic regression model that uses income and balance to predict default.
glm.fitall = glm(default~income+balance, data = Default, family = binomial)
summary(glm.fitall)
##
## 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
glm.probs = predict(glm.fitall, type = 'response')
glm.pred = rep("No",10000)
glm.pred[glm.probs>0.5]="Yes"
table(glm.pred,default)
## default
## glm.pred No Yes
## No 9629 225
## Yes 38 108
mean(glm.pred!=default)
## [1] 0.0263
This model has a missclassification rate of 2.63%
(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.
50train/50test split
sample.observations = sample(nrow(Default),nrow(Default)*0.5)
train.50 = Default[sample.observations,]
test.50 = Default[-sample.observations,]
ii. Fit a multiple logistic regression model using only the training observations.
glm.fittrain50 = glm(default~income+balance, data = train.50, family = binomial)
summary(glm.fittrain50)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = train.50)
##
## 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
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.
glm.probstest50 = predict(glm.fittrain50, test.50, type = 'response')
glm.predtest50 = rep("No",5000)
glm.predtest50[glm.probstest50>0.5]="Yes"
table(glm.predtest50,test.50$default)
##
## glm.predtest50 No Yes
## No 4824 108
## Yes 19 49
iv. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
mean(glm.predtest50!=test.50$default)
## [1] 0.0254
This model has a miss-classification rate of 2.54%
(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.
25train/75test split
sample.observations2 = sample(nrow(Default),nrow(Default)*0.25)
train.25 = Default[sample.observations2,]
test.25 = Default[-sample.observations2,]
glm.fittrain25 = glm(default~income+balance, data = train.25, family = binomial)
summary(glm.fittrain25)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = train.25)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.6159 -0.1249 -0.0492 -0.0170 3.8022
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.227e+01 9.258e-01 -13.258 < 2e-16 ***
## income 2.781e-05 9.543e-06 2.914 0.00357 **
## balance 5.960e-03 4.843e-04 12.305 < 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: 721.72 on 2499 degrees of freedom
## Residual deviance: 379.12 on 2497 degrees of freedom
## AIC: 385.12
##
## Number of Fisher Scoring iterations: 8
glm.probstest25 = predict(glm.fittrain25, test.25, type = 'response')
glm.predtest25 = rep("No",7500)
glm.predtest25[glm.probstest25>0.5]="Yes"
table(glm.predtest25,test.25$default)
##
## glm.predtest25 No Yes
## No 7212 171
## Yes 37 80
mean(glm.predtest25!=test.25$default)
## [1] 0.02773333
This model has a 2.77% miss-classification rate.
60train/40test split
sample.observations3 = sample(nrow(Default),nrow(Default)*0.60)
train.60 = Default[sample.observations3,]
test.60 = Default[-sample.observations3,]
glm.fittrain60 = glm(default~income+balance, data = train.60, family = binomial)
summary(glm.fittrain60)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = train.60)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4945 -0.1271 -0.0488 -0.0173 3.8179
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.201e+01 5.998e-01 -20.019 < 2e-16 ***
## income 1.857e-05 6.695e-06 2.774 0.00554 **
## balance 5.914e-03 3.163e-04 18.700 < 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: 1665.29 on 5999 degrees of freedom
## Residual deviance: 862.49 on 5997 degrees of freedom
## AIC: 868.49
##
## Number of Fisher Scoring iterations: 8
glm.probstest60 = predict(glm.fittrain60, test.60, type = 'response')
glm.predtest60 = rep("No",4000)
glm.predtest60[glm.probstest60>0.5]="Yes"
table(glm.predtest60,test.60$default)
##
## glm.predtest60 No Yes
## No 3840 102
## Yes 14 44
mean(glm.predtest60!=test.60$default)
## [1] 0.029
This model has a 2.9% miss-classification rate.
90train/30test split
sample.observations4 = sample(nrow(Default),nrow(Default)*0.90)
train.90 = Default[sample.observations4,]
test.90 = Default[-sample.observations4,]
glm.fittrain90 = glm(default~income+balance, data = train.90, family = binomial)
summary(glm.fittrain90)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = train.90)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.5146 -0.1434 -0.0555 -0.0198 3.7434
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.165e+01 4.598e-01 -25.342 < 2e-16 ***
## income 1.941e-05 5.207e-06 3.728 0.000193 ***
## balance 5.769e-03 2.422e-04 23.819 < 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: 2704.3 on 8999 degrees of freedom
## Residual deviance: 1435.6 on 8997 degrees of freedom
## AIC: 1441.6
##
## Number of Fisher Scoring iterations: 8
glm.probstest90 = predict(glm.fittrain90, test.90, type = 'response')
glm.predtest90 = rep("No",1000)
glm.predtest90[glm.probstest90>0.5]="Yes"
table(glm.predtest90,test.90$default)
##
## glm.predtest90 No Yes
## No 972 16
## Yes 6 6
mean(glm.predtest90!=test.90$default)
## [1] 0.022
This model has a 2.2% miss-classification rate.
Based on the different split it appears that a 90% train, 10% test split has the smallest miss-classification rate.
(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.
sample.observations5 = sample(nrow(Default),nrow(Default)*0.90)
train.90s = Default[sample.observations5,]
test.90s = Default[-sample.observations5,]
glm.fittrain90s = glm(default~income+balance+student, data = train.90s, family = binomial)
summary(glm.fittrain90s)
##
## Call:
## glm(formula = default ~ income + balance + student, family = binomial,
## data = train.90s)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4227 -0.1478 -0.0592 -0.0216 3.7135
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.055e+01 5.026e-01 -20.984 < 2e-16 ***
## income -3.413e-07 8.458e-06 -0.040 0.96782
## balance 5.623e-03 2.382e-04 23.606 < 2e-16 ***
## studentYes -7.044e-01 2.423e-01 -2.907 0.00365 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2664.2 on 8999 degrees of freedom
## Residual deviance: 1456.2 on 8996 degrees of freedom
## AIC: 1464.2
##
## Number of Fisher Scoring iterations: 8
glm.probstest90s = predict(glm.fittrain90s, test.90s, type = 'response')
glm.predtest90s = rep("No",1000)
glm.predtest90s[glm.probstest90s>0.5]="Yes"
table(glm.predtest90s,test.90s$default)
##
## glm.predtest90s No Yes
## No 968 18
## Yes 4 10
mean(glm.predtest90s!=test.90s$default)
## [1] 0.022
Since the 90% train, 10% test split was best, I used the same model but included the variable student and the miss-calssification rate is still 2.2%. So variabel student does not lead to a 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.
library(boot)
library(ISLR)
attach(Default)
set.seed(1)
(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.
glm.fit = glm(default~income+balance, data = Default, family = binomial)
summary(glm.fit)
##
## 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
The standard error for intercept is 4.348e-01, for income is 4.985e-06, for balance is 2.274e-04.
(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){
glm.fit = glm(default~income+balance, data = Default, subset = index, family = binomial)
return(coef(glm.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,10)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Default, statistic = boot.fn, R = 10)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* -1.154047e+01 -7.520387e-02 2.811104e-01
## t2* 2.080898e-05 1.589838e-06 3.324258e-06
## t3* 5.647103e-03 1.784084e-05 1.752440e-04
(d) Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.
The standard error for intercept is 4.348e-01, for income is 4.985e-06, for balance is 2.274e-04 in the original logistic regression model. The standard error for intercept is 3.865294e-01, for income is 5.266881e-06, for balance is 1.486229e-04 in the bootstrap model when replaced 10 times. It appears that the standard errors are similar/close in values.
We will now consider the Boston housing data set, from the MASS library.
library(MASS)
attach(Boston)
(a) Based on this data set, provide an estimate for the population mean of medv. Call this estimate \(ˆ μ\).
pop.mean = mean(medv)
pop.mean
## [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.
pop_se<-sd(medv)/sqrt(nrow(Boston))
pop_se
## [1] 0.4088611
(c) Now estimate the standard error of \(ˆ μ\) using the bootstrap. How does this compare to your answer from (b)?
library(boot)
set.seed(1)
boot.fn = function(data,index){
pop.mean2 = mean(data[index])
return(pop.mean2)
}
set.seed(1)
boot(medv,boot.fn,10)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn, R = 10)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 0.08778656 0.4112905
Estimated population means are the same for both methods. Estimated standard error for (a) is 0.4088611 and for (b) is 0.4112905 so the two methods have relatively similar standard errors.
(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(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
conf.int95 = c(pop.mean -2*0.4112905,pop.mean+2*0.4112905)
conf.int95
## [1] 21.71023 23.35539
The results for both methods for 95% confidence interval are similar.
(e) Based on this data set, provide an estimate, \(ˆ μ\) med, for the median value of medv in the population.
pop.median = median(medv)
pop.median
## [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.
library(boot)
set.seed(1)
boot.fn = function(data,index){
pop.medianbs = median(data[index])
return(pop.medianbs)
}
set.seed(1)
boot(medv,boot.fn,10)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn, R = 10)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 0.105 0.3378445
The standard error when using the median of medv with bootstrap method is 0.3378445 which is less then the standard error when using the mean of medv with bootstrap method.
(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.)
pop.quantile = quantile(medv,c(0.1))
pop.quantile
## 10%
## 12.75
(h) Use the bootstrap to estimate the standard error of \(ˆ μ\) 0.1. Comment on your findings.
library(boot)
set.seed(1)
boot.fn = function(data,index){
pop.quantilebs = quantile(data[index],c(0.1))
return(pop.quantile)
}
set.seed(1)
boot(medv,boot.fn,10)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn, R = 10)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 12.75 0 0
The standard error is small so this shows that the tenth percentile is reflected accurately.