An alternate process to LOOCV is k-fold cross-validation. This process involves randomly selecting and dividing observations into k groups or k folds of equal size approximately.
The first fold is treated as a validation set and the method is fitted on the remaining k-1 folds. The mean square error(MSE) is calculated on this observations in the remaining folds(held out). And, this procedure is repeated for k times, and each time the different observations group is considered for validation set. Thus, the process results in k estimates of the test error such that MSE1,MSE2..MSEk. The k-fold cross validation is compuated or implemented by averaging these values of MSEs(MSE1,MSE2…MSEK).
The most obvious advantage of using the k-fold cross-validation is computational and this is very general approach and which can be applied to most statistical learning approach.
1.Compared to the validation set approach, the k-fold CV has substantially less variability, especially for smaller datasets. Because the data in a validation set is partitioned only once, a model may appear more or less favorable “by chance” depending on the observations that were included in the train and test datasets. For this reason, decisions about model selection and tuning may be heavily influenced by these observations.
2.Model performance is tested and trained using all of the data.
3.Since most models improve with more data and a major fraction is removed entirely from training, the validation set strategy may overestimate the test error when compared to a model that is trained on the entire dataset.
A model is trained and evaluated only once, which is a computational advantage of the validation set approach. For conventional values of k such that 5 and 10, these training datasets will also typically be larger than those used in the validation procedure. In a k-fold CV, k models will be trained. All of this indicates that huge data sets and high values of k may require significantly longer k-fold CV processing times.
Here validation set approach has computational advantage over k-fold CV usually when folds are higher.
At typical values of k, k-fold CV scales well and requires significantly less computing power. An example is 5 and 10 and When k = n, k-fold CV and LOOCV are the same. However, in the case of k = 10 and n = 10,000, k-fold CV will fit 10 models while LOOCV will fit 10,000.
The bias-variance trade-off (LOOCV has larger variance but lower bias), there is some evidence to suggest that k-fold CV can provide a more accurate estimate of the test error rate than LOOCV.
Besides the computational concerns, k-fold CV more accurate estimates of the test error rate than does LOOCV, which is more beneficial when related to trade-offs between bias and variation.
k-fold has high bias.
Similar to validation set approach k-fold CV has element of randomness while selecting the k-fold.
In few cases, LOOCV require less computational than k-fold.
library(ISLR)
## Warning: package 'ISLR' was built under R version 4.3.2
library(MASS)
## Warning: package 'MASS' was built under R version 4.3.2
library(caret)
## Warning: package 'caret' was built under R version 4.3.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Loading required package: lattice
library(boot)
##
## Attaching package: 'boot'
## The following object is masked from 'package:lattice':
##
## melanoma
attach(Default) # from ISLR package.
set.seed(1)
logreg<-glm(default~income+balance,family = binomial,data=Default)
summary(logreg)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = Default)
##
## 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
Interpretation: From the summary of logistic regression model that uses income and balance to predict default shows that predictors(income and balance) are significant at 0.05.
logreg_pred<-predict(logreg,Default$default,type="response")
logreg_pred.class<-ifelse(logreg_pred>0.5,"Yes","No")
Misclassification_Rate=round(mean(Default$default!=logreg_pred.class),4)
print(Misclassification_Rate)
## [1] 0.0263
print(paste("Misclassification Rate:", round(Misclassification_Rate * 100, 2), "%"))
## [1] "Misclassification Rate: 2.63 %"
Interpretation: 0.0263 suggests that the misclassification rate of the logistic regression model on the Default dataset is approximately 2.63%.
Accuracy=1−MisclassificationRate
accuracy <- 1 - Misclassification_Rate
print(paste("Accuracy:", round(accuracy * 100, 2), "%"))
## [1] "Accuracy: 97.37 %"
Interpretation: The accuracy of the logistic regression model that uses income and balance to predict default on the Default dataset is approximately 97.37%.
set.seed(123)
index <- createDataPartition(y = Default$default, p = 0.5, list = F) # I chose 50-50 split approach
train_set <- Default[index, ]
valid_set <- Default[-index, ]
nrow(train_set) / nrow(Default)
## [1] 0.5001
nrow(valid_set) / nrow(Default)
## [1] 0.4999
log_reg_splt<-glm(default~income+balance,family = binomial,data=train_set)
summary(log_reg_splt)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = train_set)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.158e+01 6.209e-01 -18.656 < 2e-16 ***
## income 2.296e-05 7.206e-06 3.187 0.00144 **
## balance 5.617e-03 3.200e-04 17.555 < 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: 1463.76 on 5000 degrees of freedom
## Residual deviance: 791.05 on 4998 degrees of freedom
## AIC: 797.05
##
## Number of Fisher Scoring iterations: 8
Interpretation: From the summary of logistic regression model that uses income and balance to predict default shows that predictors(income and balance) are significant at 0.05.
logreg_splt_pred<-predict(log_reg_splt,valid_set,type="response")
logreg_splt_pred.class<-ifelse(logreg_splt_pred>0.5,"Yes","No")
table(valid_set$default,logreg_splt_pred.class,dnn=c("Actual","Predicted"))
## Predicted
## Actual No Yes
## No 4813 20
## Yes 117 49
Misclassification_Rate2=round(mean(logreg_splt_pred.class!=valid_set$default),4)
print(Misclassification_Rate2)
## [1] 0.0274
print(paste("Misclassification Rate:", round(Misclassification_Rate2 * 100, 2), "%"))
## [1] "Misclassification Rate: 2.74 %"
Interpretation: 0.0274 suggests that the misclassification rate of the logistic regression model on the Default dataset is approximately 2.74%.
Accuracy=1−MisclassificationRate
accuracy2 <- 1 - Misclassification_Rate2
print(paste("Accuracy:", round(accuracy2 * 100, 2), "%"))
## [1] "Accuracy: 97.26 %"
set.seed(123)
index_80 <- createDataPartition(y = Default$default, p = 0.8, list = F)
train_set_80 <- Default[index_80, ]
valid_set_80 <- Default[-index_80, ]
nrow(train_set_80) / nrow(Default)
## [1] 0.8001
nrow(valid_set_80) / nrow(Default)
## [1] 0.1999
log_reg_splt_80<-glm(default~income+balance,family = binomial,data=train_set_80)
summary(log_reg_splt_80)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = train_set_80)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.186e+01 5.007e-01 -23.691 < 2e-16 ***
## income 2.708e-05 5.670e-06 4.777 1.78e-06 ***
## balance 5.715e-03 2.574e-04 22.201 < 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: 2340.6 on 8000 degrees of freedom
## Residual deviance: 1252.2 on 7998 degrees of freedom
## AIC: 1258.2
##
## Number of Fisher Scoring iterations: 8
logreg_splt_pred_80<-predict(log_reg_splt_80,valid_set_80,type="response")
logreg_splt_pred_80.class<-ifelse(logreg_splt_pred_80>0.5,"Yes","No")
table(valid_set_80$default,logreg_splt_pred_80.class,dnn=c("Actual","Predicted"))
## Predicted
## Actual No Yes
## No 1922 11
## Yes 49 17
Misclassification_Rate2_80=round(mean(logreg_splt_pred_80.class!=valid_set_80$default),4)
print(Misclassification_Rate2_80)
## [1] 0.03
print(paste("Misclassification Rate:", round(Misclassification_Rate2_80 * 100, 2), "%"))
## [1] "Misclassification Rate: 3 %"
Accuracy=1−MisclassificationRate
accuracy2_80 <- 1 - Misclassification_Rate2_80
print(paste("Accuracy:", round(accuracy2_80 * 100, 2), "%"))
## [1] "Accuracy: 97 %"
set.seed(123)
index_70 <- createDataPartition(y = Default$default, p = 0.7, list = F)
train_set_70 <- Default[index_70, ]
valid_set_70 <- Default[-index_70, ]
nrow(train_set_70) / nrow(Default)
## [1] 0.7001
nrow(valid_set_70) / nrow(Default)
## [1] 0.2999
log_reg_splt_70<-glm(default~income+balance,family = binomial,data=train_set_70)
summary(log_reg_splt_70)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = train_set_70)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.142e+01 5.131e-01 -22.250 < 2e-16 ***
## income 2.383e-05 5.973e-06 3.989 6.64e-05 ***
## balance 5.533e-03 2.681e-04 20.639 < 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: 2050.6 on 7000 degrees of freedom
## Residual deviance: 1139.1 on 6998 degrees of freedom
## AIC: 1145.1
##
## Number of Fisher Scoring iterations: 8
logreg_splt_pred_70<-predict(log_reg_splt_70,valid_set_70,type="response")
logreg_splt_pred_70.class<-ifelse(logreg_splt_pred_70>0.5,"Yes","No")
table(valid_set_70$default,logreg_splt_pred_70.class,dnn=c("Actual","Predicted"))
## Predicted
## Actual No Yes
## No 2885 15
## Yes 61 38
Misclassification_Rate2_70=round(mean(logreg_splt_pred_70.class!=valid_set_70$default),4)
print(Misclassification_Rate2_70)
## [1] 0.0253
print(paste("Misclassification Rate:", round(Misclassification_Rate2_70 * 100, 2), "%"))
## [1] "Misclassification Rate: 2.53 %"
Accuracy=1−MisclassificationRate
accuracy2_70 <- 1 - Misclassification_Rate2_70
print(paste("Accuracy:", round(accuracy2_70 * 100, 2), "%"))
## [1] "Accuracy: 97.47 %"
set.seed(123)
index_90 <- createDataPartition(y = Default$default, p = 0.9, list = F)
train_set_90 <- Default[index_90, ]
valid_set_90 <- Default[-index_90, ]
nrow(train_set_90) / nrow(Default)
## [1] 0.9001
nrow(valid_set_90) / nrow(Default)
## [1] 0.0999
log_reg_splt_90<-glm(default~income+balance,family = binomial,data=train_set_90)
summary(log_reg_splt_90)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = train_set_90)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.133e+01 4.495e-01 -25.210 < 2e-16 ***
## income 1.895e-05 5.259e-06 3.603 0.000315 ***
## balance 5.552e-03 2.353e-04 23.596 < 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: 2630.7 on 9000 degrees of freedom
## Residual deviance: 1436.8 on 8998 degrees of freedom
## AIC: 1442.8
##
## Number of Fisher Scoring iterations: 8
logreg_splt_pred_90<-predict(log_reg_splt_90,valid_set_90,type="response")
logreg_splt_pred_90.class<-ifelse(logreg_splt_pred_90>0.5,"Yes","No")
table(valid_set_90$default,logreg_splt_pred_90.class,dnn=c("Actual","Predicted"))
## Predicted
## Actual No Yes
## No 962 4
## Yes 24 9
Misclassification_Rate2_90=round(mean(logreg_splt_pred_90.class!=valid_set_90$default),4)
print(Misclassification_Rate2_90)
## [1] 0.028
print(paste("Misclassification Rate:", round(Misclassification_Rate2_90 * 100, 2), "%"))
## [1] "Misclassification Rate: 2.8 %"
Accuracy=1−MisclassificationRate
accuracy2_90 <- 1 - Misclassification_Rate2_90
print(paste("Accuracy:", round(accuracy2_90 * 100, 2), "%"))
## [1] "Accuracy: 97.2 %"
Interpretation:
Based on the dataset split into three different ratios splits and the model is fitted on training set and the misclassification rate is calculated on validation set.
Missclassification rate(validation estimate of the test error rate ) (Train:Test–80:20): 0.03
Missclassification rate(validation estimate of the test error rate ) (Train:Test–70:30): 0.0253
Missclassification rate(validation estimate of the test error rate ) (Train:Test–90:10): 0.028
From the above metric is understood that validation estimate error rate is nearly different based on the split ratio.
set.seed(123)
index_70_d <- createDataPartition(y = Default$default, p = 0.7, list = F)
train_set_70_d <- Default[index_70_d, ]
valid_set_70_d <- Default[-index_70_d, ]
nrow(train_set_70_d) / nrow(Default)
## [1] 0.7001
nrow(valid_set_70_d) / nrow(Default)
## [1] 0.2999
log_reg_splt_70_d<-glm(default~income+balance+student,family = binomial,data=train_set_70_d)
summary(log_reg_splt_70_d)
##
## Call:
## glm(formula = default ~ income + balance + student, family = binomial,
## data = train_set_70_d)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.048e+01 5.782e-01 -18.122 < 2e-16 ***
## income -5.519e-07 9.660e-06 -0.057 0.95444
## balance 5.638e-03 2.733e-04 20.628 < 2e-16 ***
## studentYes -8.937e-01 2.774e-01 -3.221 0.00128 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 2050.6 on 7000 degrees of freedom
## Residual deviance: 1128.8 on 6997 degrees of freedom
## AIC: 1136.8
##
## Number of Fisher Scoring iterations: 8
logreg_splt_pred_70_d<-predict(log_reg_splt_70_d,valid_set_70_d,type="response")
logreg_splt_pred_70_d.class<-ifelse(logreg_splt_pred_70_d>0.5,"Yes","No")
table(valid_set_70_d$default,logreg_splt_pred_70_d.class,dnn=c("Actual","Predicted"))
## Predicted
## Actual No Yes
## No 2884 16
## Yes 63 36
Misclassification_Rate2_70_d=round(mean(logreg_splt_pred_70_d.class!=valid_set_70_d$default),4)
print(Misclassification_Rate2_70_d)
## [1] 0.0263
print(paste("Misclassification Rate:", round(Misclassification_Rate2_70_d * 100, 2), "%"))
## [1] "Misclassification Rate: 2.63 %"
Accuracy=1−Misclassification_Rate2_70_d
accuracy2_70_d <- 1 - Misclassification_Rate2_70_d
print(paste("Accuracy:", round(accuracy2_70_d * 100, 2), "%"))
## [1] "Accuracy: 97.37 %"
Interpretation: Except for 70-30 ratio split of the dataset(slight increase in the error rate), all other ratios has slightly reduced(decreased) in test error rate when added a dummy variable student to the model, however, their isn’t much variation in the test error rate reduction.
set.seed(123)
logreg_6a<-glm(default~income+balance,family = binomial,data=Default)
summary(logreg_6a)
##
## Call:
## glm(formula = default ~ income + balance, family = binomial,
## data = Default)
##
## 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
Interpretation: From the above logistic regression summary the Std.Error co-efficients are obtained as 4.348e-01, 4.985e-06, and 2.274e-04 for beta0,beta1 and beta2 respectively.
boot.fn<-function(data,index){
boot_logreg_fit<-glm(default~income+balance,data=data,family="binomial",subset=index)
return(coef(boot_logreg_fit))
}
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 -2.754771e-02 4.204817e-01
## t2* 2.080898e-05 1.582518e-07 4.729534e-06
## t3* 5.647103e-03 1.296980e-05 2.217214e-04
Interpretation: The bootstrap estimates using the boot function of the standard errors for the coefficients of beta0,beta1,beta2 are respectively 4.204817e-01, 4.729534e-06, and 2.217214e-04.
Answer:
From the logistic regression summaries coefficients (beta0, beta1, and beta2), the predicted standard errors using the bootstrap method and the `glm()} function closely match, suggesting the bootstrap’s dependability in capturing coefficient fluctuation. By using a non-parametric resampling strategy, this consistency highlights the bootstrap approach’s robustness, particularly in small or complicated datasets(such as Default) where standard methods may not be as effective. As a result, confidence in coefficient estimates is increased.
attach(Boston)
mu<-mean(medv)
mu
## [1] 22.53281
Interpretation:
The estimated population mean (mu) of medv in the dataset is 22.53281.
SE<-sd(medv)/sqrt(nrow(Boston))
SE
## [1] 0.4088611
Interpretation: From the above , the sample mean estimate of medv in the Boston dataset is probably an approximation of the population mean, with relatively low variability around this estimate, according to the standard error of 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.007650791 0.4106622
Interpretation: The estimate of 0.4088611 obtained in (b) and the bootstrap estimated standard error of (mu) of 0.4106622 are both extremely very close. Demonstrating the consistency of the direct calculation method utilized in part (b) with the bootstrapped standard error estimation method.
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
CI_mu<-c(mu-2*0.4106622,mu+2*0.4106622)
CI_mu
## [1] 21.71148 23.35413
Interpretation: The confidence interval offered by the t.test() function and the bootstrapped one are very close intervals.
mu_med<-median(medv)
mu_med
## [1] 21.2
Interpretation: Median value of medv in the population is 21.2.
set.seed(123)
boot.fn<-function(data,index){
med_mu<-median(data[index])
return(med_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.0203 0.3676453
Interpretation: From the above median value is 21.2 which is equal to the estimated value obtained in question (e), with a standard error of 0.3676453 which is small when relatively compared to median value of 21.2.
quntl_mu<-quantile(medv,c(0.1))
quntl_mu
## 10%
## 12.75
Interpretation: The boston census tracts wth 10(tenth) percentile of medv is 12.75.
boot.fn <- function(data, index) {
quantile_mu <- quantile(data[index], c(0.1))
return (quantile_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.0071 0.5138566
Interpretation: From the above quantile value is 12.75 which is equal to the estimated value obtained in question (g), with a standard error of 0.5138566 which is small when relatively compared to median value of 12.75.