(a) Explain how k-fold cross-validation is implemented.
With k-fold Cross Validation you first divide the observations into k different non-overlapping groups of apx equal size. You then remove the first group, fit the model on the remaining k-1 groups, and see how good the predictions are on the left out group that was first removed by computing MSE1. That process then gets repeated K different times, taking out a different group each time and generating an MSE value. By averaging the K different MSE’s we get an estimated validation (test) error rate for new observations.
(b) What are the advantages and disadvantages of k-fold crossvalidation relative to:
i. The validation set approach?
Disadvantages - The validation set approach is simpler, computationally easier, and generally easier to implement.
Advantages - First, the validation estimate of the test error rate can be highly variable (depending on precisely which observations are included in the training set and which observations are included in the validation set). Second, only a subset of the observations are used to fit the model. Since statistical methods tend to perform worse when trained on fewer observations, this suggests that the validation set error rate may tend to overestimate the test error rate for the model fit on the entire data set.
ii. LOOCV?
Disadvantages - LOOCV has less bias than k-fold CV when k<n
Advantages - LOOCV has higher variance than k-fold CV when k<n and is computationally easier to produce.
Still, there is one fact left to consider for this type of question. While there is a bias-variance trade-off associated with the choice of k in k-fold cross-validation, It has been empirically shown that k=5 and k=10 yield test error rate estimates that suffer neither from excessively high bias or variance. I would consider this a significant advantage for k-fold cross-validation compared to loocv.
(a) Fit a logistic regression model that uses income and balance to predict default.
library(ISLR)
attach(Default)
set.seed(1)
glm.fitA <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(glm.fitA)
##
## 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) 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.
train = sample(dim(Default)[1], dim(Default)[1]/2)
ii. Fit a multiple logistic regression model using only the training observations.
glm.fit5Bii = glm(default ~ income + balance, data = Default, family = binomial, subset = train)
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.
glm.pred = rep("No", dim(Default)[1]/2)
glm.probs = predict(glm.fit5Bii, Default[-train, ], type = "response")
glm.pred[glm.probs > 0.5] = "Yes"
iv. 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)
## [1] 0.0286
(c) 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(2)
train5c = sample(dim(Default)[1], dim(Default)[1]/2)
glm.fit5C = glm(default ~ income + balance, data = Default, family = binomial, subset = train5c)
glm.pred = rep("No", dim(Default)[1]/2)
glm.probs = predict(glm.fit5C, Default[-train5c, ], type = "response")
glm.pred[glm.probs > 0.5] = "Yes"
mean(glm.pred != Default[-train5c, ]$default)
## [1] 0.0276
set.seed(3)
train5c = sample(dim(Default)[1], dim(Default)[1]/2)
glm.fit5C = glm(default ~ income + balance, data = Default, family = binomial, subset = train5c)
glm.pred = rep("No", dim(Default)[1]/2)
glm.probs = predict(glm.fit5C, Default[-train5c, ], type = "response")
glm.pred[glm.probs > 0.5] = "Yes"
mean(glm.pred != Default[-train5c, ]$default)
## [1] 0.0248
set.seed(4)
train5c = sample(dim(Default)[1], dim(Default)[1]/2)
glm.fit5C = glm(default ~ income + balance, data = Default, family = binomial, subset = train5c)
glm.pred = rep("No", dim(Default)[1]/2)
glm.probs = predict(glm.fit5C, Default[-train5c, ], type = "response")
glm.pred[glm.probs > 0.5] = "Yes"
mean(glm.pred != Default[-train5c, ]$default)
## [1] 0.0262
The test error rates produced can be variable depending on which observations are included in the training set and validation set.
(d) 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(1)
Student01 = as.numeric(student == 'Yes')
Default <- data.frame(Default, Student01)
train5d = sample(dim(Default)[1], dim(Default)[1]/2)
glm.fit5D = glm(default ~ income + balance + Student01, data = Default, family = binomial, subset = train5d)
glm.pred = rep("No", dim(Default)[1]/2)
glm.probs = predict(glm.fit5D, Default[-train5d, ], type = "response")
glm.pred[glm.probs > 0.5] = "Yes"
mean(glm.pred != Default[-train5d, ]$default)
## [1] 0.0288
When I created and included the Student01 dummy variable in the logistic regression model I noticed that this test error appears to be larger than the error rates from models without Student01.
(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.
glm.fit6A = glm(default ~ income + balance, data = Default, family = binomial)
summary(glm.fit6A)
##
## 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
The estimated standard errors for the coefficients associated with income and balance are 4.985e-06 and 2.274e-04 respectively.
(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){
return(coef(glm(default~income+balance,data=data,subset=index,family=binomial)))
}
(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)
set.seed(1)
boot(Default,boot.fn,R=1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Default, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* -1.154047e+01 -8.008379e-03 4.239273e-01
## t2* 2.080898e-05 5.870933e-08 4.582525e-06
## t3* 5.647103e-03 2.299970e-06 2.267955e-04
(d) Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.
The estimated standard errors obtained by the two methods are rather close to each other in value.
(a) Based on this data set, provide an estimate for the population mean of medv. Call this estimate ˆ μ.
library(MASS)
attach(Boston)
muhat = mean(medv)
muhat
## [1] 22.53281
(b) 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.
STDEE = sd(Boston$medv) / sqrt(length(Boston$medv))
STDEE
## [1] 0.4088611
So what this value 0.4088611 is quantifying is the average amount that this estimate ˆ μ differs from the actual value of μ.
(c) Now estimate the standard error of ˆ μ using the bootstrap. How does this compare to your answer from (b)?
set.seed(1)
boot.fn = function(data, index) {
mu = mean(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* 22.53281 0.008517589 0.4119374
With R = 1,000, the bootstrap estimated standard error of mu of 0.4119374, which is fairly close to the estimate found in 9(b), which is 0.4088611.
(d) 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
# 95% CI = 21.72953 23.33608
CI.muhat = c(22.53281 - 2 * 0.4119, 22.53281 + 2 * 0.4119)
CI.muhat
## [1] 21.70901 23.35661
The 95% CI from the t-test was between 21.72953 and 23.33608, which is pretty close to the confidence interval for the mean of medv, which is between 21.70901 and 23.35661
(e) Based on this data set, provide an estimate, ˆ μmed, for the median value of medv in the population.
MedMhat = median(medv)
MedMhat
## [1] 21.2
(f) 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(2)
boot.fn = function(data, index) {
mu2 = median(data[index])
return (mu2)
}
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.0275 0.3821843
We get an estimated median value of 21.2, which is the same as out result from 9(e). The standard error of the median is 0.3821843
(g) 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.)
Mu_Hat_10tile = quantile(medv, c(0.1))
Mu_Hat_10tile
## 10%
## 12.75
(h) Use the bootstrap to estimate the standard error of ˆ μ0.1. Comment on your findings.
set.seed(4)
boot.fn = function(data, index) {
muq = quantile(data[index], c(0.1))
return (muq)
}
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.0229 0.5024421
We get an estimated tenth percentile value of 12.75, which is the same as out result from 9(g). The standard error of the tenth percentile is 0.5024421