We now review k-fold cross-validation.
(a) Explain how k-fold cross-validation is implemented.
K-Fold Cross Validation involves randomly dividing the set of observations into k groups or folds of approximately equal size. With this method, the first fold is treated as a validation set, and the method is fit on the remaining k-1 folds. The mean squared error, MSE, is then computed on observations in the held-out fold. This procedures is repeated times; each time, a different group of observations is treated as a validation set. This process results in the k estimates of the test error, \(MSE_1\), \(MSE_2\),… \(MSE_k\). The k-fold CV estimate is computed by averaging the values of the MSEs.
(b) What are the advantages and disadvantages of k-fold crossvalidation relative to:
i. The validation set approach? The validation set appraoch is conceptually simple and is easy to implement. However, the validation set approach has two key drawbacks when compared to k-fold cross- validation. First, validation test error estimate can be highly variable in comparision to CV estimates from the k-fold approach. Second, only a subset of observations–those that are included in the training set rather than in the validation set–are used to fit the model. Since statistical methods tend to perform worse when trained on a fewer observations, this suggests that the validation set error rate may overestimate the test error rate for the model fit on the entire data set.
ii. LOOCV? K-Folds Cross Validation is computationally less expensive than LOOCV as LOOCV requires fitting the statistically learning method n times. Additionally, it often gives more accurate estimates of the test error rate than does LOOCV. On the other hand, LOOCV is preferred to K-Fold CV in the perspective of bias reduction.
In Chapter 4, we used logistic regression to predict the probability of 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.
library(ISLR)
set.seed(12)
attach(Default)
summary(Default)
## default student balance income
## No :9667 No :7056 Min. : 0.0 Min. : 772
## Yes: 333 Yes:2944 1st Qu.: 481.7 1st Qu.:21340
## Median : 823.6 Median :34553
## Mean : 835.4 Mean :33517
## 3rd Qu.:1166.3 3rd Qu.:43808
## Max. :2654.3 Max. :73554
str(Default)
## 'data.frame': 10000 obs. of 4 variables:
## $ default: Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
## $ student: Factor w/ 2 levels "No","Yes": 1 2 1 1 1 2 1 2 1 1 ...
## $ balance: num 730 817 1074 529 786 ...
## $ income : num 44362 12106 31767 35704 38463 ...
(a) Fit a logistic regression model that uses income and balance to predict default.
By fitting a Logistic Regression model on the entire dataset, we can see that both balance and income have a real association with the outcome/ predictor variable, Default as the p-values are less than 0.05.
default.glm = glm(default ~ balance + income, data = Default, family = "binomial")
summary(default.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
The confusion matrix below shows that the Logistic Regression modeled on the whole dataset performs relatively well on predicting the outcome variable. Since these results were implemented on the entire dataset instead of a training and testing dataset, its a little optimistic with regard to the error rate.
For this Logistic Regression run, our model correctly predicts that the individual defaults 108 times and does not default 9,629 times - in total there are 9,737 correct predictions. The below percentages are the Accuracy Rate, Missclassification Rate, Sensitivity (true positive rate), and Specificity (true negative rate). While the prediction accuracy is relatively high, the prediction may not hold true on outside datasets.
Accuracy Rate - 9629+108/10000 = 97.4% Missclassification - 38+225/10000 = 2.6% Sensitivity - 108/333 = 32% Specificity - 9629/(38+9629) = 100%
default.probs = predict(default.glm, type = 'response')
default.pred = rep("No", 10000)
default.pred[default.probs > 0.5] = "Yes"
table(default.pred, default)
## default
## default.pred No Yes
## No 9629 225
## Yes 38 108
mean(default.pred==default)
## [1] 0.9737
mean(default.pred!=default)
## [1] 0.0263
(b) Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:
i. Split the sample set into a training set and a validation set.
set.seed(1)
default.train = sample(10000, 5000)
ii. Fit a multiple logistic regression model using only the training observations.
Being fit on only half of the data, both income and balance remain significant.
default.glm2 = glm(default ~ income + balance, data = Default, family = "binomial", subset = default.train)
summary(default.glm2)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = default.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.
default.probs2 = predict(default.glm2, newdata = Default[-default.train,], type = "response")
default.pred2 = ifelse(default.probs2> 0.5, "Yes", "No")
iv. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
The Validation Set Error Rate is 0.0254 or 2.54%. This means that defaults are misclassified 2.54% of the time. In comparison to the model fitted on the entire dataset, the Validation Set Model’s test error rate is lower by 0.0009.
# Accuracy Rate #
mean(Default[-default.train,]$default == default.pred2)
## [1] 0.9746
# Misclassification Rate #
mean(Default[-default.train,]$default != default.pred2)
## [1] 0.0254
(c) Repeat the process in (b) three times, using three different splits of the observations into a training set and a validation set. Comment on the results obtained.
set.seed(12)
default.train2 = sample(10000, 7500)
default.glm3 = glm(default ~ income + balance, data = Default, family = "binomial", subset = default.train2)
summary(default.glm3)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = default.train2)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4958 -0.1341 -0.0518 -0.0184 3.7794
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.186e+01 5.308e-01 -22.343 < 2e-16 ***
## income 1.987e-05 5.939e-06 3.346 0.000819 ***
## balance 5.825e-03 2.764e-04 21.073 < 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: 2110.8 on 7499 degrees of freedom
## Residual deviance: 1117.6 on 7497 degrees of freedom
## AIC: 1123.6
##
## Number of Fisher Scoring iterations: 8
With a 3:4 split, we achieved a slightly higher test error rate of 0.03. This means that the defaults were misclassified 3.0% of the time. In comparison to the 1:2 split, this model performs slightly worse.
default.probs3 = predict(default.glm3, newdata = Default[-default.train2,], type = "response")
default.pred3 = ifelse(default.probs3> 0.5, "Yes", "No")
# Accuracy Rate #
mean(Default[-default.train2,]$default == default.pred3)
## [1] 0.97
# Misclassification Rate #
mean(Default[-default.train2,]$default != default.pred3)
## [1] 0.03
One thing that is noticably different is that the training split has made income an insignificant predictor of default.
set.seed(12)
default.train3 = sample(10000, 2500)
default.glm4 = glm(default~ income + balance, data = Default, family = "binomial", subset = default.train3)
summary(default.glm4)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = default.train3)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.54281 -0.12193 -0.04169 -0.01300 3.01977
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.236e+01 9.748e-01 -12.68 <2e-16 ***
## income 9.000e-06 1.058e-05 0.85 0.395
## balance 6.315e-03 5.151e-04 12.26 <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: 735.21 on 2499 degrees of freedom
## Residual deviance: 357.96 on 2497 degrees of freedom
## AIC: 363.96
##
## Number of Fisher Scoring iterations: 9
When training the Logistic Regression model on a much smaller data set, we get a test error rate of 0.0272. This means that 2.72% of the defaults are misclassified with this model.
default.probs4 = predict(default.glm4, newdata = Default[-default.train3,], type = "response")
default.pred4 = ifelse(default.probs4> 0.5, "Yes", "No")
# Accuracy Rate #
mean(Default[-default.train3,]$default == default.pred4)
## [1] 0.9728
# Misclassification Rate #
mean(Default[-default.train3,]$default != default.pred4)
## [1] 0.0272
Similar to the last iteration, the split at 1000 observations results in income becoming an insignificant predictor of default.
set.seed(12)
default.train5 = sample(10000, 1000)
default.glm5 = glm(default ~ income + balance, data = Default, family = "binomial", subset = default.train5)
summary(default.glm5)
##
## Call:
## glm(formula = default ~ income + balance, family = "binomial",
## data = Default, subset = default.train5)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.66042 -0.09849 -0.02897 -0.00782 2.82326
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.404e+01 1.870e+00 -7.509 5.96e-14 ***
## income 1.501e-05 1.817e-05 0.826 0.409
## balance 7.303e-03 9.629e-04 7.584 3.34e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 310.03 on 999 degrees of freedom
## Residual deviance: 134.29 on 997 degrees of freedom
## AIC: 140.29
##
## Number of Fisher Scoring iterations: 9
When training the Logistic Regression model on a much smaller data set, we get a test error rate of 0.0281. This means that 2.81% of the defaults are misclassified with this model.
default.probs5 = predict(default.glm5, newdata = Default[-default.train5,], type = "response")
default.pred5 = ifelse(default.probs5> 0.5, "Yes", "No")
# Accuracy Rate #
mean(Default[-default.train5,]$default == default.pred5)
## [1] 0.9718889
# Misclassification Rate #
mean(Default[-default.train5,]$default != default.pred5)
## [1] 0.02811111
Out of all of the 4 splits (1:2, 3:4, 1:4, 1:10), we achieve the lowest error rate at the 1:2 split.
(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. Comment on whether or not including a dummy variable for student leads to a reduction in the test error rate.
With the inclusion of the Student dummy variable, the only variable that remains significant is balance.
set.seed(12)
student.train = sample(10000, 5000)
default.student.glm = glm(default ~ income + balance + student, data = Default, family = "binomial", subset = student.train)
summary(default.student.glm)
##
## Call:
## glm(formula = default ~ income + balance + student, family = "binomial",
## data = Default, subset = student.train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.6033 -0.1249 -0.0440 -0.0147 3.8793
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.200e+01 7.702e-01 -15.578 <2e-16 ***
## income 8.800e-06 1.229e-05 0.716 0.474
## balance 6.236e-03 3.624e-04 17.207 <2e-16 ***
## studentYes -2.685e-01 3.571e-01 -0.752 0.452
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1429.88 on 4999 degrees of freedom
## Residual deviance: 717.82 on 4996 degrees of freedom
## AIC: 725.82
##
## Number of Fisher Scoring iterations: 8
With the Student dummy variable, we receive a test error rate of 0.0268. This means that 2.68% of the time defaults are misclassified when considering income, balance, and whether a person is a student or not. Using the Validation Set Approach and adding the student dummy variable seems to lead in a slight reduction in the test error rate, but not significantly.
default.student.probs = predict(default.student.glm, newdata = Default[-student.train,], type = "response")
default.student.pred = ifelse(default.student.probs > 0.5, "Yes", "No")
# Accuracy Rate #
mean(Default[-student.train,]$default == default.student.pred)
## [1] 0.9732
# Misclassification Rate #
mean(Default[-student.train,]$default != default.student.pred)
## [1] 0.0268
We continue to consider the use of a logistic regression model to predict the probability of 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.
library(ISLR)
summary(Default)
## default student balance income
## No :9667 No :7056 Min. : 0.0 Min. : 772
## Yes: 333 Yes:2944 1st Qu.: 481.7 1st Qu.:21340
## Median : 823.6 Median :34553
## Mean : 835.4 Mean :33517
## 3rd Qu.:1166.3 3rd Qu.:43808
## Max. :2654.3 Max. :73554
(a) Using the 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.
The Logistic Regression Model estimates the standard errors for the coefficients associated with income and balance (\(\beta_1\) & \(\beta_2\)) as 0.000004985 and 0.0002274 respecitvely.
set.seed(12)
def.glm = glm(default ~ income + balance, data = Default, family = "binomial")
summary(def.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
(b) Write a function, 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.
default.boot.fn = function(Default, index) return(coef(glm(default ~ income + balance, data = Default, family = "binomial", subset = index)))
(c) Use the boot() function together with your boot.fn() function to estimate the standard errors of the logistic regression coefficients for income and balance.
For the below code, we use the boot() function to compute the standard errors of 50 bootstrap estimates for the coefficients for income and balance. The bootstrap estimates the standard errors for the coefficients for the income (t2) and balance (t3) as 0.0000050395 and 0.00022640.
library(boot)
boot(Default, default.boot.fn, 50)
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = Default, statistic = default.boot.fn, R = 50)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* -1.154047e+01 -1.028862e-01 4.691623e-01
## t2* 2.080898e-05 5.561945e-07 5.039478e-06
## t3* 5.647103e-03 4.857587e-05 2.246402e-04
(d) Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.
The estimated standard errors obtained using the glm() function and bootstrap function are extremely similar. The difference between the standard errors for the coefficients are only off by 0.000000054478 for income & 0.0000027598 for balance.
detach(Default)
We will now consider the Boston housing data set, from the MASS library.
library(MASS)
summary(Boston)
## crim zn indus chas
## Min. : 0.00632 Min. : 0.00 Min. : 0.46 Min. :0.00000
## 1st Qu.: 0.08204 1st Qu.: 0.00 1st Qu.: 5.19 1st Qu.:0.00000
## Median : 0.25651 Median : 0.00 Median : 9.69 Median :0.00000
## Mean : 3.61352 Mean : 11.36 Mean :11.14 Mean :0.06917
## 3rd Qu.: 3.67708 3rd Qu.: 12.50 3rd Qu.:18.10 3rd Qu.:0.00000
## Max. :88.97620 Max. :100.00 Max. :27.74 Max. :1.00000
## nox rm age dis
## Min. :0.3850 Min. :3.561 Min. : 2.90 Min. : 1.130
## 1st Qu.:0.4490 1st Qu.:5.886 1st Qu.: 45.02 1st Qu.: 2.100
## Median :0.5380 Median :6.208 Median : 77.50 Median : 3.207
## Mean :0.5547 Mean :6.285 Mean : 68.57 Mean : 3.795
## 3rd Qu.:0.6240 3rd Qu.:6.623 3rd Qu.: 94.08 3rd Qu.: 5.188
## Max. :0.8710 Max. :8.780 Max. :100.00 Max. :12.127
## rad tax ptratio black
## Min. : 1.000 Min. :187.0 Min. :12.60 Min. : 0.32
## 1st Qu.: 4.000 1st Qu.:279.0 1st Qu.:17.40 1st Qu.:375.38
## Median : 5.000 Median :330.0 Median :19.05 Median :391.44
## Mean : 9.549 Mean :408.2 Mean :18.46 Mean :356.67
## 3rd Qu.:24.000 3rd Qu.:666.0 3rd Qu.:20.20 3rd Qu.:396.23
## Max. :24.000 Max. :711.0 Max. :22.00 Max. :396.90
## lstat medv
## Min. : 1.73 Min. : 5.00
## 1st Qu.: 6.95 1st Qu.:17.02
## Median :11.36 Median :21.20
## Mean :12.65 Mean :22.53
## 3rd Qu.:16.95 3rd Qu.:25.00
## Max. :37.97 Max. :50.00
str(Boston)
## 'data.frame': 506 obs. of 14 variables:
## $ crim : num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
## $ zn : num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
## $ indus : num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
## $ chas : int 0 0 0 0 0 0 0 0 0 0 ...
## $ nox : num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
## $ rm : num 6.58 6.42 7.18 7 7.15 ...
## $ age : num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
## $ dis : num 4.09 4.97 4.97 6.06 6.06 ...
## $ rad : int 1 2 2 3 3 3 5 5 5 5 ...
## $ tax : num 296 242 242 222 222 222 311 311 311 311 ...
## $ ptratio: num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
## $ black : num 397 397 393 395 397 ...
## $ lstat : num 4.98 9.14 4.03 2.94 5.33 ...
## $ medv : num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
attach(Boston)
(a) Based on this data set, provide an estimate for the population mean of medv. Call this estimate \(\hat{\mu}\).
medv.mean = mean(Boston$medv)
medv.mean
## [1] 22.53281
(b) Provide an estimate of the standard error of \(\hat{\mu}\). Interpret this result. 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.
The standard error for mean of the medv is 0.4089. The standard error indicates how precise (or not) an estimate of the population parameter the sample statistic is. The standard error for mean tells us that the estimate sample mean (\(\hat{\mu}\)) differs by +/- 0.4089 from the actual value of the population mean (\(\mu\)).
medv.stderr = sd(Boston$medv)/sqrt(length(Boston$medv))
medv.stderr
## [1] 0.4088611
(c) Now estimate the standard error of \(\hat{\mu}\) using the bootstrap. How does this compare to your answer from (b)?
The Bootstrap and manually calculatied Standard Error are very close. The manually calculated Standard Error is only 0.0036426 higher than the Bootstrap Standard Error.
library(boot)
medv.boot.fn = function(Boston, index){
return(mean(Boston[index]))
}
medv.bootstrap = boot(medv, medv.boot.fn, 1000)
medv.bootstrap
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = medv.boot.fn, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 22.53281 -0.02192569 0.4211865
(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). Hint: You can approximate a 95% confidence interval using the formula [\)$ − 2SE(\(\hat{\mu}\)), \(\hat{\mu}\) + 2SE(\(\hat{\mu}\))].
The two confidence intervals from the T-test & Bootstrap are pretty similar in nature. The T-test Confidence Interval estimate is only 0.00716 higher on the lower bound than the Bootstrap estimates and 0.00716 lower on the upper bound than the Bootstrap estimates.
medv.confint.ttest = t.test(medv)
medv.confint.ttest
##
## 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
medv.confint.bootstrap = c(medv.bootstrap$t0-2*0.4052185, medv.bootstrap$t0+2*0.4052185)
medv.confint.bootstrap
## [1] 21.72237 23.34324
(e) Based on this data set, provide an estimate, \(\hat{\mu}_{med}\), for the median value of medv in the population.
medv.median = median(Boston$medv)
medv.median
## [1] 21.2
(f) We now would like to estimate the standard error of \(\hat{\mu}_{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.
The standard error for median of the medv is 0.3742. The standard error indicates how precise (or not) an estimate of the population parameter the sample statistic is. The standard error for median tells us that the estimate sample median (\(\hat{\mu}\)) differs by +/- 0.3742 from the actual value of the population median (\(\mu\)).
medv.boot.fn2 = function(Boston, index){
return(median(Boston[index]))
}
medv.med.bootstrap = boot(medv, medv.boot.fn2, 1000)
medv.med.bootstrap
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = medv.boot.fn2, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 21.2 -0.0011 0.381063
(g) Based on this data set, provide an estimate for the tenth percentile of medv in Boston suburbs. Call this quantity \(\hat{\mu}_{0.1}\). (You can use the quantile() function.)
The estimate for the tenth percentile of medv \(\hat{\mu}_{0.1}\) in Boston suburbs is 12.75.
medv.tenth = quantile(Boston$medv, c(0.1))
medv.tenth
## 10%
## 12.75
(h) Use the bootstrap to estimate the standard error of \(\hat{\mu}_{0.1}\). Comment on your findings.
When using the quantile via the Bootstrap method, I receive the same estimate of 12.75 for the tenth percentile of medv in the Boston suburbs.
medv.boot.fn3 = function(Boston, index){
return(quantile(Boston[index], c(0.1)))
}
medv.tenth.bootstrap = boot(medv, medv.boot.fn3, 1000)
medv.tenth.bootstrap
##
## ORDINARY NONPARAMETRIC BOOTSTRAP
##
##
## Call:
## boot(data = medv, statistic = medv.boot.fn3, R = 1000)
##
##
## Bootstrap Statistics :
## original bias std. error
## t1* 12.75 0.04845 0.4940589
detach(Boston)