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 ).
Answer:
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.
(a) What is the probability that the first bootstrap observation is not the \(j\)th observation from the original sample? Justify your answer.
Answer: \(1 - \frac{1}{n}\)
(b) What is the probability that the second bootstrap observation is not the \(j\)th observation from the original sample?
Answer: Due to the fact that sampling is performed with replacement, we have the same answer as in (a) \(1 - \frac{1}{n}\)
(c) Argue that the probability that the \(j\)th observation is not in the bootstrap sample is \((1 − 1/n)^n\).
Answer: Since we deal with independent probabilities, the probability that the \(j\)th observation is not in the sample is the product of probabilities of each observation not being in the sample.
(d) When n = 5, what is the probability that the \(j\)th observation is in the bootstrap sample?
Answer: We use the procedure below to find:
percent(1-(1-1/5)^5)
## [1] "67.2%"
(e) When n = 100, what is the probability that the jth observation is in the bootstrap sample?
Answer: We use the procedure below to find:
percent(1-(1-1/100)^100)
## [1] "63.4%"
(f) When n = 10, 000, what is the probability that the jth observation is in the bootstrap sample?
Answer: We use the procedure below to find:
percent(1-(1-1/10000)^10000)
## [1] "63.2%"
(g) Create a plot that displays, for each integer value of n from 1 to 100, 000, the probability that the jth observation is in the bootstrap sample. Comment on what you observe.
Answer: We use the code below to create:
x <- 1:100000
y <- 1-(1-1/x)^x
ggplot() +
geom_point(aes(x, y))
Probability quickly reaches the values calculated in (e) and (f).
(h) 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.
Answer: We use the code below to obtain the results:
store=rep (NA , 10000)
for (i in 1:10000) {
store[i]=sum(sample (1:100 , rep =TRUE)==4) >0
}
mean(store)
## [1] 0.6365
which is close to what we calculate with formula in (f): 63.2%
We now review k-fold cross-validation.
(a) Explain how k-fold cross-validation is implemented.
Answer: This approach involves randomly dividing the set of observations into k groups, or folds, of approximately equal size. The first fold is treated as a validation set, and the method is fit on the remaining k − 1 folds. (book_ref. 5.1.3)
(b) What are the advantages and disadvantages of k-fold cross validation relative to:
(i) The validation set approach?
Answer: The advantages are:
1. Lower variability;
2. More accurate test error estimates.
The disadvantages are:
1. Computational.
(ii) LOOCV?
Answer: The advantages are:
1. Computational;
2. More accurate test error estimates (book_ref. 5.1.4).
The disadvantages are:
1. Higher variability.
Suppose that we use some statistical learning method to make a prediction for the response Y for a particular value of the predictor X. Carefully describe how we might estimate the standard deviation of our prediction.
Answer: We can use the bootstrap approach. First, we predict B-number of times with replacement Y for particular values of X. Second, we compute the SE of the estimates by using formula (5.8)
rm(list = ls())
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.
set.seed(123)
(a) Fit a logistic regression model that uses income
and balance
to predict default
.
Answer: Following is the fit:
logreg_mod <- logistic_reg()
logreg_fit <-
logreg_mod %>%
set_engine("glm") %>%
fit(default ~ income + balance, data = Default)
logreg_fit$fit %>% broom::tidy()
## # A tibble: 3 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -11.5 0.435 -26.5 2.96e-155
## 2 income 0.0000208 0.00000499 4.17 2.99e- 5
## 3 balance 0.00565 0.000227 24.8 3.64e-136
(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.
Answer: Following is the split:
DefaultSplit <- initial_split(Default)
(ii) Fit a multiple logistic regression model using only the training observations.
Answer: Following is the fit:
logreg_fit_ii <-
logreg_mod %>%
set_engine("glm") %>%
fit(default ~ income + balance, data = training(DefaultSplit))
logreg_fit_ii$fit %>% broom::tidy()
## # A tibble: 3 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -11.3 0.488 -23.2 5.41e-119
## 2 income 0.0000232 0.00000567 4.08 4.45e- 5
## 3 balance 0.00547 0.000254 21.6 4.61e-103
(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.
Answer: Following is the prediction:
logreg_preds <- predict(logreg_fit_ii, type = "prob", testing(DefaultSplit)) %>%
mutate(.pred_default = as_factor(ifelse(.pred_Yes > 0.5, "Yes", "No")))
logreg_preds
## # A tibble: 2,500 x 3
## .pred_No .pred_Yes .pred_default
## <dbl> <dbl> <fct>
## 1 0.998 0.00218 No
## 2 0.998 0.00198 No
## 3 1.000 0.0000202 No
## 4 0.987 0.0130 No
## 5 0.995 0.00475 No
## 6 0.999 0.000878 No
## 7 0.953 0.0473 No
## 8 0.943 0.0570 No
## 9 1.000 0.000187 No
## 10 0.998 0.00170 No
## # ... with 2,490 more rows
(iv) Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.
Answer: Following is the computation:
valid_set_error <- logreg_preds %>%
bind_cols(testing(DefaultSplit)) %>%
select(.pred_default, default) %>%
mutate(valid = if_else(.pred_default == default, 1, 0)) %>%
summarise(
total_valid = sum(valid),
total = n(),
valid_set_error = total_valid/total
) %>%
pull(valid_set_error)
percent(1-valid_set_error)
## [1] "2.12%"
(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.
rm(list = ls())
Answer: We repeat the process, defining different seeds, in the following code:
set.seed(111)
logreg_mod <- logistic_reg()
DefaultSplit_1st <- initial_split(Default, prop = 1/2)
train_1st <- training(DefaultSplit_1st)
test_1st <- testing(DefaultSplit_1st)
logreg_fit_1st <-
logreg_mod %>%
set_engine("glm") %>%
fit(default ~ income + balance, data = train_1st)
logreg_preds_1st <- predict(logreg_fit_1st, type = "prob", test_1st) %>%
mutate(.pred_default = as_factor(ifelse(.pred_Yes > 0.5, "Yes", "No")))
vseterror_1st <- logreg_preds_1st %>%
bind_cols(test_1st) %>%
select(.pred_default, default) %>%
mutate(valid = if_else(.pred_default == default, 1, 0)) %>%
summarise(
total_valid = sum(valid),
total = n(),
valid_set_error = total_valid/total
) %>%
pull(valid_set_error)
percent(1-vseterror_1st)
## [1] "2.68%"
set.seed(222)
logreg_mod <- logistic_reg()
DefaultSplit_2nd <- initial_split(Default, prop = 1/2)
train_2nd <- training(DefaultSplit_2nd)
test_2nd <- testing(DefaultSplit_2nd)
logreg_fit_2nd <-
logreg_mod %>%
set_engine("glm") %>%
fit(default ~ income + balance, data = train_2nd)
logreg_preds_2nd <- predict(logreg_fit_2nd, type = "prob", test_2nd) %>%
mutate(.pred_default = as_factor(ifelse(.pred_Yes > 0.5, "Yes", "No")))
vseterror_2nd <- logreg_preds_2nd %>%
bind_cols(test_2nd) %>%
select(.pred_default, default) %>%
mutate(valid = if_else(.pred_default == default, 1, 0)) %>%
summarise(
total_valid = sum(valid),
total = n(),
valid_set_error = total_valid/total
) %>%
pull(valid_set_error)
percent(1-vseterror_2nd)
## [1] "2.54%"
set.seed(333)
logreg_mod <- logistic_reg()
DefaultSplit_3rd <- initial_split(Default, prop = 1/2)
train_3rd <- training(DefaultSplit_3rd)
test_3rd <- testing(DefaultSplit_3rd)
logreg_fit_3rd <-
logreg_mod %>%
set_engine("glm") %>%
fit(default ~ income + balance, data = train_3rd)
logreg_preds_3rd <- predict(logreg_fit_3rd, type = "prob", test_3rd) %>%
mutate(.pred_default = as_factor(ifelse(.pred_Yes > 0.5, "Yes", "No")))
vseterror_3rd <- logreg_preds_3rd %>%
bind_cols(test_3rd) %>%
select(.pred_default, default) %>%
mutate(valid = if_else(.pred_default == default, 1, 0)) %>%
summarise(
total_valid = sum(valid),
total = n(),
valid_set_error = total_valid/total
) %>%
pull(valid_set_error)
percent(1-vseterror_3rd)
## [1] "2.60%"
Validation test error rate varies depending on the split.
rm(list = ls())
(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.
Answer: Following is the code for estimation of the test error:
set.seed(123)
logreg_mod <- logistic_reg()
DefaultSplit <- initial_split(Default)
training <- training(DefaultSplit)
testing <- testing(DefaultSplit)
logreg_fit <-
logreg_mod %>%
set_engine("glm") %>%
fit(default ~ income + balance + student, data = training)
logreg_preds <- predict(logreg_fit, type = "prob", testing) %>%
mutate(.pred_default = as_factor(ifelse(.pred_Yes > 0.5, "Yes", "No")))
vseterror <- logreg_preds %>%
bind_cols(testing) %>%
select(.pred_default, default) %>%
mutate(valid = if_else(.pred_default == default, 1, 0)) %>%
summarise(
total_valid = sum(valid),
total = n(),
valid_set_error = total_valid/total
) %>%
pull(valid_set_error)
percent(1-vseterror)
## [1] "2.28%"
Adding student
doesn’t reduce the validation test error.
rm(list = ls())
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.
(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.
Answer: Following is the code for estimation of the std.error
logreg_mod <- logistic_reg()
logreg_fit <-
logreg_mod %>%
set_engine("glm") %>%
fit(default ~ income + balance, data = Default)
summary_betas <-
logreg_fit$fit %>%
broom::tidy() %>%
arrange(term) %>%
select(std.error) %>%
transmute(
std.error.summary = std.error
)
logreg_fit$fit %>%
broom::tidy() %>%
arrange(term)
## # A tibble: 3 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -11.5 0.435 -26.5 2.96e-155
## 2 balance 0.00565 0.000227 24.8 3.64e-136
## 3 income 0.0000208 0.00000499 4.17 2.99e- 5
(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.
(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.
Answer (b), (c): Following is the estimation of the std.errors:
set.seed(1)
logreg_mod <- logistic_reg()
logreg_fit <-
logreg_mod %>%
set_engine("glm")
bt_resamples <- bootstraps(Default, times = 250)
mod_form <- as.formula(default ~ income + balance)
glm_coefs <- function(splits, ...) {
mod <- logreg_fit %>% fit(..., data = analysis(splits))
mod$fit %>%
broom::tidy()
}
bt_resamples$betas <- map(.x = bt_resamples$splits,
.f = glm_coefs,
mod_form)
bt_resamples %>%
unnest(betas) %>%
select(term, estimate) %>%
group_by(term) %>%
summarise(
betas = mean(estimate),
std.error.boot = sd(estimate),
) %>%
arrange(term) %>%
bind_cols(
summary_betas
)
## # A tibble: 3 x 4
## term betas std.error.boot std.error.summary
## <chr> <dbl> <dbl> <dbl>
## 1 (Intercept) -11.5 0.422 0.435
## 2 balance 0.00562 0.000221 0.000227
## 3 income 0.0000208 0.00000475 0.00000499
(d) Comment on the estimated standard errors obtained using the glm()
function and using your bootstrap function.
Answer: The estimated standard errors are pretty close.
rm(list = ls())
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 the glm()
and predict.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 the Weekly
data set. Recall that in the context of classification problems, the LOOCV error is given in (5.4).
(a) Fit a logistic regression model that predicts Direction
using Lag1
and Lag2
.
Answer: Following is the fit:
set.seed(123)
logreg_mod <- logistic_reg()
mod_form <- as.formula(Direction ~ Lag1 + Lag2)
logreg_fit <-
logreg_mod %>%
set_engine("glm") %>%
fit(mod_form, data = Weekly)
logreg_fit$fit %>% broom::tidy()
## # A tibble: 3 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 0.221 0.0615 3.60 0.000319
## 2 Lag1 -0.0387 0.0262 -1.48 0.140
## 3 Lag2 0.0602 0.0265 2.27 0.0232
(b) Fit a logistic regression model that predicts Direction
using Lag1
and Lag2
using all but the first observation.
(c) 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?
(d) 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:
(i) Fit a logistic regression model using all but the \(i\)th observation to predict Direction
using Lag1
and Lag2
.
(ii) Compute the posterior probability of the market moving up for the \(i\)th observation.
(iii) Use the posterior probability for the \(i\)th observation in order to predict whether or not the market moves up.
(iv) 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.
(e) Take the average of the \(n\) numbers obtained in (d)iv in order to obtain the LOOCV estimate for the test error. Comment on the results.
Answer: Following is the code to obtain the LOOCV estimate for the test error:
set.seed(123)
mod_form <- as.formula(Direction ~ Lag1 + Lag2)
loo_resamples_weekly <- loo_cv(Weekly)
holdout_results <- function(splits, ...) {
mod <- glm(..., data = analysis(splits), family = binomial)
holdout <- assessment(splits)
res <- broom::augment(mod, newdata = holdout, type.predict = "response")
prediction <- as_factor(ifelse(res$.fitted > 0.5, "Up", "Down"))
# Calculate whether the prediction was correct
res$wrong <- prediction != holdout$Direction
# Return the assessment data set with the additional columns
res
}
loo_resamples_weekly$results <- map(loo_resamples_weekly$splits, holdout_results, mod_form)
test_error <- loo_resamples_weekly %>%
unnest(results) %>%
summarise(
test.error = mean(wrong)
) %>%
pull
percent(test_error)
## [1] "45.0%"
#Clean-up
rm(list = ls())
We will now perform cross-validation on a simulated data set.
(a) Generate a simulated data set as follows:
set.seed(1)
x <- rnorm(100)
y <- x - 2 * x^2 + rnorm(100)
In this data set, what is \(n\) and what is \(p\)? Write out the model used to generate the data in equation form.
Answer: \(n = 100\), \(p = 1\), \(Y = \beta_0 + \beta_1 \times X_1 - \beta_2 \times X_1^2 + \epsilon\)
(b) Create a scatterplot of X against Y . Comment on what you find.
Answer: Following is the plot:
ggplot() +
geom_point(mapping = aes(x = x, y = y))
We observe a parabolic relationship.
(c) Set a random seed, and then compute the LOOCV errors that result from fitting the following four models using least squares:
i. \(Y = \beta_0 + \beta_1X + \epsilon\)
ii. \(Y = \beta_0 + \beta_1X + \beta_2X_2^2 + \epsilon\)
iii. \(Y = \beta_0 + \beta_1X + \beta_2X_2^2 + \beta_3X_3^3 + \epsilon\)
iv. \(Y = \beta_0 + \beta_1X + \beta_2X_2^2 + \beta_3X_3^3 + \beta_4X_4^4 + \epsilon\)
data <- tibble(
x = x,
y = y
)
Answer: Following is the code that computes the LOOCV errors:
set.seed(1)
mod_form <- as.formula(y ~ x)
loo_samples <- loo_cv(data)
holdout_results <- function(splits, ...) {
mod <- glm(..., data = analysis(splits))
holdout <- assessment(splits)
res <- broom::augment(mod, newdata = holdout)
# Calculate the MSE
res$mse <- (res$y - res$.fitted)^2
# Return the assessment data set with the additional columns
res
}
loo_samples$results <- map(loo_samples$splits, holdout_results, mod_form)
mse_error <- loo_samples %>%
unnest(results) %>%
summarise(
mse = mean(mse)
) %>%
pull
tblrez <- tibble(
degree = 1,
mse = mse_error,
)
tblrez
## # A tibble: 1 x 2
## degree mse
## <dbl> <dbl>
## 1 1 7.29
mod_form <- as.formula(y ~ poly(x, 2))
loo_samples$results <- map(loo_samples$splits, holdout_results, mod_form)
mse_error <- loo_samples %>%
unnest(results) %>%
summarise(
mse = mean(mse)
) %>%
pull
tblrez <- tblrez %>% add_row(degree = 2, mse = mse_error)
tblrez[2, ]
## # A tibble: 1 x 2
## degree mse
## <dbl> <dbl>
## 1 2 0.937
mod_form <- as.formula(y ~ poly(x, 3))
loo_samples$results <- map(loo_samples$splits, holdout_results, mod_form)
mse_error <- loo_samples %>%
unnest(results) %>%
summarise(
mse = mean(mse)
) %>%
pull
tblrez <- tblrez %>% add_row(degree = 3, mse = mse_error)
tblrez[3, ]
## # A tibble: 1 x 2
## degree mse
## <dbl> <dbl>
## 1 3 0.957
mod_form <- as.formula(y ~ poly(x, 4))
loo_samples$results <- map(loo_samples$splits, holdout_results, mod_form)
mse_error <- loo_samples %>%
unnest(results) %>%
summarise(
mse = mean(mse)
) %>%
pull
tblrez <- tblrez %>% add_row(degree = 4, mse = mse_error)
tblrez[4, ]
## # A tibble: 1 x 2
## degree mse
## <dbl> <dbl>
## 1 4 0.954
(d) Repeat (c) using another random seed, and report your results. Are your results the same as what you got in (c)? Why?
Answer: Following is the code using another random seed:
set.seed(111)
get_mse_errors <- function(n, dataset){
mod_form <- as.formula(y ~ poly(x, n))
loo_samples <- loo_cv(dataset)
loo_samples$results <- map(loo_samples$splits, holdout_results, mod_form)
mse_error <- loo_samples %>%
unnest(results) %>%
summarise(
mse = mean(mse)
) %>%
pull
mse_error
}
mse <- map_dbl(1:4, get_mse_errors, data)
mse
## [1] 7.2881616 0.9374236 0.9566218 0.9539049
The results are practically the same. In both cases, (c) and (d), we have used the same data, samples to model and one-out to validate.
(e) Which of the models in (c) had the smallest LOOCV error? Is this what you expected? Explain your answer.
Answer: Following is a graphical representation of results and explanation:
ggplot(data = tblrez) +
geom_line(aes(y = mse, x = degree))
2nd degree exhibits the lowest error, which is expected and coincides with the way we generated the dataset.
(f) 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?
Answer: Following is the coef estimates and after that the answers:
glm(y ~ poly(x, 4), data = data) %>%
tidy
## # A tibble: 5 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -1.55 0.0959 -16.2 5.17e-29
## 2 poly(x, 4)1 6.19 0.959 6.45 4.59e- 9
## 3 poly(x, 4)2 -23.9 0.959 -25.0 1.59e-43
## 4 poly(x, 4)3 0.264 0.959 0.275 7.84e- 1
## 5 poly(x, 4)4 1.26 0.959 1.31 1.93e- 1
1st and 2nd degree coef are only statistically significant which coincides with the results obtained from LOOCV method.
#Clean-up
rm(list = ls())
We will now consider the Boston
housing data set, from the MASS
library.
(a) Based on this data set, provide an estimate for the population mean of medv
. Call this estimate \(\hat{\mu}\).
Answer: Following is the code for estimation:
miu.hat <- mean(Boston$medv)
miu.hat
## [1] 22.53281
(b) Provide an estimate of the standard error of \(\hat{\mu}\). Interpret this result.
Answer: Following is the code for estimation:
std.error_miuhat <- sd(Boston$medv)/sqrt(nrow(Boston))
std.error_miuhat
## [1] 0.4088611
We are now able to calculate 95% confidence interval [22.5328063 - 0.8013678] and [22.5328063 + 0.8013678].
(c) Now estimate the standard error of \(\hat{\mu}\) using the bootstrap. How does this compare to your answer from (b)?
Answer: Following is the code for estimation using the bootstrap:
set.seed(1)
bt_resamples <- bootstraps(Boston, times = 1000)
get_mean_sample <- function(splits){
rez <- mean(analysis(splits)$medv)
rez
}
bt_resamples$means <- map(.x = bt_resamples$splits,
.f = get_mean_sample
)
bt_std.error_miuhat <- bt_resamples %>%
unnest(means) %>%
summarize(
mean = mean(means),
std.error = sd(means)
) %>%
pull(std.error)
bt_mean_miuhat <- bt_resamples %>%
unnest(means) %>%
summarize(
mean = mean(means),
std.error = sd(means)
) %>%
pull(mean)
bt_std.error_miuhat
## [1] 0.415502
The answer is comparable to (b).
(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)
.
Answer: Following is the 95% interval and after that the results from
t.test
[21.72694 , 23.3557078]
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
The results are identical.
(e) Based on this data set, provide an estimate, \(\hat{\mu}_{med}\), for the median value of medv in the population.
Answer: Following is the code for the estimate:
med.hat <- median(Boston$medv)
med.hat
## [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.
Answer: Following is the code for the estimate of the standard error:
get_median_sample <- function(splits){
rez <- median(analysis(splits)$medv)
rez
}
bt_resamples$medians <- map(.x = bt_resamples$splits,
.f = get_median_sample
)
bt_std.error_medianhat <- bt_resamples %>%
unnest(medians) %>%
summarize(
std.error = sd(medians)
) %>%
pull(std.error)
bt_std.error_medianhat
## [1] 0.3829064
We observe relatively narrow 95% conf. interval.
(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.)
Answer: Following is the code for the estimate:
tenthhat <- quantile(Boston$medv, probs = 0.1)
tenthhat
## 10%
## 12.75
(h) Use the bootstrap to estimate the standard error of \(\hat{\mu}_{0.1}\). Comment on your findings.
Answer: Following is the code for the estimate of the std. error:
get_tenthperc_sample <- function(splits){
rez <- quantile(analysis(splits)$medv, probs = 0.1)
rez
}
bt_resamples$tenth <- map(.x = bt_resamples$splits,
.f = get_tenthperc_sample
)
bt_std.error_tenthperc <- bt_resamples %>%
unnest(tenth) %>%
summarize(
std.error = sd(tenth)
) %>%
pull(std.error)
bt_std.error_tenthperc
## [1] 0.4979704