We now review k-fold cross-validation.
(a) Explain how k-fold cross-validation is implemented.
In k-fold cross-validation, we divide the data set into K different parts. We then remove the first part, fit the model on the remaining K-1 parts, and see how good the predictions are on the left out part. We then repeat this K different times taking out a different part each time. Then, by averaging the K different MSE’s we get an estimated validation (test) error rate for new observations,
(b) What are the advantages and disadvantages of k-fold crossvalidation relative to:
i. The validation set approach?
The validation set approach is comparably simple and easy to implement. However its disadvantages are that the validation MSE can be highly variable and only a subset of observations are used to fit the model (training data). Statistical methods tend to perform worse when trained on fewer observations
ii. LOOCV?
LOOCV has less bias.We repeatedly fit the statistical learning method using training data that contains n-1 obs. Also, LOOCV produces a less variable MSE.The validation approach produces different MSE when applied repeatedly due to randomness in the splitting process, while performing LOOCV multiple times will always yield the same results, because we split based on 1 obs. each time. However the disadvantage is that LOOCV is 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)
(a) Fit a logistic regression model that uses income and balance to predict default.
set.seed(1)
glm.fits <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(glm.fits)
##
## 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) 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.
train = sample(dim(Default)[1], dim(Default)[1] / 2)
ii. Fit a multiple logistic regression model using only the training observations.
glm.fits = glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
summary(glm.fits)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = train)
##
## 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.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(glm.probs))
glm.pred[glm.probs > .5] = "Yes"
iv. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
mean(glm.pred != Default[-train, ]$default)
## [1] 0.0254
(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.
train = sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fits = glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
summary(glm.fits)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.5420 -0.1329 -0.0512 -0.0176 3.7909
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.193e+01 6.379e-01 -18.709 < 2e-16 ***
## income 1.939e-05 6.953e-06 2.789 0.00528 **
## balance 5.918e-03 3.355e-04 17.641 < 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: 1490.52 on 4999 degrees of freedom
## Residual deviance: 774.41 on 4997 degrees of freedom
## AIC: 780.41
##
## Number of Fisher Scoring iterations: 8
glm.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(glm.probs))
glm.pred[glm.probs > .5] = "Yes"
mean(glm.pred != Default[-train, ]$default)
## [1] 0.0274
train = sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fits = glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
summary(glm.fits)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.1634 -0.1446 -0.0553 -0.0203 3.3281
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.158e+01 6.008e-01 -19.281 < 2e-16 ***
## income 1.975e-05 6.775e-06 2.916 0.00355 **
## balance 5.723e-03 3.180e-04 17.996 < 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: 1543.58 on 4999 degrees of freedom
## Residual deviance: 816.44 on 4997 degrees of freedom
## AIC: 822.44
##
## Number of Fisher Scoring iterations: 8
glm.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(glm.probs))
glm.pred[glm.probs > .5] = "Yes"
mean(glm.pred != Default[-train, ]$default)
## [1] 0.0244
train = sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fits = glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
summary(glm.fits)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4027 -0.1517 -0.0624 -0.0233 3.6833
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.112e+01 5.816e-01 -19.120 <2e-16 ***
## income 1.638e-05 6.755e-06 2.425 0.0153 *
## balance 5.489e-03 3.067e-04 17.897 <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: 1503.85 on 4999 degrees of freedom
## Residual deviance: 831.51 on 4997 degrees of freedom
## AIC: 837.51
##
## Number of Fisher Scoring iterations: 8
glm.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(glm.probs))
glm.pred[glm.probs > .5] = "Yes"
mean(glm.pred != Default[-train, ]$default)
## [1] 0.0244
We can see that set error rate is different for three different splits of the observation. This is caused by different inclusion of sets of observations.
(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.
train = sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fits = glm(default ~ income + balance + student, data = Default, family = "binomial", subset = train)
summary(glm.fits)
##
## Call:
## glm(formula = default ~ income + balance + student, family = "binomial",
## data = Default, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.3850 -0.1413 -0.0568 -0.0212 3.7409
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.042e+01 6.921e-01 -15.058 <2e-16 ***
## income -5.472e-06 1.205e-05 -0.454 0.6498
## balance 5.638e-03 3.276e-04 17.212 <2e-16 ***
## studentYes -8.286e-01 3.468e-01 -2.389 0.0169 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1409.45 on 4999 degrees of freedom
## Residual deviance: 767.12 on 4996 degrees of freedom
## AIC: 775.12
##
## Number of Fisher Scoring iterations: 8
glm.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(glm.probs))
glm.pred[glm.probs > .5] = "Yes"
mean(glm.pred != Default[-train, ]$default)
## [1] 0.0278
According to the data, the set error has not seem to significantly decrease just by including a dummy variable of “student”.
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(ISLR)
attach(Default)
(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)
glm.fits = glm(default ~ income + balance, data = Default, family = "binomial")
summary(glm.fits)
##
## 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 coefficient of the standard errors are .0438, 4.985e-06, 2.27e-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) {
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.
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 -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.
We can realize the standard errors obtained using the glm and bootstrap are pretty close to each other.
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 \(ˆμ\).
mu.hat = mean(medv)
mu.hat
## [1] 22.53281
Our estimate for the population mean of medv is 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.hat = sd(medv)/sqrt(length(medv))
se.hat
## [1] 0.4088611
The estimate of the standard error is 0.4088611
(c) Now estimate the standard error of \(ˆμ\) using the bootstrap. How does this compare to your answer from (b)?
bootmu.fn = function(data, index){
mu = mean(data[index])
return(mu)}
boot(medv, bootmu.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = bootmu.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 0.0109415 0.414028
Our standard error is 0.4094961 which differs from (b) which was 0.4088611
(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
ci.muhat = c(22.53281 - 2 * 0.4094961 , 22.53281 + 2 * 0.4094961)
ci.muhat
## [1] 21.71382 23.35180
We can see that the confidence interval of bootstrap value are about 0.015 greater or less than the t.test 95 confidence interval.
(e) Based on this data set, provide an estimate, \(ˆμ\)med, for the median value of medv in the population.
mu.med = median(medv)
mu.med
## [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.
bootmed.fn = function(data,index){
med.mu = (median(data[index]))
return(med.mu)}
boot(medv,bootmed.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = bootmed.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 -0.02095 0.3707169
Our bootstrap statistics gave us the standard error of 0.3830635. Considering that our original is 21.2 the error can be said to be relatively small value.
(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.)
mu.1 = quantile(medv, c(0.1))
mu.1
## 10%
## 12.75
(h) Use the bootstrap to estimate the standard error of ˆμ0.1. Comment on your findings.
boot.1.fn = function(data,index){
mu.1 = quantile(data[index],c(0.1))
return(mu.1)
}
boot(medv,boot.1.fn,1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.1.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 12.75 0.02795 0.5077737
We can see that original is same for bootstrap and the estimate while the standard error is relatively small.