Exercise 3 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.
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.
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.
Exercise 5 (a) 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. Fit a logistic regression model that uses income and balance to predict default.
attach(Default)
data(Default)
set.seed(1)
glm1.default = glm(default ~ income + balance, data = Default, family = binomial)
summary(glm1.default)
##
## 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
i
trainB= sample(dim(Default)[1], dim(Default)[1]/2)
ii
glm2.default = glm(default ~ income + balance, data = Default, family = binomial, subset = trainB)
summary(glm2.default)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = Default, subset = trainB)
##
## 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
glm2.prob = predict(glm2.default, newdata = Default[-trainB,], type = "response")
glm2.pred = rep("No", length(glm2.prob))
glm2.pred[glm2.prob > 0.5] = "Yes"
iv
mean(glm2.pred != Default[-trainB,]$default)
## [1] 0.0254
train1= sample(dim(Default)[1], dim(Default)[1]/2)
glm2.default = glm(default ~ income + balance, data = Default, family = binomial, subset = train1)
glm2.prob = predict(glm2.default, newdata = Default[-train1,], type = "response")
glm2.pred = rep("No", length(glm2.prob))
glm2.pred[glm2.prob > 0.5] = "Yes"
mean(glm2.pred != Default[-train1,]$default)
## [1] 0.0274
train2= sample(dim(Default)[1], dim(Default)[1]/2)
glm2c.default = glm(default ~ income + balance, data = Default, family = binomial, subset = train2)
glm2c.prob = predict(glm2c.default, newdata = Default[-train2,], type = "response")
glm2c.pred = rep("No", length(glm2c.prob))
glm2c.pred[glm2c.prob > 0.5] = "Yes"
mean(glm2c.pred != Default[-train2,]$default)
## [1] 0.0244
train3= sample(dim(Default)[1], dim(Default)[1]/2)
glm3.default = glm(default ~ income + balance, data = Default, family = binomial, subset = train3)
glm3.prob = predict(glm3.default, newdata = Default[-train3,], type = "response")
glm3.pred = rep("No", length(glm3.prob))
glm3.pred[glm3.prob > 0.5] = "Yes"
mean(glm3.pred != Default[-train3,]$default)
## [1] 0.0244
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.trainD= sample(dim(Default)[1], dim(Default)[1]/2)
glmD.default = glm(default ~ income + balance + student, data = Default, family = binomial, subset = trainD)
glmD.prob = predict(glmD.default, newdata = Default[-trainD,], type = "response")
glmD.pred = rep("No", length(glmD.prob))
glmD.pred[glmD.prob > 0.5] = "Yes"
mean(glmD.pred != Default[-trainD,]$default)
## [1] 0.0278
Exercise 6 (a) 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.
model_vars = default~income+balance
fit2.glm = glm(model_vars, data = Default, family = binomial)
summary(fit2.glm)
##
## Call:
## glm(formula = model_vars, 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
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 multiplelogistic regression model.set.seed(1)
boot.fn = function(data,index) {
fit3.glm = glm(default~income+balance, data=data, subset = index , family = binomial)
return(coef(fit3.glm))
}
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, R=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
Exercise 9
We will now consider the Boston housing data set, from the MASS library.
ˆµ.attach(Boston)
set.seed(1)
mean.medv = mean(medv)
mean.medv
## [1] 22.53281
stderr.medv = sd(medv)/sqrt(nrow(Boston))
stderr.medv
## [1] 0.4088611
boston.boot.fn = function(col,index){
cof = mean(col[index])
return(cof)
}
bootstrap = boot(medv, boston.boot.fn, R = 1000)
bootstrap
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boston.boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 0.007650791 0.4106622
left.ci = 22.53281 - 2*.394032
right.ci = 22.53281 + 2*.394032
ci.table = matrix(c(left.ci, right.ci), nrow = 1, ncol =2)
ci.table
## [,1] [,2]
## [1,] 21.74475 23.32087
med.medv = median(medv)
med.medv
## [1] 21.2
med.boot.fn = function(data,index){
est.med = median(data[index])
return(est.med)
}
boot(medv, med.boot.fn, R = 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = med.boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 -0.0386 0.3770241
percentile.ten = quantile(medv,c(.1))
percentile.ten
## 10%
## 12.75
precentile.boot.fn = function(data,index){
precentile = quantile(data[index],c(.1))
return(precentile)
}
boot(medv, precentile.boot.fn, R = 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = precentile.boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 12.75 0.0186 0.4925766