library(ISLR)
library(ISLR2)
##
## Attaching package: 'ISLR2'
## The following objects are masked from 'package:ISLR':
##
## Auto, Credit
basically, k-fold Cross-Validation is a resampling procedure used to evaluate machine learning models on a limited data sample. This procedure has has a single parameter k that refers to the number of groups that a give data sample is to be split into.
Advantages:
It uses more of the data for both training and testing, leading to more reliable estimate model perfromance
It uses all the data for both training and validation
Disadvantages:
Advantages: 1. For large datasets, k-fold cross-validation is less computationally intensive than LOOCV, it requires fewer model fits.
Disadvantages:
head(Default)
## default student balance income
## 1 No No 729.5265 44361.625
## 2 No Yes 817.1804 12106.135
## 3 No No 1073.5492 31767.139
## 4 No No 529.2506 35704.494
## 5 No No 785.6559 38463.496
## 6 No Yes 919.5885 7491.559
data("Default")
set.seed(123)
# Fit a logistic regression model
model <- glm(default ~ income + balance, data = Default, family = "binomial")
set.seed(123)
train_indices <- sample(1:nrow(Default), nrow(Default) * 0.7)
train_set <- Default[train_indices, ]
validation_set <- Default[-train_indices, ]
model <- glm(default ~ income + balance, data = train_set, family = "binomial")
predictions <- predict(model, validation_set, type = "response")
predicted_class <- ifelse(predictions > 0.5, "Yes", "No")
misclassified <- predicted_class != validation_set$default
validation_set_error <- sum(misclassified) / length(misclassified)
print(validation_set_error)
## [1] 0.02633333
data(Boston)
mu_hat <- mean(Boston$medv)
print(mu_hat)
## [1] 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.
s <- sd(Boston$medv)
n <- length(Boston$medv)
se_mu_hat <- s / sqrt(n)
print(se_mu_hat)
## [1] 0.4088611
Bootstrap estimated the standard error of μ^ as 0.4118401, where as standard error of sample mean is 0.4088611.
bootstrap_replicate <- function(data) {
bootstrap_sample <- sample(data, length(data), replace = TRUE)
return(mean(bootstrap_sample))
}
n_replicates <- 1000
bootstrap_means <- replicate(n_replicates, bootstrap_replicate(Boston$medv))
se_mu_hat_bootstrap <- sd(bootstrap_means)
print(se_mu_hat_bootstrap)
## [1] 0.4118401
Based on my test, the CE is 21.72953 - 23.33608 which is very similar to the bootstrp estimate 21.70913 - 23.35649.
ci_bootstrap_formula <- c(mu_hat - 2 * se_mu_hat_bootstrap, mu_hat + 2 * se_mu_hat_bootstrap)
print(ci_bootstrap_formula)
## [1] 21.70913 23.35649
ci_bootstrap_percentile <- quantile(bootstrap_means, c(0.025, 0.975))
print(ci_bootstrap_percentile)
## 2.5% 97.5%
## 21.75349 23.34298
t_test_result <- t.test(Boston$medv)
print(t_test_result$conf.int)
## [1] 21.72953 23.33608
## attr(,"conf.level")
## [1] 0.95
mu_hat_med <- median(Boston$medv)
print(mu_hat_med)
## [1] 21.2
According to my results, I see that my standard error is 0.3723012 which means if I wanted to repeatedly sample from the population and calculate the median medv for each sample, the distribution of those medians would have a standard deviation of around $372.30.
bootstrap_median <- function(data) {
bootstrap_sample <- sample(data, length(data), replace = TRUE)
return(median(bootstrap_sample))
}
n_replicates <- 1000
bootstrap_medians <- replicate(n_replicates, bootstrap_median(Boston$medv))
se_mu_hat_med_bootstrap <- sd(bootstrap_medians)
print(se_mu_hat_med_bootstrap)
## [1] 0.3723012
mu_hat_0_1 <- quantile(Boston$medv, 0.1)
print(mu_hat_0_1)
## 10%
## 12.75
My estimated standard error of the tench percentile is 0.4969654. This value shows us the uncertainty around the estimate of the 10th percentile of the median value of owner-occupied homes due to sampling variability.
bootstrap_tenth_percentile <- function(data) {
bootstrap_sample <- sample(data, length(data), replace = TRUE)
return(quantile(bootstrap_sample, 0.1))
}
n_replicates <- 1000
bootstrap_tenth_percentiles <- replicate(n_replicates, bootstrap_tenth_percentile(Boston$medv))
se_mu_hat_0_1_bootstrap <- sd(bootstrap_tenth_percentiles)
print(se_mu_hat_0_1_bootstrap)
## [1] 0.4969654