5 Resampling Methods
5.1 Conceptual
5.1.1 Question 1
Using basic statistical properties of the variance, as well as single- variable calculus, derive (5.6). In other words, prove that \(\alpha\) given by (5.6) does indeed minimize \(Var(\alpha X + (1 − \alpha)Y)\).
5.1.2 Question 2
We will now derive the probability that a given observation is part of a bootstrap sample. Suppose that we obtain a bootstrap sample from a set of n observations.
What is the probability that the first bootstrap observation is not the \(j\)th observation from the original sample? Justify your answer.
What is the probability that the second bootstrap observation is not the \(j\)th observation from the original sample?
Argue that the probability that the \(j\)th observation is not in the bootstrap sample is \((1 − 1/n)^n\).
When \(n = 5\), what is the probability that the \(j\)th observation is in the bootstrap sample?
When \(n = 100\), what is the probability that the \(j\)th observation is in the bootstrap sample?
When \(n = 10,000\), what is the probability that the \(j\)th observation is in the bootstrap sample?
Create a plot that displays, for each integer value of \(n\) from 1 to 100,000, the probability that the \(j\)th observation is in the bootstrap sample. Comment on what you observe.
We will now investigate numerically the probability that a bootstrap sample of size \(n = 100\) contains the \(j\)th observation. Here \(j = 4\). We repeatedly create bootstrap samples, and each time we record whether or not the fourth observation is contained in the bootstrap sample.
> store <- rep (NA, 10000) > for (i in 1:10000) { store[i] <= sum(sample(1:100, rep = TRUE) == 4) > 0 } > mean(store)
Comment on the results obtained.
5.2 Applied
5.2.1 Question 5
In Chapter 4, we used logistic regression to predict the probability of
default
usingincome
andbalance
on theDefault
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.
Fit a logistic regression model that uses
income
andbalance
to predictdefault
.Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps:
- Split the sample set into a training set and a validation set.
- Fit a multiple logistic regression model using only the training observations.
- 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.- Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
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.
Now consider a logistic regression model that predicts the probability of
default
usingincome
,balance
, and a dummy variable forstudent
. Estimate the test error for this model using the validation set approach. Comment on whether or not including a dummy variable forstudent
leads to a reduction in the test error rate.
5.2.2 Question 6
We continue to consider the use of a logistic regression model to predict the probability of
default
usingincome
andbalance
on theDefault
data set. In particular, we will now compute estimates for the standard errors of theincome
andbalance
logistic regression coefficients in two different ways: (1) using the bootstrap, and (2) using the standard formula for computing the standard errors in theglm()
function. Do not forget to set a random seed before beginning your analysis.
Using the
summary()
andglm()
functions, determine the estimated standard errors for the coefficients associated withincome
andbalance
in a multiple logistic regression model that uses both predictors.Write a function,
boot.fn()
, that takes as input theDefault
data set as well as an index of the observations, and that outputs the coefficient estimates forincome
andbalance
in the multiple logistic regression model.Use the
boot()
function together with yourboot.fn()
function to estimate the standard errors of the logistic regression coefficients for income and balance.Comment on the estimated standard errors obtained using the
glm()
function and using your bootstrap function.
5.2.3 Question 7
In Sections 5.3.2 and 5.3.3, we saw that the
cv.glm()
function can be used in order to compute the LOOCV test error estimate. Alternatively, one could compute those quantities using just theglm()
andpredict.glm()
functions, and a for loop. You will now take this approach in order to compute the LOOCV error for a simple logistic regression model on theWeekly
data set. Recall that in the context of classification problems, the LOOCV error is given in (5.4).
Fit a logistic regression model that predicts
Direction
usingLag1
andLag2
.Fit a logistic regression model that predicts
Direction
usingLag1
andLag2
using all but the first observation.Use the model from (b) to predict the direction of the first observation. You can do this by predicting that the first observation will go up if \(P(\)
Direction="Up" | Lag1 , Lag2
\() > 0.5\). Was this observation correctly classified?Write a for loop from \(i = 1\) to \(i = n\), where \(n\) is the number of observations in the data set, that performs each of the following steps:
- Fit a logistic regression model using all but the \(i\)th observation to predict
Direction
usingLag1
andLag2
.- Compute the posterior probability of the market moving up for the \(i\)th observation.
- Use the posterior probability for the \(i\)th observation in order to predict whether or not the market moves up.
- Determine whether or not an error was made in predicting the direction for the \(i\)th observation. If an error was made, then indicate this as a 1, and otherwise indicate it as a 0.
Take the average of the \(n\) numbers obtained in (d) in order to obtain the LOOCV estimate for the test error. Comment on the results.
5.2.4 Question 8
We will now perform cross-validation on a simulated data set.
Generate a simulated data set as follows:
In this data set, what is \(n\) and what is \(p\)? Write out the model used to generate the data in equation form.
Create a scatterplot of \(X\) against \(Y\). Comment on what you find.
Set a random seed, and then compute the LOOCV errors that result from fitting the following four models using least squares:
- \(Y = \beta_0 + \beta_1 X + \epsilon\)
- \(Y = \beta_0 + \beta_1 X + \beta_2 X^2 + \epsilon\)
- \(Y = \beta_0 + \beta_1 X + \beta_2 X^2 + \beta_3 X^3 + \epsilon\)
- \(Y = \beta_0 + \beta_1 X + \beta_2 X^2 + \beta_3 X^3 + \beta_4 X^4 + \epsilon\).
Note you may find it helpful to use the
data.frame()
function to create a single data set containing both \(X\) and \(Y\).Repeat (c) using another random seed, and report your results. Are your results the same as what you got in (c)? Why?
Which of the models in (c) had the smallest LOOCV error? Is this what you expected? Explain your answer.
Comment on the statistical significance of the coefficient estimates that results from fitting each of the models in (c) using least squares. Do these results agree with the conclusions drawn based on the cross-validation results?
5.2.5 Question 9
We will now consider the
Boston
housing data set, from theISLR2
library.
Based on this data set, provide an estimate for the population mean of
medv
. Call this estimate \(\hat\mu\).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.
Now estimate the standard error of \(\hat\mu\) using the bootstrap. How does this compare to your answer from (b)?
Based on your bootstrap estimate from (c), provide a 95% confidence interval for the mean of
medv
. Compare it to the results obtained usingt.test(Boston$medv)
.Hint: You can approximate a 95% confidence interval using the formula \([\hat\mu − 2SE(\hat\mu), \hat\mu + 2SE(\hat\mu)].\)
Based on this data set, provide an estimate, \(\hat\mu_{med}\), for the median value of
medv
in the population.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.
Based on this data set, provide an estimate for the tenth percentile of
medv
in Boston census tracts. Call this quantity \(\hat\mu_{0.1}\). (You can use thequantile()
function.)Use the bootstrap to estimate the standard error of \(\hat\mu_{0.1}\). Comment on your findings.