ISLR Resampling Methods Exercises


Keeping the streak going but now with exercises from chapter 5 in An Introduction to Statistical Learning with Applications in R.

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.fit = glm(default~income+balance,family='binomial')
summary(glm.fit)
The following objects are masked from Default (pos = 3):

    balance, default, income, student





Call:
glm(formula = default ~ income + balance, family = "binomial")

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: * Split the sample set into a training set and a validation set.

train = sample(dim(Default)[1], dim(Default)[1] / 2)
  • Fit a multiple logistic regression model using only the training observations.
glm.fit2 = glm(default~income+balance,data = Default,family='binomial',subset=train)
summary(glm.fit2)
Call:
glm(formula = default ~ income + balance, family = "binomial", 
    data = Default, subset = train)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-2.3583  -0.1268  -0.0475  -0.0165   3.8116

Coefficients:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept) -1.208e+01  6.658e-01 -18.148   <2e-16 ***
income       1.858e-05  7.573e-06   2.454   0.0141 *  
balance      6.053e-03  3.467e-04  17.457   <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: 1457.0  on 4999  degrees of freedom
Residual deviance:  734.4  on 4997  degrees of freedom
AIC: 740.4

Number of Fisher Scoring iterations: 8
  • 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.
probs <- predict(glm.fit2, newdata = Default[-train, ], type = "response")
glm.pred <- rep("No", length(probs))
glm.pred[probs > 0.5] <- "Yes"
  • 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)

0.0286

We obtain a test error of \(2.86\%\) using the validation set approach.

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

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

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 -8.331926e-03 4.240329e-01
t2*  2.080898e-05  5.792741e-08 4.590086e-06
t3*  5.647103e-03  2.526993e-06 2.268457e-04