'Function for bootstrap - get sd of statistics of simulations
I am trying to write a function for bootstrap (an assignment). The question is as follow:
Compute the bootstrap standard error for: -
mean()and -median()and - the top quartile and - the standard deviation of the price (yes, I want the standard deviation of the standard deviation... If this confuses you, go through the mean example and replace the computation of themeanbythe_thing_we_wantand realize thatthe_thing_we_wantcan be the standard deviation) -max()One way to approach this is to define a new function for each. Another is to write abootstrap_funcfunction that takes an additional argument calledfun, and then you call itbootstrap_func(B, v, median)to have the same effect asbootstrap_median. Implement this functionbootstrap_func.
Example call to this function:bootstrap_func(1000, vienna_data$price, mean).
This is what I attempt to do:
hotels_price <- read.csv("data_repo/hotels-europe/clean/hotels-europe_price.csv")
hotels_features_vienna <- read.csv("data_repo/hotels-europe/clean/hotels-europe_features.csv") %>%
filter(city == "Vienna")
hotels_vienna <- left_join(hotels_price, hotels_features_vienna) %>%
filter(across(.cols = everything(), function(x){!is.na(x)}))
set.seed(7777)
B <- 100 # B is asked to be set to 100 - the number of simulations
v <- hotels_vienna$price
get_sim <- function(v) {
sample(v, replace = TRUE)
}
fun <- function(get_sim) {
mean <- function(get_sim) {mean(get_sim)}
median <- function(get_sim) {mean(get_sim)}
top_quartile <- function(get_sim) {quantile(get_sim, 0.75)}
sd <- function(get_sim) {sd(get_sim)}
max <- function(get_sim) {max(get_sim)}
}
sim_stats <- function(B) {
replicate(B, fun(get_sim()))
}
bootstrap_func <- function(B, v, fun) {
sd(sim_stats())
}
bootstrap_func(100, hotels_vienna$price, sd)
When I try to run the last line R throw an error "Error: C stack usage 15926432 is too close to the limit". I do not know how to fix this, and more importantly, if my code will get to the desired results. I appreciate any inputs. TIA.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
