(a) Explain how k-fold cross-validation is implemented.
K-fold cross validation approach involves randomly dividing the set of observations into k groups, or folds, of approximately equal size. The first fold is treated as a validation set, and the method is fit on the remaining k − 1 folds. The mean squared error, MSE1, is then computed on the observations in the held-out fold. This procedure is repeated k times; each time, a different group of observations is treated as a validation set. The K-fold CV estimate is calculated by averaging all the k MSE’s.
(b) What are the advantages and disadvantages of k-fold cross-validation relative to: i. The validation set approach? ii. LOOCV? K-fold cross validation approach is more stable in estimating mean square error than validation set approach. the MSE calculated from different sets of data in validation set method differ greatly among themselves. On the other hand, LOOCV generates stable MSE estimates but it more computationally expensive than k-fold as it fits the model n times (total no of data points) and is is much evident in higher order and complex fits of model.
(a) Fit a logistic regression model that uses income and balance to predict default.
attach(Default)
default.glm.fit = glm(default ~ income + balance, data = Default, family = "binomial")
(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.
set.seed(1)
train = sample(nrow(Default), 0.8*nrow(Default))
ii. Fit a multiple logistic regression model using only the training observations.
default.glm.fit2 = glm(default ~ income + balance, data = Default, subset = train, family = "binomial")
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.fit2.pred = predict(default.glm.fit2, newdata = Default[-train,], type = 'response')
glm.fit2.class = ifelse(glm.fit2.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.
mean(Default$default[-train] != glm.fit2.class)
## [1] 0.026
The validation set error, i.e, fraction of observations that are misclassified is 2.6% when using 80-20 spilt, 80% of data for training and 20% for testing.
(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.
70-30 split
set.seed(1)
train = sample(nrow(Default), 0.7*nrow(Default))
default.glm.fit3 = glm(default ~ income + balance, data = Default, subset = train, family = "binomial")
glm.fit3.pred = predict(default.glm.fit3, newdata = Default[-train,], type = 'response')
glm.fit3.class = ifelse(glm.fit3.pred > 0.5,'Yes','No')
mean(Default$default[-train] != glm.fit3.class)
## [1] 0.02666667
The validation set error, i.e, fraction of observations that are misclassified is 2.67% when using 70-30 spilt, 70% of data for training and 30% for testing.
60-40 split
set.seed(1)
train = sample(nrow(Default), 0.6*nrow(Default))
default.glm.fit4 = glm(default ~ income + balance, data = Default, subset = train, family = "binomial")
glm.fit4.pred = predict(default.glm.fit4, newdata = Default[-train,], type = 'response')
glm.fit4.class = ifelse(glm.fit4.pred > 0.5,'Yes','No')
mean(Default$default[-train] != glm.fit4.class)
## [1] 0.025
The validation set error, i.e, fraction of observations that are misclassified is 2.5% when using 60-40 split, 60% of data for training and 40% for testing.
50-50 split
set.seed(1)
train = sample(nrow(Default), 0.5*nrow(Default))
default.glm.fit5 = glm(default ~ income + balance, data = Default, subset = train, family = "binomial")
glm.fit5.pred = predict(default.glm.fit5, newdata = Default[-train,], type = 'response')
glm.fit5.class = ifelse(glm.fit5.pred > 0.5,'Yes','No')
mean(Default$default[-train] != glm.fit5.class)
## [1] 0.0254
The validation set error, i.e, fraction of observations that are misclassified is 2.54% when using 50-50 split, 50% of data for training and 50% for testing.
Thus it is evident that using different subsets of data for training, and testing model in validation set approach yields different validation set error. Besides, this method is not best suited if the number of observations are small, as it doesn’t use the entire data to train the model.
(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)
train = sample(nrow(Default), 0.8*nrow(Default))
default.glm.fit6 = glm(default ~ income + balance+ student, data = Default, subset = train, family = "binomial")
glm.fit6.pred = predict(default.glm.fit6, newdata = Default[-train,], type = 'response')
glm.fit6.class = ifelse(glm.fit6.pred > 0.5,'Yes','No')
mean(Default$default[-train] != glm.fit6.class)
## [1] 0.0275
After the addition of dummy variable student the validation set error is 2.75, there is no reducttion in error rate after the addition of the dummy variable.
(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.
default.glm.fit7 = glm(default ~ income + balance, data = Default, family = "binomial")
summary(default.glm.fit7)$coef
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.154047e+01 4.347564e-01 -26.544680 2.958355e-155
## income 2.080898e-05 4.985167e-06 4.174178 2.990638e-05
## balance 5.647103e-03 2.273731e-04 24.836280 3.638120e-136
(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, family = "binomial",data = data, subset = index)))
(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.
boot.fn(Default, 1:10000)
## (Intercept) income balance
## -1.154047e+01 2.080898e-05 5.647103e-03
set.seed(1)
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 -3.945460e-02 4.344722e-01
## t2* 2.080898e-05 1.680317e-07 4.866284e-06
## t3* 5.647103e-03 1.855765e-05 2.298949e-04
(d) Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.
The estimated standard errors from glm() function and boot() are nearly same for the slope, income and balance variables. This suggests that bootstrap is an extremely powerful tool to quantify uncertainty associated with given estimator or statistical learning model.
(a) Based on this data set, provide an estimate for the population mean of medv. Call this estimate .
detach(Default)
attach(Boston)
mu_hat = mean(medv)
(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.
se = sd(medv)/sqrt(nrow(Boston))
se
## [1] 0.4088611
(c) Now estimate the standard error of using the bootstrap. How does this compare to your answer from (b)?
set.seed(1)
boot.fn2 = function(data, index)
return(mean(data[index]))
boot(medv, boot.fn2, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn2, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 0.007650791 0.4106622
The standard estimate of mean from bootstrap method is a bit higher than that calculated from the dataset. Nevertheless, it is a pretty good estimate.
(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(ˆμ)].
ci = c(mu_hat - 2*0.4106622, mu_hat + 2*0.4106622)
ci
## [1] 21.71148 23.35413
t.test(Boston$medv)
##
## One Sample t-test
##
## data: Boston$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
The mean calculated from one sample t-test of medv falls in 95% confidence interval obtained from mean by bootstrap methods.
(e) 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
(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(1)
boot.fn3 = function(data, index)
return(median(data[index]))
boot(medv, boot.fn3, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn3, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 0.02295 0.3778075
The estimate of the median from bootstrapping method is equal to that calculated from the data. The standard esrror estimate of median is 0.3778075.
(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_0.1 = quantile(medv, 0.1)
mu_0.1
## 10%
## 12.75
(h) Use the bootstrap to estimate the standard error of μ^0.1. Comment on your findings.
set.seed(1)
boot.fn4 = function(data, index)
return(quantile(data[index], 0.1))
boot(medv, boot.fn4, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = boot.fn4, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 12.75 0.0339 0.4767526
The standard error of the tenth percentile of medv is 0.4767526. Bootstrapping technique comes handy in estimating unknown estimator which otherwise would be difficult to calculate using standard statistical softwares.