3A: Explain how k-fold cross-validation is implemented
3B: What are the advantages and disadvantages of k-fold crossvalidation relative to:
The validation set approach?
Advantage of k-fold CV relative to validation set approach: The validation approach test error rate has high variance, depending on the observations included in training and validation set. And it tends to overestimate the test error rate for the model fit on the entire data set.
Disadvantage of k-fold CV relative to validation set approach: The validation set approach is fairly easy and simple to implement
LOOCV?
Advantage of k-fold CV relative to LOOCV: In LOOCV, we repeatedly fit the statistical learning method using training sets that contain n-1 observations, almost as many as are in the entire dataset. This has the potential to be computationally expensive, since we have to fit the model n times – this can be time consuming if n is large and if each individual model is slow to it.
Disadvantage of k-fold CV relative to LOOCV: LOOCV is a very general method, and can be used with any kind of predictive modeling. It should be considered when the main purpose is bias reduction since it tends to have less bias than k-fold CV.
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.5A: Fit a logistic regression model that uses income and balance to predict default
set.seed(9)
library(ISLR)
attach(Default)
glm.default <- glm(default ~ income + balance, Default, family = 'binomial')
summary(glm.default)
##
## 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
5B: Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:
i. Split the sample set into a training set and a validation set.
subset <- sample(nrow(Default),nrow(Default)*0.9)
default.train <- Default[subset,]
default.test <- Default[-subset,]
ii. Fit a multiple logistic regression model using only the training observations.
glm.90 <- glm(default ~ income + balance, family = 'binomial', data = default.train)
summary(glm.90)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = default.train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4050 -0.1475 -0.0598 -0.0226 3.6977
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.128e+01 4.500e-01 -25.069 < 2e-16 ***
## income 1.881e-05 5.272e-06 3.568 0.00036 ***
## balance 5.508e-03 2.352e-04 23.413 < 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: 2576.5 on 8999 degrees of freedom
## Residual deviance: 1420.1 on 8997 degrees of freedom
## AIC: 1426.1
##
## Number of Fisher Scoring iterations: 8
iii. 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.
pred <- predict(glm.90, default.test, type = 'response')
class <- ifelse(pred > 0.5, 'Yes', 'No')
iv. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
table(default.test$default, class, dnn=c("Actual","Predicted"))
## Predicted
## Actual No Yes
## No 958 1
## Yes 28 13
round(mean(class!=default.test$default),4)
## [1] 0.029
5C: 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.
#80/20
set.seed(9)
subset1 <- sample(nrow(Default),nrow(Default)*0.8)
default.train1 <- Default[subset1,]
default.test1 <- Default[-subset1,]
glm.80 <- glm(default ~ income + balance, family = 'binomial', data = default.train1)
pred1 <- predict(glm.80, default.test1, type = 'response')
class1 <- ifelse(pred1 > 0.5, 'Yes', 'No')
round(mean(class1!=default.test1$default),4)
## [1] 0.0255
#70/30
set.seed(9)
subset2 <- sample(nrow(Default),nrow(Default)*0.7)
default.train2 <- Default[subset2,]
default.test2 <- Default[-subset2,]
glm.70 <- glm(default ~ income + balance, family = 'binomial', data = default.train2)
pred2 <- predict(glm.70, default.test2, type = 'response')
class2 <- ifelse(pred2 > 0.5, 'Yes', 'No')
round(mean(class2!=default.test2$default),4)
## [1] 0.0233
#50/50
set.seed(9)
subset3 <- sample(nrow(Default),nrow(Default)*0.5)
default.train3 <- Default[subset3,]
default.test3 <- Default[-subset3,]
glm.50 <- glm(default ~ income + balance, family = 'binomial', data = default.train3)
pred3 <- predict(glm.50, default.test3, type = 'response')
class3 <- ifelse(pred3 > 0.5, 'Yes', 'No')
round(mean(class3!=default.test3$default),4)
## [1] 0.0246
Comment: As you can see, three different ratios yield different misclassification/error rate for different samples (although not that far off)
5D: 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.
#80/20
set.seed(9)
subset4 <- sample(nrow(Default),nrow(Default)*0.8)
default.train4 <- Default[subset4,]
default.test4 <- Default[-subset4,]
glm.new <- glm(default ~ income + balance + student, family = 'binomial', data = default.train4)
pred4 <- predict(glm.new, default.test4, type = 'response')
class4 <- ifelse(pred4 > 0.5, 'Yes', 'No')
round(mean(class4!=default.test4$default),4)
## [1] 0.026
Comment: Using a 80/20 split, we can see that using a dummy variable student does not lead to a reduction in the test error/misclassification rate
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.set.seed(9)
attach(Default)
## The following objects are masked from Default (pos = 3):
##
## balance, default, income, student
6A: 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.def <- glm(default ~ income + balance, Default, family = 'binomial')
summary(glm.def)
##
## 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
Comment: The estimated standard errors for the coefficients associated with income is 4.985e-06, for balance, 2.274e-04
6B: 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, Default, family = 'binomial', subset = index)
return (coef(fit))
}
6C: 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 -2.593696e-02 4.234840e-01
## t2* 2.080898e-05 -1.006081e-07 4.768061e-06
## t3* 5.647103e-03 1.608805e-05 2.237253e-04
6D: Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.
glm() function for 6ABoston housing data set, from the MASS library9A: Based on this data set, provide an estimate for the population mean of medv. Call this estimate ˆμ.
mean(Boston$medv)
## [1] 22.53281
9B: 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.)
estimated_sd <- sd(Boston$medv)/sqrt(506)
print(estimated_sd)
## [1] 0.4088611
9C: Now estimate the standard error of ˆμ using the bootstrap. How does this compare to your answer from (b)?
set.seed(9)
fn <- function(data,index){
return(mean(data[index]))
}
boot(Boston$medv, fn, R=1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Boston$medv, statistic = fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 0.009616206 0.4196039
Comment: The boostrap result yielded a very close answer to 9B (0.4196)
9D: 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
CI.mu.hat <- c(22.53 - 2 * 0.4196, 22.53 + 2 * 0.4196)
CI.mu.hat
## [1] 21.6908 23.3692
Comment: The boostrap CI is very close to the one provided by t.test() function
9E: Based on this data set, provide an estimate, ˆμmed, for the median value of medv in the population.
med.hat <- median(medv)
med.hat
## [1] 21.2
9F: 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.
boot.fn <- function(data, index) {
mu <- median(data[index])
return (mu)
}
boot(medv, boot.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 0.0056 0.3733135
Comment: We have an estimated median value of 21.2, which is exactly the same answer provided in 9E, and a standard error of 0.3733.
9G: Based on this data set, provide an estimate for the tenth percentile of medv in Boston suburbs. Call this quantity ˆμ0.1. (You can use the quantile() function.)
percent.hat <- quantile(medv, c(0.1))
percent.hat
## 10%
## 12.75
9H: Use the bootstrap to estimate the standard error of μ̂ 0.1. Comment on your findings.
boot.fn <- function(data, index) {
mu <- quantile(data[index], c(0.1))
return (mu)
}
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.0046 0.5129298
Comment: We have an estimated 10th percentile value of 12.75, which is exactly the same answer provided in 9G, and a standard error of 0.5021.