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.

  1. What are the advantages and disadvantages of k-fold crossvalidation relative to:
  1. 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.

  1. 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.

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
  1. Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:

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
  1. 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.
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
  1. 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.
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
  1. 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 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))
}
  1. 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, 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
  1. Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.

Exercise 9

We will now consider the Boston housing data set, from the MASS library.

  1. Based on this data set, provide an estimate for the population mean of medv. Call this estimate ˆµ.
attach(Boston)
set.seed(1)
mean.medv = mean(medv)
mean.medv
## [1] 22.53281
  1. Provide an estimate of the standard error of ˆµ. Interpret this result.
stderr.medv = sd(medv)/sqrt(nrow(Boston))
stderr.medv
## [1] 0.4088611
  1. Now estimate the standard error of ˆµ using the bootstrap. How does this compare to your answer from (b)?
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
  1. 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).
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
  1. Based on this data set, provide an estimate, ˆµmed, for the media value of medv in the population.
med.medv = median(medv)
med.medv
## [1] 21.2
  1. 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.
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
  1. 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.)
percentile.ten = quantile(medv,c(.1))
percentile.ten
##   10% 
## 12.75
  1. Use the bootstrap to estimate the standard error of ˆµ0.1. Comment on your findings.
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