QUESTION 3

We now review k-fold cross-validation.

  1. Explain how k-fold cross-validation is implemented. Data is randomly split into k number of equal groups. One of the groups is treated as the validation set and the rest are treated as the training sets. This process is repeated k number of times with each time using a different group as the validation set. The results are averaged to provide a single estimation (MSE).

  2. What are the advantages and disadvantages of k-fold cross- validation relative to:

  1. The validation set approach? Compared to validation set approach, test error estimate has less variance. Validation set approach is significantly faster since model just needs training once.
  2. LOOCV? Compared to LOOCV, k-fold method is not as computationally demanding. LOOCV offers more stable performance estimate because it uses nearly all the data for training each iteration.

QUESTION 5

  1. 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.
  1. Fit a logistic regression model that uses income and balance to predict default.
glm(default ~ income + balance, family = "binomial", data = Default)
## 
## Call:  glm(formula = default ~ income + balance, family = "binomial", 
##     data = Default)
## 
## Coefficients:
## (Intercept)       income      balance  
##  -1.154e+01    2.081e-05    5.647e-03  
## 
## Degrees of Freedom: 9999 Total (i.e. Null);  9997 Residual
## Null Deviance:       2921 
## Residual Deviance: 1579  AIC: 1585
  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.
  2. Fit a multiple logistic regression model using only the train- ing observations.
  3. 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.
  4. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
require(caTools)
## Loading required package: caTools
set.seed(42)

def <- Default
sample <- sample.split(def$default, SplitRatio = 0.50)
trainset <- subset(def, sample==TRUE) 
testset <- subset(def, sample==FALSE)
testdef <- testset$default
logmodel = glm(default ~ income+balance, data=trainset, family=binomial)

logprob = predict(logmodel, testset, type="response")
logpreds = rep("No", length(testset$default)) 
logpreds[logprob>0.5] = "Yes"

table(logpreds, testdef)
##         testdef
## logpreds   No  Yes
##      No  4812  112
##      Yes   21   55
round(mean(logpreds!=testset$default),4)
## [1] 0.0266

Validation error 0.0266.

  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.
set.seed(42)
def <- Default
sample <- sample.split(def$default, SplitRatio = 0.60)
trainset <- subset(def, sample==TRUE) 
testset <- subset(def, sample==FALSE)
testdef <- testset$default
logmodel = glm(default ~ income+balance, data=trainset, family=binomial)

logprob = predict(logmodel, testset, type="response")
logpreds = rep("No", length(testset$default)) 
logpreds[logprob>0.5] = "Yes"

table(logpreds, testdef)
##         testdef
## logpreds   No  Yes
##      No  3852   88
##      Yes   15   45
round(mean(logpreds!=testset$default),4)
## [1] 0.0257

60:40 split has validation error of 0.0257.

set.seed(42)
def <- Default
sample <- sample.split(def$default, SplitRatio = 0.70)
trainset <- subset(def, sample==TRUE) 
testset <- subset(def, sample==FALSE)
testdef <- testset$default
logmodel = glm(default ~ income+balance, data=trainset, family=binomial)

logprob = predict(logmodel, testset, type="response")
logpreds = rep("No", length(testset$default)) 
logpreds[logprob>0.5] = "Yes"

table(logpreds, testdef)
##         testdef
## logpreds   No  Yes
##      No  2886   69
##      Yes   14   31
round(mean(logpreds!=testset$default),4)
## [1] 0.0277

70:30 split has validation error of 0.0277.

set.seed(42)
def <- Default
sample <- sample.split(def$default, SplitRatio = 0.80)
trainset <- subset(def, sample==TRUE) 
testset <- subset(def, sample==FALSE)
testdef <- testset$default
logmodel = glm(default ~ income+balance, data=trainset, family=binomial)

logprob = predict(logmodel, testset, type="response")
logpreds = rep("No", length(testset$default)) 
logpreds[logprob>0.5] = "Yes"

table(logpreds, testdef)
##         testdef
## logpreds   No  Yes
##      No  1922   44
##      Yes   11   23
round(mean(logpreds!=testset$default),4)
## [1] 0.0275

80:20 split has validation error of 0.0275.

  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.
require(caTools)
set.seed(42)

def <- Default
sample <- sample.split(def$default, SplitRatio = 0.50)
trainset <- subset(def, sample==TRUE) 
testset <- subset(def, sample==FALSE)
testdef <- testset$default
logmodel = glm(default ~ income+balance+student, data=trainset, family=binomial)

logprob = predict(logmodel, testset, type="response")
logpreds = rep("No", length(testset$default)) 
logpreds[logprob>0.5] = "Yes"

table(logpreds, testdef)
##         testdef
## logpreds   No  Yes
##      No  4809  114
##      Yes   24   53
round(mean(logpreds!=testset$default),4)
## [1] 0.0276

Including the dummy variable “student” does not lead to reduction in the test error rate.

QUESTION 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.
set.seed(42)
defmod<-glm(default ~ income+balance, family = binomial, data=Default)
summary(defmod)
## 
## Call:
## glm(formula = default ~ income + balance, family = binomial, 
##     data = Default)
## 
## 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

Std. errors are in above table.

  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.
boot.fn = function(data, index)
  {
  logmod = glm(default ~ income+balance, data=data, family="binomial", subset=index)
  return(coef(logmod))
  }
  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.
library(boot)
boot(Default, boot.fn, 100)
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = Default, statistic = boot.fn, R = 100)
## 
## 
## Bootstrap Statistics :
##          original        bias     std. error
## t1* -1.154047e+01  8.325503e-04 4.106915e-01
## t2*  2.080898e-05 -3.709342e-07 5.320364e-06
## t3*  5.647103e-03  6.482769e-06 2.003151e-04
  1. Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function. While std. errors are all low, the std. errors for income and balance are lower. Bootstrapping seems to reduce std. errors in comparison to non-bootstrapping. Logistic std. errors assumptions seem satisfied.

QUESTION 9

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

  1. Based on this data set, provide an estimate for the population mean of medv. Call this estimate μˆ.
attach(Boston)
mu = mean(medv)
mu
## [1] 22.53281
  1. Provide an estimate of the standard error of μˆ. 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 = sd(medv)/sqrt(nrow(Boston))
se
## [1] 0.4088611

Estimate of standard error is 0.4089. Sample standard error shows how much the value of the statistic is expected to vary from sample to sample.

  1. Now estimate the standard error of μˆ using the bootstrap. How does this compare to your answer from (b)?
set.seed(42)
boot.fn = function(data,index)
  {
  mu = mean(data[index])
  return(mu)
  }

boot(medv, boot.fn, 100)
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = medv, statistic = boot.fn, R = 100)
## 
## 
## Bootstrap Statistics :
##     original     bias    std. error
## t1* 22.53281 0.01197233   0.3804984

Std. error from bootstapping is pretty closed to the Std. error obtained from (b).

  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). Hint: You can approximate a 95 % confidence interval using the formula [μˆ − 2SE(μˆ), μˆ + 2SE(μˆ)].
t.test(medv)
## 
##  One Sample t-test
## 
## data:  medv
## t = 55.111, df = 505, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  21.72953 23.33608
## sample estimates:
## mean of x 
##  22.53281
conf.mu = c(mu-2*0.3804984, mu+2*0.3804984)
conf.mu
## [1] 21.77181 23.29380

Confidence interval from bootstrapped method is also very similar to the ones obtained from t.test.

  1. Based on this data set, provide an estimate, μˆmed, for the median value of medv in the population.
mu.med = median(medv)
mu.med
## [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.
set.seed(42)

boot.fn<-function(data,index)
  {
  mu.med = median(data[index])
  return(mu.med)
  }

boot(medv, boot.fn, 100)
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = medv, statistic = boot.fn, R = 100)
## 
## 
## Bootstrap Statistics :
##     original  bias    std. error
## t1*     21.2   0.009   0.3735835

Above shows the std. error of median.

  1. Based on this data set, provide an estimate for the tenth percentile of medv in Boston census tracts. Call this quantity μˆ0.1. (You can use the quantile() function.)
mu.quant = quantile(medv, c(0.1))
mu.quant
##   10% 
## 12.75
  1. Use the bootstrap to estimate the standard error of μˆ0.1. Comment on your findings.
boot.fn = function(data, index) 
  {
  mu.quant <- quantile(data[index], c(0.1))
  return (mu.quant)
}

boot(medv, boot.fn, 1000)
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = medv, statistic = boot.fn, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original  bias    std. error
## t1*    12.75 0.01845   0.5034338

Bootstrapped method has lower std. error than 10th percentile. This means population is being represented more accurately.