# \donttest{
set.seed(2022)
# Create an example bootstrap survey design object ----
data('api', package = 'survey')
boot_design <- svydesign(id=~1,strata=~stype, weights=~pw,
data=apistrat, fpc=~fpc) |>
svrep::as_bootstrap_design(replicates = 5000)
# Calculate estimates of interest and retain estimates from each replicate ----
estimated_means_and_proportions <- svymean(x = ~ api00 + api99 + stype, design = boot_design,
return.replicates = TRUE)
custom_statistic <- withReplicates(design = boot_design,
return.replicates = TRUE,
theta = function(wts, data) {
numerator <- sum(data$api00 * wts)
denominator <- sum(data$api99 * wts)
statistic <- numerator/denominator
return(statistic)
})
# Determine minimum number of bootstrap replicates needed to obtain given simulation CVs ----
## For means and proportions
sim_cv_curve <- estimate_boot_reps_for_target_cv(
svrepstat = estimated_means_and_proportions,
target_cv = c(0.01, 0.05, 0.10)
)
sim_cv_curve
if (require('ggplot2')) {
plot(sim_cv_curve)
}
## For custom statistic
estimate_boot_reps_for_target_cv(
svrepstat = custom_statistic,
target_cv = c(0.01, 0.05, 0.10)
)
# }
Run the code above in your browser using DataLab