The k-fold CV approach is implemented by 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 a model is trained using 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. This process results in k estimates of the test error, MSE1,MSE2, . . . ,MSEk. The k-fold CV estimate is computed by averaging these values,
\[CV_{(k)} = \frac{1}{k}\sum_{i=1}^{k} MSE_i\]
Normally, the k-fold CV is performed with k = 5 or k = 10.
The disadvantages of Validation Set over k-fold is that the 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 as they are randomly divided from the available set of observations. Also, since only subset of observations are used in the training set than the validation set, the statistical method tends to perform worst with the validation set error rate tend to overestimate the test error rate as compare to the model that fits entire data set (which is the case in k-fold). On the other hand, the Validation Set is conceptually simple and easy to implement as compared to k-fold which is more computational than the Validation Set.
LOOCV is a special case of k-fold CV when k = n, where n is number of observations and in k-fold CV, the k is either 5 or 10. Because of this, k-fold has computational advantage than the LOOCV where LOOCV has to fit the statistical models n times (especially when n is extremely large). The other advantage of k-fold is that it has bias-variance trade off, that is, k-fold gives more accurate estimates of the test error rate. On the other hand, since the training are performed fewer sets than the LOOCV, it will lead to intermediate level of bias. So, from the bias reduction perspective, LOOCV is preferred than k-fold. In contrary to this, since k-fold CV are performed with k < n, the averaging outputs of the k fitted models are less correlated with each other as the overlap of training sets with each model is smaller. Because of this, the test error estimate resulting from LOOCV tends to have higher variance than the test error estimate resulting from k-fold.
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.income and balance to predict default.attach(Default)
set.seed(100)
glm.default.fit <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(glm.default.fit)
##
## 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
Since the p-value for both predictors income and balance is much smaller than the significance value (0.05), their association with default variable is significant.
Using 75-25% split of the dataset for training and validation.
set.seed(120)
sample_split_75 <- sample.split(Default$default, SplitRatio=3/4)
train75 <- subset(Default, sample_split_75 == TRUE)
validation25 <-subset(Default, sample_split_75 == FALSE)
glm.train75.fit <- glm(default ~ income + balance, data = train75, family = "binomial")
summary(glm.train75.fit)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = train75)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.2077 -0.1451 -0.0575 -0.0210 3.7239
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.167e+01 5.024e-01 -23.219 < 2e-16 ***
## income 2.455e-05 5.713e-06 4.296 1.74e-05 ***
## balance 5.660e-03 2.635e-04 21.478 < 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: 2192.2 on 7499 degrees of freedom
## Residual deviance: 1188.6 on 7497 degrees of freedom
## AIC: 1194.6
##
## Number of Fisher Scoring iterations: 8
default category if the posterior probability is greater than 0.5.default25.probs <- predict(glm.train75.fit, validation25, type="response")
default25.pred <- rep("No", length(validation25$default))
default25.pred[default25.probs > .5] <- "Yes"
table(default25.pred, validation25$default)
##
## default25.pred No Yes
## No 2407 50
## Yes 10 33
mean(default25.pred != validation25$default)
## [1] 0.024
From the above mean calculation, the misclassification rate is 2.4% (validation set error rate). This can be be calculated from (iii) table as well, i.e., ((50+10)/2500) * 100 = 2.4%.
#Split 50%
set.seed(200)
sample_split_50 <- sample.split(Default$default, SplitRatio=1/2)
train50 <- subset(Default, sample_split_50 == TRUE)
validation50 <-subset(Default, sample_split_50 == FALSE)
#Run the regression
glm.train50.fit <- glm(default ~ income + balance, data = train50, family = "binomial")
#Run the prediction
default50.probs <- predict(glm.train50.fit, validation50, type="response")
default50.pred <- rep("No", length(validation50$default))
default50.pred[default50.probs > .5] <- "Yes"
table(default50.pred, validation50$default)
##
## default50.pred No Yes
## No 4827 120
## Yes 6 47
mean(default50.pred != validation50$default)
## [1] 0.0252
From the above mean calculation, the misclassification rate for the Split 1 (50-50 split) is 2.52% (validation set error rate). This can be be calculated from prediction table as well, i.e., ((120+6)/5000) * 100 = 2.52%.
#Split 70%
set.seed(300)
sample_split_70 <- sample.split(Default$default, SplitRatio=0.70)
train70 <- subset(Default, sample_split_70 == TRUE)
validation30 <-subset(Default, sample_split_70 == FALSE)
#Run the regression
glm.train70.fit <- glm(default ~ income + balance, data = train70, family = "binomial")
#Run the prediction
default30.probs <- predict(glm.train70.fit, validation30, type="response")
default30.pred <- rep("No", length(validation30$default))
default30.pred[default30.probs > .5] <- "Yes"
table(default30.pred, validation30$default)
##
## default30.pred No Yes
## No 2887 67
## Yes 13 33
mean(default30.pred != validation30$default)
## [1] 0.02666667
From the above mean calculation, the misclassification rate for the Split 2 (70-30 split) is 2.67% (validation set error rate). This can be be calculated from prediction table as well, i.e., ((13+67)/3000) * 100 = 2.67%.
#Split 80%
set.seed(350)
sample_split_80 <- sample.split(Default$default, SplitRatio=4/5)
train80 <- subset(Default, sample_split_80 == TRUE)
validation20 <-subset(Default, sample_split_80 == FALSE)
#Run the regression
glm.train80.fit <- glm(default ~ income + balance, data = train80, family = "binomial")
#Run the prediction
default20.probs <- predict(glm.train80.fit, validation20, type="response")
default20.pred <- rep("No", length(validation20$default))
default20.pred[default20.probs > .5] <- "Yes"
table(default20.pred, validation20$default)
##
## default20.pred No Yes
## No 1926 46
## Yes 7 21
mean(default20.pred != validation20$default)
## [1] 0.0265
From the above mean calculation, the misclassification rate for the Split 3 (80-20 split) is 2.65% (validation set error rate). This can be be calculated from prediction table as well, i.e., ((7+46)/2000) * 100 = 2.65%.
In all the 3 splits, we see that the 50-50 split has the lowest misclassification rate of 2.52%. But, when this is compared to the 75-25 split (2.4%) in (b), its marginally higher.
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.For this logistic regression model we are using 75-25% split as we found out this is the lowest so far from (b) and (c).
#Split 75-25% ratio
set.seed(120)
sample_dummy_75 <- sample.split(Default$default, SplitRatio=0.75)
train.dummy.75 <- subset(Default, sample_dummy_75 == TRUE)
val.dummy.25 <-subset(Default, sample_dummy_75 == FALSE)
#Run the regression
glm.train.dummy.fit <- glm(default ~ income + balance + student, data = train.dummy.75, family = "binomial")
#Run the prediction
def.dummy.probs <- predict(glm.train.dummy.fit, val.dummy.25, type="response")
def.dummy.pred <- rep("No", length(val.dummy.25$default))
def.dummy.pred[def.dummy.probs > .5] <- "Yes"
table(def.dummy.pred, val.dummy.25$default)
##
## def.dummy.pred No Yes
## No 2405 54
## Yes 12 29
mean(def.dummy.pred != val.dummy.25$default)
## [1] 0.0264
From the above mean calculation, the misclassification rate is 2.64% (validation set error rate). This can be be calculated from prediction table as well, i.e., ((12+54)/2500) * 100 = 2.64%. This is higer than the same split without the addition of the dummy varaible student in the logistic regression model in (b). So, the additional variable DID NOT reduce the test error 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.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(120)
#Fit the logistic regression model for `income` and `balance`
glm.default.fit <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(glm.default.fit)$coefficients[2:3,]
## Estimate Std. Error z value Pr(>|z|)
## income 2.080898e-05 4.985167e-06 4.174178 2.990638e-05
## balance 5.647103e-03 2.273731e-04 24.836280 3.638120e-136
The estimated standard errors for the coefficients associated with income and balance are 4.985167e-06 and 2.273731e-04 respectively.
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) {
glm.fit <- glm(default ~ income + balance, data = data, family = "binomial", subset = index)
return(summary(glm.fit)$coefficients[2:3, 2])
}
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(600)
boot(Default, boot.fn, R=500)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Default, statistic = boot.fn, R = 500)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 4.985167e-06 5.416004e-09 1.386681e-07
## t2* 2.273731e-04 8.407107e-07 1.065559e-05
glm() function and using your bootstrap function.The estimated standard errors of the coefficients of the income and balance predictors from the logistic regression function glm() are SE(\(\hat\beta_{income}\)) = 4.985167e-06 and SE(\(\hat\beta_{balance}\)) = 2.273731e-04 respectively.
The estimate standard errors of the coefficients of the income and balance predictors from the bootstrap function are SE(\(\hat\beta_{income}\)) = 1.386681e-07 and SE(\(\hat\beta_{balance}\)) = 1.065559e-05 respectively.
The Standard Errors of the coefficients of the income and balance from the bootstrap function is lesser than that of the logistic regression. This is because the bootstrap function does not rely on any assumptions that the logistic regression function are.
Boston housing data set, from the MASS library.medv. Call this estimate \(\hat\mu\).mu_hat <- mean(Boston$medv)
mu_hat
## [1] 22.53281
\(\hat\mu\) = 22.53281
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 <- sd(Boston$medv) / sqrt(length(Boston$medv))
se_mu_hat
## [1] 0.4088611
The standard error of \(\hat\mu\) = 0.4088611
set.seed(1000)
boot.fn <- function(data, index) {
mu.hat <- mean(data[index])
return (mu.hat)
}
boot(Boston$medv, boot.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Boston$medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 0.01153162 0.4154223
The standard error of \(\hat\mu\) from the bootstrap is 0.4154223, which is very close to the standard error of \(\hat\mu\) from (b).
medv. Compare it to the results obtained using t.test(Boston$medv).Hint: You can approximate a 95% confidence interval using the formula [\(\hat\mu\) − 2SE(\(\hat\mu\)), \(\hat\mu\) + 2SE(\(\hat\mu\))].
mu_hat_lower <- (22.53281 - (2 * 0.4154223))
mu_hat_upper <- (22.53281 + (2 * 0.4154223))
sprintf("The lower bound of the confidence interval = %f", mu_hat_lower)
## [1] "The lower bound of the confidence interval = 21.701965"
sprintf("The upper bound of the confidence interval = %f", mu_hat_upper)
## [1] "The upper bound of the confidence interval = 23.363655"
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 confidence intervals that are calculated from the Bootstrap estimated standard error are very close to the One Sample t-test confidence intervals estimate (even we can say both are approximately same).
medv in the population.mu_hat_median <- median(Boston$medv)
mu_hat_median
## [1] 21.2
\(\hat\mu_{median}\) = 21.2
set.seed(1000)
boot.fn <- function(data, index) {
mu.hat.median <- median(data[index])
return (mu.hat.median)
}
boot(Boston$medv, boot.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Boston$medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 -0.00225 0.3861691
The estimate of the standard error of the median using the bootstrap, \(\hat\mu_{median}\) = 0.3861691.
The standard error of the median, \(\hat\mu_{median}\), is smaller than the standard error of the mean, \(\hat\mu\) = 0.4154223, which indicates that the median estimate of the entire population could be accurate.
medv in Boston suburbs. Call this quantity \(\hat\mu_{0.1}\). (You can use the quantile() function.)quantile(Boston$medv, 0.1)
## 10%
## 12.75
The estimate of the tenth percentile of medv, \(\hat\mu_{0.1}\) = 12.75.
set.seed(1000)
boot.fn <- function(data, index) {
mu.hat.quantile <- quantile(data[index], 0.1)
return (mu.hat.quantile)
}
boot(Boston$medv, boot.fn, 1000)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Boston$medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 12.75 0.0153 0.5081034
The estimated standard error of \(\hat\mu_{0.1}\) = 0.5081034, which is very small compared to the percentile value in (g). Also, the estimated tenth percentile value 12.75 from bootstrap is same as the value in (g).