Question 3

We now review k-fold cross-validation. (a) Explain how k-fold cross-validation is implemented.

K-Fold Cross Validation is used to evaluate the performance of your predictive model. Previously we’ve learned about splitting our data into 1 train set and 1 test set. The K-Fold approach differs in that you divide your data into “k” equal size folds. Then the model is trained k amount of times. During each training, one fold is used as the validation set while the k-1 folds are the training set. Each time that the model is trained, performance metrics are calculated. The performance metrics are averaged to give the model’s overall performance scores (for example, accuracy).

  1. What are the advantages and disadvantages of k-fold cross- validation relative to: i. The validation set approach? ii. LOOCV?

Advantages relative to validation set approach: It can provide a better estimate of model performance because it’s training and validating on all of the data. For validation set approach, you could get very difference performance depending on how the data was split. Disadvantages relative to validation set approach: The performance metrics will vary depending on how many folds you choose. It is also computationally more “expensive” because you are training and evaluating multiple times. Advantages relative to LOOCV: It is less computationally “expensive” because it involves fewer iterations. Would be less biased than LOOCV because it averages over k-folds rather than being influenced by a single data point for each iteration. Disadvantages relative LOOCV: Again, the performance metrics could be more variable than LOOCV depending on how many folds you choose for k. LOOCV tends to have lower bias but higher variance compared to k-folds cv.

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

  1. Fit a logistic regression model that uses income and balance to predict default.
library(ISLR2); library(corrplot); library(MASS); library(caret); library(car); library(dplyr); library(class);library(e1071);library(boot)
## corrplot 0.92 loaded
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:ISLR2':
## 
##     Boston
## Loading required package: ggplot2
## Loading required package: lattice
## Loading required package: carData
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following object is masked from 'package:MASS':
## 
##     select
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## 
## Attaching package: 'boot'
## The following object is masked from 'package:car':
## 
##     logit
## The following object is masked from 'package:lattice':
## 
##     melanoma
default = Default
str(default)
## 'data.frame':    10000 obs. of  4 variables:
##  $ default: Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
##  $ student: Factor w/ 2 levels "No","Yes": 1 2 1 1 1 2 1 2 1 1 ...
##  $ balance: num  730 817 1074 529 786 ...
##  $ income : num  44362 12106 31767 35704 38463 ...
m5a = glm(formula = default ~ income + balance, data = default, family = binomial)
summary(m5a)
## 
## 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
  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.
# i. 
set.seed(42)
index = sample(nrow(default), 0.8*nrow(default), replace = F) # 80/20 split
default_train = default[index,]
default_val = default[-index,]
#ii. 
m5b = glm(formula = default ~ income + balance, data = default_train, family = binomial)
#iii.
predprob_log_default = predict.glm(m5b, default_val, type = "response")
predclass_log_default = ifelse(predprob_log_default >= 0.5, "Yes", "No")
confusionMatrix(as.factor(predclass_log_default), as.factor(default_val$default), positive = "Yes")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   No  Yes
##        No  1932   44
##        Yes    7   17
##                                          
##                Accuracy : 0.9745         
##                  95% CI : (0.9666, 0.981)
##     No Information Rate : 0.9695         
##     P-Value [Acc > NIR] : 0.1061         
##                                          
##                   Kappa : 0.3895         
##                                          
##  Mcnemar's Test P-Value : 4.631e-07      
##                                          
##             Sensitivity : 0.2787         
##             Specificity : 0.9964         
##          Pos Pred Value : 0.7083         
##          Neg Pred Value : 0.9777         
##              Prevalence : 0.0305         
##          Detection Rate : 0.0085         
##    Detection Prevalence : 0.0120         
##       Balanced Accuracy : 0.6375         
##                                          
##        'Positive' Class : Yes            
## 
#iv. (1 - accuracy) is error
print(1-0.9745)
## [1] 0.0255
  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)
train_ctrl <- trainControl(method = "cv",   
                     number = 3)      #number is folds 


m5c <- train(default ~ income + balance, 
             data = default, 
             method = "glm", 
             family = binomial, 
             trControl = train_ctrl)
print(m5c)
## Generalized Linear Model 
## 
## 10000 samples
##     2 predictor
##     2 classes: 'No', 'Yes' 
## 
## No pre-processing
## Resampling: Cross-Validated (3 fold) 
## Summary of sample sizes: 6667, 6667, 6666 
## Resampling results:
## 
##   Accuracy   Kappa    
##   0.9738002  0.4417961
print(m5c$results)
##   parameter  Accuracy     Kappa  AccuracySD    KappaSD
## 1      none 0.9738002 0.4417961 0.002208342 0.05914816

Accuracy is 0.9738002 and error is 0.0261998. This is very similar to the previous method where error was 0.0255.

  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.
set.seed(42)
#dummy variable creation
default$student_dummy <- ifelse(default$student == "Yes", 1, 0)
default_train$student_dummy <- ifelse(default_train$student == "Yes", 1, 0)
default_val$student_dummy <- ifelse(default_val$student == "Yes", 1, 0)
m5d = glm(formula = default ~ income + balance + student_dummy, data = default_train, family = binomial)

predprob_log_default2 = predict.glm(m5d, default_val, type = "response")
predclass_log_default2 = ifelse(predprob_log_default2 >= 0.5, "Yes", "No")
confusionMatrix(as.factor(predclass_log_default2), as.factor(default_val$default), positive = "Yes")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   No  Yes
##        No  1932   42
##        Yes    7   19
##                                           
##                Accuracy : 0.9755          
##                  95% CI : (0.9677, 0.9818)
##     No Information Rate : 0.9695          
##     P-Value [Acc > NIR] : 0.06378         
##                                           
##                   Kappa : 0.4263          
##                                           
##  Mcnemar's Test P-Value : 1.191e-06       
##                                           
##             Sensitivity : 0.3115          
##             Specificity : 0.9964          
##          Pos Pred Value : 0.7308          
##          Neg Pred Value : 0.9787          
##              Prevalence : 0.0305          
##          Detection Rate : 0.0095          
##    Detection Prevalence : 0.0130          
##       Balanced Accuracy : 0.6539          
##                                           
##        'Positive' Class : Yes             
## 
# error
print(1-0.9755)
## [1] 0.0245

Including the dummy student variable decreased the test error by 0.001.

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)

m6a = glm(formula = default ~ income + balance, data = default, family = binomial )
summary(m6a)
## 
## 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
summary(m6a)$coefficients[, 2]
##  (Intercept)       income      balance 
## 4.347564e-01 4.985167e-06 2.273731e-04

Estimated standard errors:
(Intercept) income balance 4.347564e-01 4.985167e-06 2.273731e-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.
boot.fn = function(data, index) {
  # Subset data
  data_subset = data[index, ]
  # Fit model
  model = glm(default ~ income + balance, data = data_subset, family = binomial)
  # Return coefficient estimates
  return(coef(model))
}
  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.
set.seed(42)
boot(data = default, 
                     statistic = boot.fn, 
                     R = 1000) # R is number of iterations
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = default, statistic = boot.fn, R = 1000)
## 
## 
## Bootstrap Statistics :
##          original        bias     std. error
## t1* -1.154047e+01 -2.292405e-02 4.435269e-01
## t2*  2.080898e-05  2.737444e-08 5.073444e-06
## t3*  5.647103e-03  1.176249e-05 2.299133e-04

Standard errors: 4.435e-01, 5.07e-06,2.299e-04

  1. Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.

The standard errors are very similar when you compare the two methods.

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 μˆ.
boston = Boston
mu_hat = mean(boston$medv)
mu_hat
## [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_mu_hat = stats::sd(boston$medv)/(length(boston$medv)^(1/2))
se_mu_hat
## [1] 0.4088611
  1. Now estimate the standard error of μˆ using the bootstrap. How does this compare to your answer from (b)?
set.seed(42)
# bootstrapping function to calculate sample mean
boot_fn = function(data, indices) {
  mean(data[indices])}

# bootstrap
boot_results =boot(boston$medv, boot_fn, R = 1000)

boot_results
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = boston$medv, statistic = boot_fn, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original     bias    std. error
## t1* 22.53281 0.02671186   0.4009216

SE from B is 0.4088611 and SE here is 0.4009216. They are very similar, with this error being slightly lower.

  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(μˆ)].
se_bootstrap <- sd(boot_results$t)
lower_bound <- mu_hat - 2 * se_bootstrap
upper_bound <- mu_hat + 2 * se_bootstrap
lower_bound
## [1] 21.73096
upper_bound
## [1] 23.33465

Based on bootstrap, the CE is 21.73 - 23.33.

t_test_result = t.test(Boston$medv)
t_test_result$conf.int
## [1] 21.72953 23.33608
## attr(,"conf.level")
## [1] 0.95

Based on t-test, the CE is 21.72953 - 23.33608. These results are basically the same as the bootstrap estimate.

  1. Based on this data set, provide an estimate, μˆmed, for the median value of medv in the population.
mu_hat_median = median(boston$medv)
mu_hat_median
## [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)
# bootstrapping function to calculate sample median
boot_fn_median = function(data, indices) {
  median(data[indices])}

boot_results_median <- boot(boston$medv, boot_fn_median, R = 1000)

boot_results_median
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = boston$medv, statistic = boot_fn_median, R = 1000)
## 
## 
## Bootstrap Statistics :
##     original  bias    std. error
## t1*     21.2  0.0106   0.3661785

SE is 0.3661785.

  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_0.1_hat <- quantile(boston$medv, 0.1)

mu_0.1_hat
##   10% 
## 12.75
  1. Use the bootstrap to estimate the standard error of μˆ0.1. Comment on your findings.
set.seed(42)
boot_fn_10th_percentile <- function(data, indices) {
  quantile(data[indices], 0.1)}

boot_results_10th_percentile <- boot(boston$medv, boot_fn_10th_percentile, R = 1000)
boot_results_10th_percentile
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot(data = boston$medv, statistic = boot_fn_10th_percentile, 
##     R = 1000)
## 
## 
## Bootstrap Statistics :
##     original  bias    std. error
## t1*    12.75  0.0215   0.4948966

Standard error estimate is 0.4948966 - slightly bigger than previous standard error.