options(knitr.duplicate.label = "allow")

Problem 3 We now review k-fold cross-validation. (a) Explain how k-fold cross-validation is implemented. -This approach involves randomly dividing the set of observations into k groups, or folds, of approximately equal size. This procedure is repeated k times; each time, a different group of observations is treated as a validation set. This process results in k estimates of the test error, MSE1, MSE2, . . . , MSEk. The k-fold CV estimate is computed by averaging these values. (b) What are the advantages and disadvantages of k-fold cross-validation relative to: i. The validation set approach? -A disadvantage of cross validation comapred to k-fold is that cross validation process is that only a subset of the observations are used to fit the model, so the validation set error may overestimate the test error rate for the model fit on the entire data set. ii. LOOCV? -An advantage to using LOOCV over k-fold is that LOOCV is less bias.

Problem 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. (a) Fit a logistic regression model that uses income and balance to predict default.

library(ISLR)
attach(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
  1. Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:
  1. Split the sample set into a training set and a validation set.
train=sample(10000,5000)
  1. Fit a multiple logistic regression model using only the training observations.
glm.fits1=glm(default~income+balance,data=Default,family=binomial,subset=train)
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
  1. 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(probs))
glm.pred[probs > 0.5] = "Yes"
  1. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
mean(pred.glm != Default[-train,]$default)

The validation set approach gave us a 2.54% test error rate.

  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.
train=sample(10000,5000)
glm.fits2=glm(default~income+balance,data=Default,family=binomial,subset=train)
glm.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(probs))
glm.pred[probs > 0.5] = "Yes"
mean(pred.glm != Default[-train,]$default)
train=sample(10000,5000)
glm.fits3=glm(default~income+balance,data=Default,family=binomial,subset=train)
glm.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(probs))
glm.pred[probs > 0.5] = "Yes"
mean(pred.glm != Default[-train,]$default)
train=sample(10000,5000)
glm.fits4=glm(default~income+balance,data=Default,family=binomial,subset=train)
glm.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(probs))
glm.pred[probs > 0.5] = "Yes"
mean(pred.glm != Default[-train,]$default)
  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.
train=sample(10000,5000)
glm.fits5=glm(default~income+balance+student,data=Default,family=binomial,subset=train)
glm.probs = predict(glm.fits, newdata = Default[-train, ], type = "response")
glm.pred = rep("No", length(probs))
glm.pred[probs > 0.5] = "Yes"
mean(pred.glm != Default[-train,]$default)

By adding the student variable, we do not see a reduction to the test error rate.

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

  1. 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.fits6=glm(default~income+balance,data=Default,family=binomial)
summary(glm.fits6)

The glm() function estimates that the standard error for the intercept to be 4.348e-01, the income standard error to be 4.985e-06, and the balance standard error to be 2.274e-04.

  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 multiple logistic regression model.
library(boot)
boot.fn=function(data,index)
  return(coef(lm(default~income+balance,data=data,subset=index)))
  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(Auto,boot.fn,1000)
  1. Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.
detach(Default)

-The glm() function and bootstrapping both have similar processes and the standard errors are fairly close.

Problem 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 (mu-hat).
library(MASS)
attach(Boston)
mu.hat = mean(medv)
mu.hat
  1. Provide an estimate of the standard error of (mu-hat). 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(dim(Boston)[1])
se.hat
  1. Now estimate the standard error of (mu-hat) using the bootstrap. How does this compare to your answer from (b)?
set.seed(1)
boot.fn=function(data,index)
  return(coef(lm(medv,data=data,subset=index)))
boot(medv, boot.fn, 1000)