K-fold CV is implemented by randomly dividing subsets of observations into groups, called k folds. The two groups have relatively the same number of observations, but the two subsets are treated differently. The first ‘fold’ is a validation set, and the test is fit on the second set, which contains k-1, or the observations not in the first fold. The MSE is computed on the second fold and this is repreated “k” times, hence why the code can contain k=1, k=2, and so on.
the advantages of the validation set approach are that it takes a simple approach and randomly selects subsets to use as validation and training sets, similar to other models. The disadvantages of the validation set approach is that is that the small subset of observations can cause the model to overestimate the test error rate.
The advantages of LOOCV are that it is less bias when choosing the subset of observations, since it only subtracts one. Because it has a higher sample size, the model does not overestimate the test error as much as the validation set approach. However, a disadvantage of LOOCV is that the model has to refit n number of times, making it more complicated.
library(ISLR2)
default <- Default
glm <- glm(default ~ balance + income, family = binomial, data = default)
summary(glm)
##
## Call:
## glm(formula = default ~ balance + income, 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 ***
## balance 5.647e-03 2.274e-04 24.836 < 2e-16 ***
## income 2.081e-05 4.985e-06 4.174 2.99e-05 ***
## ---
## 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
set.seed(1)
train <- sample(10000, 5000)
test = default[-train, ]
train <- default[train, ]
glmfit <- glm(default ~ income + balance, data = train, family = "binomial")
summary(glmfit)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.5830 -0.1428 -0.0573 -0.0213 3.3395
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.194e+01 6.178e-01 -19.333 < 2e-16 ***
## income 3.262e-05 7.024e-06 4.644 3.41e-06 ***
## balance 5.689e-03 3.158e-04 18.014 < 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: 1523.8 on 4999 degrees of freedom
## Residual deviance: 803.3 on 4997 degrees of freedom
## AIC: 809.3
##
## 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.
glmprob <- predict(glmfit, data = test, type = "response")
glmpred <- rep("No", length(glmprob))
glmpred[glmprob > 0.5] <- "Yes"
(mean(glmpred!= test$default))*100
## [1] 4.76
set.seed(2)
train <- sample(10000, 7000)
test = default[-train, ]
train <- default[train, ]
glmfit <- glm(default ~ income + balance, data = train, family = "binomial")
glmprob <- predict(glmfit, data = test, type = "response")
glmpred <- rep("No", length(glmprob))
glmpred[glmprob > 0.5] <- "Yes"
(mean(glmpred!= test$default))*100
## [1] 4.328571
set.seed(3)
train <- sample(10000, 8000)
test = default[-train, ]
train <- default[train, ]
glmfit <- glm(default ~ income + balance, data = train, family = "binomial")
glmprob <- predict(glmfit, data = test, type = "response")
glmpred <- rep("No", length(glmprob))
glmpred[glmprob > 0.5] <- "Yes"
(mean(glmpred!= test$default))*100
## [1] 4.2125
set.seed(4)
train <- sample(10000, 9000)
test = default[-train, ]
train <- default[train, ]
glmfit <- glm(default ~ income + balance, data = train, family = "binomial")
glmprob <- predict(glmfit, data = test, type = "response")
glmpred <- rep("No", length(glmprob))
glmpred[glmprob > 0.5] <- "Yes"
(mean(glmpred!= test$default))*100
## [1] 4.111111
# All the testing errors are different, but still very close to one another. The testing error gets slightly smaller as the ratio gets larger.
##(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.
set.seed(1)
train <- sample(10000, 5000)
test = default[-train, ]
train <- default[train, ]
glmfit <- glm(default ~ income + balance + student, data = train, family = "binomial")
summary(glmfit)
##
## Call:
## glm(formula = default ~ income + balance + student, family = "binomial",
## data = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.5823 -0.1419 -0.0554 -0.0210 3.3961
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.134e+01 6.937e-01 -16.346 <2e-16 ***
## income 1.686e-05 1.122e-05 1.502 0.1331
## balance 5.767e-03 3.213e-04 17.947 <2e-16 ***
## studentYes -5.992e-01 3.324e-01 -1.803 0.0715 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1523.77 on 4999 degrees of freedom
## Residual deviance: 800.07 on 4996 degrees of freedom
## AIC: 808.07
##
## Number of Fisher Scoring iterations: 8
glmprob <- predict(glmfit, data = test, type = "response")
glmpred <- rep("No", length(glmprob))
glmpred[glmprob > 0.5] <- "Yes"
(mean(glmpred!= test$default))*100
## [1] 4.84
## including the dummy variable does not really change the test error rate.
options(scipen = 999)
glmfit <- glm(default ~ income + balance, data = default, family = "binomial")
summary(glmfit)
##
## 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) -11.540468437 0.434756357 -26.545 < 0.0000000000000002 ***
## income 0.000020809 0.000004985 4.174 0.0000299 ***
## balance 0.005647103 0.000227373 24.836 < 0.0000000000000002 ***
## ---
## 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
boot.fn <- function(data, index)
coef(lm(default ~ balance + income , data = data , subset = index))
options(scipen = 999)
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* 0.9077603163140 -0.0001053378024701 0.0066181867075
## t2* 0.0001318049697 0.0000000762187560 0.0000065489340
## t3* 0.0000004604568 0.0000000003423164 0.0000001343323
boston <- ISLR2::Boston
mu <- mean(boston$medv)
mu
## [1] 22.53281
##(b) Provide an estimate of the standard error of ˆµ. Interpret this result.
semu <- sd(boston$medv)
semu <- semu/ sqrt(dim(boston)[1])
semu
## [1] 0.4088611
# the standard error is .41. This tells use how accurate the mean of any sample of medv is compared to the actual medv mean.
##(c) Now estimate the standard error of ˆµ using the bootstrap. How does this compare to your answer from (b)?
set.seed(123)
boot.fn <- function(data, index) {
mu <- mean(data[index])
return (mu)
}
bboot <- boot(boston$medv, boot.fn, 1000)
bboot
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = boston$medv, statistic = boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 -0.01607372 0.4045557
## the standard errors are similar, the computed value is .41 and the bootstrap value with a seed of 123 is .40.
##(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).
t.test(boston$medv)
##
## One Sample t-test
##
## data: boston$medv
## t = 55.111, df = 505, p-value < 0.00000000000000022
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## 21.72953 23.33608
## sample estimates:
## mean of x
## 22.53281
ConfidenceInt <- c(22.533 - 2 * 0.405, 22.533 + 2 * 0.405)
ConfidenceInt
## [1] 21.723 23.343
# The confidence interval for both methods are pretty much the same. The T-Test confidence interval is 21.73 to 23.34 and the calculated one is 21.72 to 23.34.
##(e) Based on this data set, provide an estimate, ˆµmed, for the median value of medv in the population.
muhat <- median(boston$medv)
##(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.
SEmuhat <- sd(boston$medv) / sqrt(dim(Boston)[1])
SEmuhat
## [1] 0.4088611
##(g) Based on this data set, provide an estimate for the tenth percentile of medv in Boston census tracts. Call this quantity ˆµ0.1. (You can use the quantile() function.)
mu0.1 <- quantile(boston$medv, c(0.1))
mu0.1
## 10%
## 12.75
##(h) 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(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.0071 0.5138566
# The standard error is .514 and the estimate of the tenth percentile median value is 12.75. These results are different in part C, where the standard error was .4 and the mu was 22.5. (muhat was 21.2)