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
train=sample(10000,5000)
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
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)
The validation set approach gave us a 2.54% test error rate.
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)
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.
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.
library(boot)
boot.fn=function(data,index)
return(coef(lm(default~income+balance,data=data,subset=index)))
boot(Auto,boot.fn,1000)
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.
library(MASS)
attach(Boston)
mu.hat = mean(medv)
mu.hat
se.hat = sd(medv)/sqrt(dim(Boston)[1])
se.hat
set.seed(1)
boot.fn=function(data,index)
return(coef(lm(medv,data=data,subset=index)))
boot(medv, boot.fn, 1000)