Take the n observations and randomly splitting it into k non-overlapping groups of length of (approximately) n/k. These groups acts as a validation set, and the remainder (of length n-n/k) acts as a training set. The test error is then estimated by averaging the k resulting MSE estimates.
The validation set approach has two drawbacks compared to k-fold cross-validation.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. 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.
The LOOCV cross-validation approach is a special case of k-fold cross-validation in which k=n. This approach has two drawbacks compared to k-fold cross-validation. It requires fitting the model n times compared to k-fold cross-validation which requires the model to be fitted only k times. The LOOCV cross-validation approach may give approximately unbiased estimates of the test error, since each training set contains n-1 observations; however, this approach has higher variance than k-fold cross-validation. There is a bias-variance trade-off associated with the choice of k in k-fold cross-validation; typically using k=5 or k=10 yield test error rate estimates that suffer neither from excessively high bias nor from very high variance.
library(ISLR)
attach(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
train <- sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fit2 <- glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
summary(glm.fit2)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.3583 -0.1268 -0.0475 -0.0165 3.8116
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.208e+01 6.658e-01 -18.148 <2e-16 ***
## income 1.858e-05 7.573e-06 2.454 0.0141 *
## balance 6.053e-03 3.467e-04 17.457 <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: 1457.0 on 4999 degrees of freedom
## Residual deviance: 734.4 on 4997 degrees of freedom
## AIC: 740.4
##
## Number of Fisher Scoring iterations: 8
prob <- predict(glm.fit2, newdata = Default[-train, ], type = "response")
glm.pred <- rep("No", length(prob))
glm.pred[prob > 0.5] <- "Yes"
mean(glm.pred != Default[-train, ]$default)
## [1] 0.0286
2.86% test error rate with the validation set approach.
train <- sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fit <- glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
prob <- predict(glm.fit, newdata = Default[-train, ], type = "response")
glm.pred <- rep("No", length(prob))
glm.pred[prob > 0.5] <- "Yes"
mean(glm.pred != Default[-train, ]$default)
## [1] 0.0236
train <- sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fit <- glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
prob <- predict(glm.fit, newdata = Default[-train, ], type = "response")
glm.pred <- rep("No", length(prob))
glm.pred[prob > 0.5] <- "Yes"
mean(glm.pred != Default[-train, ]$default)
## [1] 0.028
train <- sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fit <- glm(default ~ income + balance, data = Default, family = "binomial", subset = train)
prob <- predict(glm.fit, newdata = Default[-train, ], type = "response")
glm.pred <- rep("No", length(prob))
glm.pred[prob > 0.5] <- "Yes"
mean(glm.pred != Default[-train, ]$default)
## [1] 0.0268
The validation estimate of the test error rate can be variable, depending on precisely which observations are included in the training set and which observations are included in the validation set.
train <- sample(dim(Default)[1], dim(Default)[1] / 2)
glm.fit3<- glm(default ~ income + balance + student, data = Default, family = "binomial", subset = train)
glm.pred2 <- rep("No", length(prob))
prob <- predict(glm.fit3, newdata = Default[-train, ], type = "response")
glm.pred2[prob > 0.5] <- "Yes"
mean(glm.pred2 != Default[-train, ]$default)
## [1] 0.0264
set.seed(1)
attach(Default)
## The following objects are masked from Default (pos = 3):
##
## balance, default, income, student
fit.glm <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(fit.glm)
##
## 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 glm() estimates of the standard errors for the coefficients B0, B1 and B2 are respectively 0.4347564, 4.985167210^{-6} and 2.273731410^{-4}.
boot.fn <- function(data, index)
{fit <- glm(default ~ income + balance, data = data, family = "binomial", subset = index)
return (coef(fit))}
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 -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
Bootstrap estimates of the standard errors for the coefficients B0, B1 and B2 are respectively 0.4239, 4.583 x 10^(-6) and 2.268 x 10^(-4).
Estimated standard errors obtained by the two methods are pretty close.
library(MASS)
attach(Boston)
mu.hat <- mean(medv)
mu.hat
## [1] 22.53281
se.hat <- sd(medv) / sqrt(dim(Boston)[1])
se.hat
## [1] 0.4088611
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
Bootstrap estimated standard error of U^ of 0.4119 is very close to the estimate found in (b) of 0.4089.
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.4119, 22.53 + 2 * 0.4119)
CI.mu.hat
## [1] 21.7062 23.3538
Bootstrap confidence interval is very close to the one provided by the t.test() function.
med.hat <- median(medv)
med.hat
## [1] 21.2
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.0098 0.3874004
Estimated median value of 21.2 which is equal to the value obtained in (e), with a standard error of 0.3874 which is relatively small compared to median value.
percent.hat <- quantile(medv, c(0.1))
percent.hat
## 10%
## 12.75
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.00515 0.5113487
Estimated tenth percentile value of 12.75 which is again equal to the value obtained in (g), with a standard error of 0.5113 which is relatively small compared to percentile value.
library(rsconnect)