Title: | Helper Functions for Simulation Studies |
---|---|
Description: | Calculates performance criteria measures and associated Monte Carlo standard errors for simulation results. Includes functions to help run simulation studies, following a general simulation workflow that closely aligns with the approach described by Morris, White, and Crowther (2019) <DOI:10.1002/sim.8086>. Also includes functions for calculating bootstrap confidence intervals (including normal, basic, studentized, percentile, bias-corrected, and bias-corrected-and-accelerated) with tidy output, as well as for extrapolating confidence interval coverage rates and hypothesis test rejection rates following techniques suggested by Boos and Zhang (2000) <DOI:10.1080/01621459.2000.10474226>. |
Authors: | Megha Joshi [aut, cre] |
Maintainer: | Megha Joshi <[email protected]> |
License: | GPL-3 |
Version: | 0.3.1 |
Built: | 2025-02-10 06:16:12 UTC |
Source: | https://github.com/meghapsimatrix/simhelpers |
A dataset containing simulation results from estimating Cronbach's alpha and its variance.
alpha_res
alpha_res
A tibble with 1,000 rows and 3 variables:
estimate of alpha.
estimate of the variance of alpha.
true alpha used to generate the data.
Calculate one or multiple bootstrap confidence intervals, given a sample of bootstrap replications.
bootstrap_CIs( boot_est, boot_se = NULL, est = NULL, se = NULL, influence = NULL, CI_type = "percentile", level = 0.95, B_vals = length(boot_est), reps = 1L, format = "wide", seed = NULL )
bootstrap_CIs( boot_est, boot_se = NULL, est = NULL, se = NULL, influence = NULL, CI_type = "percentile", level = 0.95, B_vals = length(boot_est), reps = 1L, format = "wide", seed = NULL )
boot_est |
vector of bootstrap replications of an estimator. |
boot_se |
vector of estimated standard errors from each bootstrap replication. |
est |
numeric value of the estimate based on the original sample.
Required for |
se |
numeric value of the estimated standard error based on the original
sample. Required for |
influence |
vector of empirical influence values for the estimator. Required for |
CI_type |
Character string or vector of character strings indicating
types of confidence intervals to calculate. Options are |
level |
numeric value between 0 and 1 for the desired coverage level,
with a default of |
B_vals |
vector of sub-sample sizes for which to calculate confidence
intervals. Setting |
reps |
integer value for the number of sub-sample confidence intervals
to generate when |
format |
character string controlling the format of the output. If
|
seed |
Single numeric value to which the random number generator seed
will be set. Default is |
Confidence intervals are calculated following the methods described
in Chapter 5 of Davison and Hinkley (1997). For basic non-parametric
bootstraps, the methods are nearly identical to the implementation in
boot.ci
from the boot
package.
If format = "wide"
, the function returns a data.frame
with reps
rows per entry of B_vals
, where each row contains
confidence intervals for one sub-sample replication.
If format = "long"
, the function returns a data.frame
with
one row for each CI_type
, each replication, and each entry of
B_vals
, where each row contains a single confidence interval for one
sub-sample replication.
If format = "wide-list"
, then the output will be structured as in
format = "wide"
but will be wrapped in an unnamed list, which makes
it easier to sore the output in a tibble, and will be assigned the class
"bootstrap_CIs"
.
Davison, A.C. and Hinkley, D.V. (1997). _Bootstrap Methods and Their Application_, Chapter 5. Cambridge University Press.
# generate t-distributed data N <- 50 mu <- 2 nu <- 5 dat <- mu + rt(N, df = nu) # create bootstrap replications f <- \(x) { c( M = mean(x, trim = 0.1), SE = sd(x) / sqrt(length(x)) ) } booties <- replicate(399, { sample(dat, replace = TRUE, size = N) |> f() }) res <- f(dat) # calculate bootstrap CIs from full set of bootstrap replicates bootstrap_CIs( boot_est = booties[1,], boot_se = booties[2,], est = res[1], se = res[2], CI_type = c("normal","basic","student","percentile","bias-corrected"), format = "long" ) # Calculate bias-corrected-and-accelerated CIs inf_vals <- res[1] - sapply(seq_along(dat), \(i) f(dat[-i])[1]) bootstrap_CIs( boot_est = booties[1,], est = res[1], influence = inf_vals, CI_type = c("percentile","bias-corrected","BCa"), format = "long" ) # calculate multiple bootstrap CIs using sub-sampling of replicates bootstrap_CIs( boot_est = booties[1,], boot_se = booties[2,], est = res[1], se = res[2], CI_type = c("normal","basic","student","percentile","bias-corrected"), B_vals = 199, reps = 4L, format = "long" ) # calculate multiple bootstrap CIs using sub-sampling of replicates, # for each of several sub-sample sizes. bootstrap_CIs( boot_est = booties[1,], boot_se = booties[2,], est = res[1], se = res[2], CI_type = c("normal","basic","student","percentile"), B_vals = c(49,99,199), reps = 4L, format = "long" )
# generate t-distributed data N <- 50 mu <- 2 nu <- 5 dat <- mu + rt(N, df = nu) # create bootstrap replications f <- \(x) { c( M = mean(x, trim = 0.1), SE = sd(x) / sqrt(length(x)) ) } booties <- replicate(399, { sample(dat, replace = TRUE, size = N) |> f() }) res <- f(dat) # calculate bootstrap CIs from full set of bootstrap replicates bootstrap_CIs( boot_est = booties[1,], boot_se = booties[2,], est = res[1], se = res[2], CI_type = c("normal","basic","student","percentile","bias-corrected"), format = "long" ) # Calculate bias-corrected-and-accelerated CIs inf_vals <- res[1] - sapply(seq_along(dat), \(i) f(dat[-i])[1]) bootstrap_CIs( boot_est = booties[1,], est = res[1], influence = inf_vals, CI_type = c("percentile","bias-corrected","BCa"), format = "long" ) # calculate multiple bootstrap CIs using sub-sampling of replicates bootstrap_CIs( boot_est = booties[1,], boot_se = booties[2,], est = res[1], se = res[2], CI_type = c("normal","basic","student","percentile","bias-corrected"), B_vals = 199, reps = 4L, format = "long" ) # calculate multiple bootstrap CIs using sub-sampling of replicates, # for each of several sub-sample sizes. bootstrap_CIs( boot_est = booties[1,], boot_se = booties[2,], est = res[1], se = res[2], CI_type = c("normal","basic","student","percentile"), B_vals = c(49,99,199), reps = 4L, format = "long" )
Calculate one or multiple bootstrap p-values, given a bootstrap sample of test statistics.
bootstrap_pvals( boot_stat, stat, alternative = "two-sided", B_vals = length(boot_stat), reps = 1L, enlist = FALSE, seed = NULL )
bootstrap_pvals( boot_stat, stat, alternative = "two-sided", B_vals = length(boot_stat), reps = 1L, enlist = FALSE, seed = NULL )
boot_stat |
vector of bootstrap replications of a test statistic. |
stat |
numeric value of the test statistic based on the original sample. |
alternative |
a character string specifying the alternative hypothesis,
must be one of |
B_vals |
vector of sub-sample sizes for which to calculate p-values.
Setting |
reps |
integer value for the number of sub-sample p-values to generate
when |
enlist |
logical indicating whether to wrap the returned values in an
unnamed list, with a default of |
seed |
Single numeric value to which the random number generator seed
will be set. Default is |
p-values are calculated by comparing stat
to the distribution
of boot_stat
, which is taken to represent the null distribution of
the test statistic. If alternative = "two-sided"
(the default), then
the p-value is the proportion of the bootstrap sample where the absolute
value of the bootstrapped statistic exceeds the absolute value of the
original statistic. If alternative = "greater"
, then the p-value is
the proportion of the bootstrap sample where the value of the bootstrapped
statistic is larger than the original statistic. If alternative =
"less"
, then the p-value is the proportion of the bootstrap sample where
the value of the bootstrapped statistic is less than the original
statistic.
The format of the output depends on several contingencies. If only a
single value of B_vals
is specified and reps = 1
, then the
function returns a vector with a single p-value. If only a single value of
B_vals
is specified but B_vals < length(boot_stat)
and
reps > 1
, then the function returns a vector p-values, with an entry
for each sub-sample replication. If B_vals
is a vector of multiple
values, then the function returns a list with one entry per entry of
B_vals
, where each entry is a vector of length reps
with
entries for each sub-sample replication.
If enlist = TRUE
, then results will be wrapped in an unnamed list,
which makes it easier to sore the output in a tibble.
Davison, A.C. and Hinkley, D.V. (1997). _Bootstrap Methods and Their Application_, Chapter 4. Cambridge University Press.
# generate data from two distinct populations dat <- data.frame( group = rep(c("A","B"), c(40, 50)), y = c( rgamma(40, shape = 7, scale = 2), rgamma(50, shape = 3, scale = 4) ) ) stat <- t.test(y ~ group, data = dat)$statistic # create bootstrap replications under the null of no difference boot_dat <- dat booties <- replicate(399, { boot_dat$group <- sample(dat$group) t.test(y ~ group, data = boot_dat)$statistic }) # calculate bootstrap p-values from full set of bootstrap replicates bootstrap_pvals(boot_stat = booties, stat = stat) # calculate multiple bootstrap p-values using sub-sampling of replicates bootstrap_pvals( boot_stat = booties, stat = stat, B_vals = 199, reps = 4L ) # calculate multiple bootstrap p-values using sub-sampling of replicates, # for each of several sub-sample sizes. bootstrap_pvals( boot_stat = booties, stat = stat, B_vals = c(49,99,199), reps = 4L )
# generate data from two distinct populations dat <- data.frame( group = rep(c("A","B"), c(40, 50)), y = c( rgamma(40, shape = 7, scale = 2), rgamma(50, shape = 3, scale = 4) ) ) stat <- t.test(y ~ group, data = dat)$statistic # create bootstrap replications under the null of no difference boot_dat <- dat booties <- replicate(399, { boot_dat$group <- sample(dat$group) t.test(y ~ group, data = boot_dat)$statistic }) # calculate bootstrap p-values from full set of bootstrap replicates bootstrap_pvals(boot_stat = booties, stat = stat) # calculate multiple bootstrap p-values using sub-sampling of replicates bootstrap_pvals( boot_stat = booties, stat = stat, B_vals = 199, reps = 4L ) # calculate multiple bootstrap p-values using sub-sampling of replicates, # for each of several sub-sample sizes. bootstrap_pvals( boot_stat = booties, stat = stat, B_vals = c(49,99,199), reps = 4L )
Bundle a data-generation function, a data-analysis function, and (optionally) a performance summary function into a simulation driver.
bundle_sim( f_generate, f_analyze, f_summarize = NULL, reps_name = "reps", seed_name = "seed", summarize_opt_name = "summarize", row_bind_reps = TRUE )
bundle_sim( f_generate, f_analyze, f_summarize = NULL, reps_name = "reps", seed_name = "seed", summarize_opt_name = "summarize", row_bind_reps = TRUE )
f_generate |
function for data-generation |
f_analyze |
function for data-analysis. The first argument must be the
data, in the format generated by |
f_summarize |
function for calculating performance summaries across
replications. The first argument must be the replicated data analysis
results. Default is |
reps_name |
character string to set the name of the argument for the
number of replications, with a default value of |
seed_name |
character string to set the name of the argument for the
seed option, with a default value of |
summarize_opt_name |
character string to set the name of the argument
for where to apply |
row_bind_reps |
logical indicating whether to combine the simulation
results into a data frame using |
A function to repeatedly run the 'f_generate' and 'f_analyze' functions and (optionally) apply 'f_summarize' to the resulting replications.
f_G <- rnorm f_A <- function(x, trim = 0) data.frame(y_bar = mean(x, trim = trim)) f_S <- function(x, calc_sd = FALSE) { if (calc_sd) { res_SD <- apply(x, 2, sd) res <- data.frame(M = colMeans(x), SD = res_SD) } else { res <- data.frame(M = colMeans(x)) } res } # bundle data-generation and data-analysis functions sim1 <- bundle_sim(f_generate = f_G, f_analyze = f_A) args(sim1) res1 <- sim1(4, n = 70, mean = 0.5, sd = 1, trim = 0.2) res1 # bundle data-generation, data-analysis, and performance summary functions sim2 <- bundle_sim(f_generate = f_G, f_analyze = f_A, f_summarize = f_S) args(sim2) res2 <- sim2(24, n = 7, mean = 0, sd = 1, trim = 0.2, calc_sd = TRUE) res2 # bundle data-generation and data-analysis functions, returning results as a list sim3 <- bundle_sim(f_generate = f_G, f_analyze = f_A, row_bind_reps = FALSE) args(sim3) res3 <- sim3(4, n = 70, mean = 0.5, sd = 3, trim = 0.2) res3
f_G <- rnorm f_A <- function(x, trim = 0) data.frame(y_bar = mean(x, trim = trim)) f_S <- function(x, calc_sd = FALSE) { if (calc_sd) { res_SD <- apply(x, 2, sd) res <- data.frame(M = colMeans(x), SD = res_SD) } else { res <- data.frame(M = colMeans(x)) } res } # bundle data-generation and data-analysis functions sim1 <- bundle_sim(f_generate = f_G, f_analyze = f_A) args(sim1) res1 <- sim1(4, n = 70, mean = 0.5, sd = 1, trim = 0.2) res1 # bundle data-generation, data-analysis, and performance summary functions sim2 <- bundle_sim(f_generate = f_G, f_analyze = f_A, f_summarize = f_S) args(sim2) res2 <- sim2(24, n = 7, mean = 0, sd = 1, trim = 0.2, calc_sd = TRUE) res2 # bundle data-generation and data-analysis functions, returning results as a list sim3 <- bundle_sim(f_generate = f_G, f_analyze = f_A, row_bind_reps = FALSE) args(sim3) res3 <- sim3(4, n = 70, mean = 0.5, sd = 3, trim = 0.2) res3
Calculates absolute bias, variance, mean squared error (mse) and root mean squared error (rmse). The function also calculates the associated Monte Carlo standard errors.
calc_absolute( data, estimates, true_param, criteria = c("bias", "variance", "stddev", "mse", "rmse"), winz = Inf )
calc_absolute( data, estimates, true_param, criteria = c("bias", "variance", "stddev", "mse", "rmse"), winz = Inf )
data |
data frame or tibble containing the simulation results. |
estimates |
vector or name of column from |
true_param |
vector or name of column from |
criteria |
character or character vector indicating the performance
criteria to be calculated, with possible options |
winz |
numeric value for winsorization constant. If set to a finite
value, estimates will be winsorized at the constant multiple of the
inter-quartile range below the 25th percentile or above the 75th percentile
of the distribution. For instance, setting |
A tibble containing the number of simulation iterations, performance criteria estimate(s) and the associated MCSE.
calc_absolute(data = t_res, estimates = est, true_param = true_param)
calc_absolute(data = t_res, estimates = est, true_param = true_param)
Calculates confidence interval coverage and width. The function also calculates the associated Monte Carlo standard errors. The confidence interval percentage is based on how you calculated the lower and upper bounds.
calc_coverage( data, lower_bound, upper_bound, true_param, criteria = c("coverage", "width"), winz = Inf )
calc_coverage( data, lower_bound, upper_bound, true_param, criteria = c("coverage", "width"), winz = Inf )
data |
data frame or tibble containing the simulation results. |
lower_bound |
vector or name of column from |
upper_bound |
vector or name of column from |
true_param |
vector or name of column from |
criteria |
character or character vector indicating the performance
criteria to be calculated, with possible options |
winz |
numeric value for winsorization constant. If set to a finite
value, estimates will be winsorized at the constant multiple of the
inter-quartile range below the 25th percentile or above the 75th percentile
of the distribution. For instance, setting |
A tibble containing the number of simulation iterations, performance criteria estimate(s) and the associated MCSE.
calc_coverage(data = t_res, lower_bound = lower_bound, upper_bound = upper_bound, true_param = true_param)
calc_coverage(data = t_res, lower_bound = lower_bound, upper_bound = upper_bound, true_param = true_param)
Calculates rejection rate. The function also calculates the associated Monte Carlo standard error.
calc_rejection(data, p_values, alpha = 0.05, format = "wide")
calc_rejection(data, p_values, alpha = 0.05, format = "wide")
data |
data frame or tibble containing the simulation results. |
p_values |
vector or name of column from |
alpha |
scalar or vector indicating the nominal alpha level(s). Default value is set to the conventional .05. |
format |
option |
A tibble containing the number of simulation iterations, performance criteria estimate and the associated MCSE.
calc_rejection(data = t_res, p_values = p_val)
calc_rejection(data = t_res, p_values = p_val)
Calculates relative bias, mean squared error (relative mse), and root mean squared error (relative rmse). The function also calculates the associated Monte Carlo standard errors.
calc_relative( data, estimates, true_param, criteria = c("relative bias", "relative mse", "relative rmse"), winz = Inf )
calc_relative( data, estimates, true_param, criteria = c("relative bias", "relative mse", "relative rmse"), winz = Inf )
data |
data frame or tibble containing the simulation results. |
estimates |
vector or name of column from |
true_param |
vector or name of column from |
criteria |
character or character vector indicating the performance
criteria to be calculated, with possible options |
winz |
numeric value for winsorization constant. If set to a finite
value, estimates will be winsorized at the constant multiple of the
inter-quartile range below the 25th percentile or above the 75th percentile
of the distribution. For instance, setting |
A tibble containing the number of simulation iterations, performance criteria estimate(s) and the associated MCSE.
calc_relative(data = t_res, estimates = est, true_param = true_param)
calc_relative(data = t_res, estimates = est, true_param = true_param)
Calculates relative bias, mean squared error (relative mse), and root mean squared error (relative rmse) of variance estimators. The function also calculates the associated jack-knife Monte Carlo standard errors.
calc_relative_var( data, estimates, var_estimates, criteria = c("relative bias", "relative mse", "relative rmse"), winz = Inf, var_winz = winz )
calc_relative_var( data, estimates, var_estimates, criteria = c("relative bias", "relative mse", "relative rmse"), winz = Inf, var_winz = winz )
data |
data frame or tibble containing the simulation results. |
estimates |
vector or name of column from |
var_estimates |
vector or name of column from |
criteria |
character or character vector indicating the performance
criteria to be calculated, with possible options |
winz |
numeric value for winsorization constant. If set to a finite
value, estimates will be winsorized at the constant multiple of the
inter-quartile range below the 25th percentile or above the 75th percentile
of the distribution. For instance, setting |
var_winz |
numeric value for winsorization constant for the
variance estimates. If set to a finite value, variance estimates will be
winsorized at the constant multiple of the inter-quartile range below the
25th percentile or above the 75th percentile of the distribution. For
instance, setting |
A tibble containing the number of simulation iterations, performance criteria estimate(s) and the associated MCSE.
calc_relative_var(data = alpha_res, estimates = A, var_estimates = Var_A)
calc_relative_var(data = alpha_res, estimates = A, var_estimates = Var_A)
Creates and opens a .R file containing a skeleton for writing a Monte Carlo simulation study.
create_skeleton()
create_skeleton()
## Not run: create_skeleton() ## End(Not run)
## Not run: create_skeleton() ## End(Not run)
Evaluates a simulation function on each row of a data frame or tibble
containing parameter values. Returns a single tibble with parameters
and simulation results. The function uses furrr::future_pmap
, which
allows for easy parallelization.
evaluate_by_row( params, sim_function, ..., results_name = ".results", .progress = FALSE, .options = furrr::furrr_options(), system_time = TRUE )
evaluate_by_row( params, sim_function, ..., results_name = ".results", .progress = FALSE, .options = furrr::furrr_options(), system_time = TRUE )
params |
data frame or tibble containing simulation parameter values. Each row should represent a separate set of parameter values. |
sim_function |
function to be evaluated, with argument names matching
the variable names in |
... |
additional arguments passed to |
results_name |
character string to set the name of the column storing the results of the simulation. Default is |
.progress |
A single logical. Should a progress bar be displayed? Only works with multisession, multicore, and multiprocess futures. Note that if a multicore/multisession future falls back to sequential, then a progress bar will not be displayed. Warning: The |
.options |
The |
system_time |
logical indicating whether to print computation time.
|
A tibble containing parameter values and simulation results.
df <- data.frame( n = 3:5, lambda = seq(8, 16, 4) ) evaluate_by_row(df, rpois)
df <- data.frame( n = 3:5, lambda = seq(8, 16, 4) ) evaluate_by_row(df, rpois)
Given a set of bootstrap confidence intervals calculated across sub-samples with different numbers of replications, extrapolates confidence interval coverage and width of bootstrap confidence intervals to a specified (larger) number of bootstraps. The function also calculates the associated Monte Carlo standard errors. The confidence interval percentage is based on how you calculated the lower and upper bounds.
extrapolate_coverage( data, CI_subsamples, true_param, B_target = Inf, criteria = c("coverage", "width"), winz = Inf, nested = FALSE, format = "wide", width_trim = 0, cover_na_val = NA, width_na_val = NA )
extrapolate_coverage( data, CI_subsamples, true_param, B_target = Inf, criteria = c("coverage", "width"), winz = Inf, nested = FALSE, format = "wide", width_trim = 0, cover_na_val = NA, width_na_val = NA )
data |
data frame or tibble containing the simulation results. |
CI_subsamples |
list or name of column from |
true_param |
vector or name of column from |
B_target |
number of bootstrap replications to which the criteria should
be extrapolated, with a default of |
criteria |
character or character vector indicating the performance
criteria to be calculated, with possible options |
winz |
numeric value for winsorization constant. If set to a finite
value, estimates will be winsorized at the constant multiple of the
inter-quartile range below the 25th percentile or above the 75th percentile
of the distribution. For instance, setting |
nested |
logical value controlling the format of the output. If
|
format |
character string controlling the format of the output when
|
width_trim |
numeric value specifying the trimming percentage to use when summarizing CI widths across replications from a single set of bootstraps, with a default of 0.0 (i.e., use the regular arithmetic mean). |
cover_na_val |
numeric value to use for calculating coverage if bootstrap CI end-points are missing. Default is |
width_na_val |
numeric value to use for calculating width if bootstrap CI end-points are missing. Default is |
A tibble containing the number of simulation iterations, performance criteria estimate(s) and the associated MCSE.
Boos DD, Zhang J (2000). “Monte Carlo evaluation of resampling-based hypothesis tests.” Journal of the American Statistical Association, 95(450), 486–492. doi:10.1080/01621459.2000.10474226.
dgp <- function(N, mu, nu) { mu + rt(N, df = nu) } estimator <- function( dat, B_vals = c(49,59,89,99), m = 4, trim = 0.1 ) { # compute estimate and standard error N <- length(dat) est <- mean(dat, trim = trim) se <- sd(dat) / sqrt(N) # compute booties booties <- replicate(max(B_vals), { x <- sample(dat, size = N, replace = TRUE) data.frame( M = mean(x, trim = trim), SE = sd(x) / sqrt(N) ) }, simplify = FALSE) |> dplyr::bind_rows() # confidence intervals for each B_vals CIs <- bootstrap_CIs( boot_est = booties$M, boot_se = booties$SE, est = est, se = se, CI_type = c("normal","basic","student","percentile"), B_vals = B_vals, reps = m, format = "wide-list" ) res <- data.frame( est = est, se = se ) res$CIs <- CIs res } #' build a simulation driver function simulate_bootCIs <- bundle_sim( f_generate = dgp, f_analyze = estimator ) boot_results <- simulate_bootCIs( reps = 50, N = 20, mu = 2, nu = 3, B_vals = seq(49, 199, 50), ) extrapolate_coverage( data = boot_results, CI_subsamples = CIs, true_param = 2 ) extrapolate_coverage( data = boot_results, CI_subsamples = CIs, true_param = 2, B_target = 999, format = "long" )
dgp <- function(N, mu, nu) { mu + rt(N, df = nu) } estimator <- function( dat, B_vals = c(49,59,89,99), m = 4, trim = 0.1 ) { # compute estimate and standard error N <- length(dat) est <- mean(dat, trim = trim) se <- sd(dat) / sqrt(N) # compute booties booties <- replicate(max(B_vals), { x <- sample(dat, size = N, replace = TRUE) data.frame( M = mean(x, trim = trim), SE = sd(x) / sqrt(N) ) }, simplify = FALSE) |> dplyr::bind_rows() # confidence intervals for each B_vals CIs <- bootstrap_CIs( boot_est = booties$M, boot_se = booties$SE, est = est, se = se, CI_type = c("normal","basic","student","percentile"), B_vals = B_vals, reps = m, format = "wide-list" ) res <- data.frame( est = est, se = se ) res$CIs <- CIs res } #' build a simulation driver function simulate_bootCIs <- bundle_sim( f_generate = dgp, f_analyze = estimator ) boot_results <- simulate_bootCIs( reps = 50, N = 20, mu = 2, nu = 3, B_vals = seq(49, 199, 50), ) extrapolate_coverage( data = boot_results, CI_subsamples = CIs, true_param = 2 ) extrapolate_coverage( data = boot_results, CI_subsamples = CIs, true_param = 2, B_target = 999, format = "long" )
Given a set of bootstrap confidence intervals calculated across sub-samples with different numbers of replications, extrapolates confidence interval coverage and width of bootstrap confidence intervals to a specified (larger) number of bootstraps. The function also calculates the associated Monte Carlo standard errors. The confidence interval percentage is based on how you calculated the lower and upper bounds.
extrapolate_rejection( data, pvalue_subsamples, B_target = Inf, alpha = 0.05, nested = FALSE, format = "wide" )
extrapolate_rejection( data, pvalue_subsamples, B_target = Inf, alpha = 0.05, nested = FALSE, format = "wide" )
data |
data frame or tibble containing the simulation results. |
pvalue_subsamples |
list or name of column from |
B_target |
number of bootstrap replications to which the criteria should
be extrapolated, with a default of |
alpha |
scalar or vector indicating the nominal alpha level(s). Default value is set to the conventional .05. |
nested |
logical value controlling the format of the output. If
|
format |
character string controlling the format of the output when
|
A tibble containing the number of simulation iterations, performance criteria estimate(s) and the associated MCSE.
Boos DD, Zhang J (2000). “Monte Carlo evaluation of resampling-based hypothesis tests.” Journal of the American Statistical Association, 95(450), 486–492. doi:10.1080/01621459.2000.10474226.
# function to generate data from two distinct populations dgp <- function(N_A, N_B, shape_A, scale_A, shape_B, scale_B) { data.frame( group = rep(c("A","B"), c(N_A, N_B)), y = c( rgamma(N_A, shape = shape_A, scale = scale_A), rgamma(N_B, shape = shape_B, scale = scale_B) ) ) } # function to do a bootstrap t-test estimator <- function( dat, B_vals = c(49,59,89,99), # number of booties to evaluate pval_reps = 4L ) { stat <- t.test(y ~ group, data = dat)$statistic # create bootstrap replications under the null of no difference boot_dat <- dat booties <- replicate(max(B_vals), { boot_dat$group <- sample(dat$group) t.test(y ~ group, data = boot_dat)$statistic }) # calculate multiple bootstrap p-values using sub-sampling of replicates res <- data.frame(stat = stat) res$pvalue_subsamples <- bootstrap_pvals( boot_stat = booties, stat = stat, B_vals = B_vals, reps = pval_reps, enlist = TRUE ) res } # create simulation driver simulate_boot_pvals <- bundle_sim( f_generate = dgp, f_analyze = estimator ) # replicate the bootstrap process x <- simulate_boot_pvals( reps = 50L, N_A = 20, N_B = 25, shape_A = 7, scale_A = 2, shape_B = 4, scale_B = 3, B_vals = c(49, 99, 149, 199), pval_reps = 2L ) extrapolate_rejection( data = x, pvalue_subsamples = pvalue_subsamples, B_target = 1999, alpha = c(.01, .05, .10) ) extrapolate_rejection( data = x, pvalue_subsamples = pvalue_subsamples, B_target = Inf, alpha = c(.01, .05, .10), nested = TRUE )
# function to generate data from two distinct populations dgp <- function(N_A, N_B, shape_A, scale_A, shape_B, scale_B) { data.frame( group = rep(c("A","B"), c(N_A, N_B)), y = c( rgamma(N_A, shape = shape_A, scale = scale_A), rgamma(N_B, shape = shape_B, scale = scale_B) ) ) } # function to do a bootstrap t-test estimator <- function( dat, B_vals = c(49,59,89,99), # number of booties to evaluate pval_reps = 4L ) { stat <- t.test(y ~ group, data = dat)$statistic # create bootstrap replications under the null of no difference boot_dat <- dat booties <- replicate(max(B_vals), { boot_dat$group <- sample(dat$group) t.test(y ~ group, data = boot_dat)$statistic }) # calculate multiple bootstrap p-values using sub-sampling of replicates res <- data.frame(stat = stat) res$pvalue_subsamples <- bootstrap_pvals( boot_stat = booties, stat = stat, B_vals = B_vals, reps = pval_reps, enlist = TRUE ) res } # create simulation driver simulate_boot_pvals <- bundle_sim( f_generate = dgp, f_analyze = estimator ) # replicate the bootstrap process x <- simulate_boot_pvals( reps = 50L, N_A = 20, N_B = 25, shape_A = 7, scale_A = 2, shape_B = 4, scale_B = 3, B_vals = c(49, 99, 149, 199), pval_reps = 2L ) extrapolate_rejection( data = x, pvalue_subsamples = pvalue_subsamples, B_target = 1999, alpha = c(.01, .05, .10) ) extrapolate_rejection( data = x, pvalue_subsamples = pvalue_subsamples, B_target = Inf, alpha = c(.01, .05, .10), nested = TRUE )
Repeat an expression (usually involving random number
generation) multiple times. Optionally, organize the results into a
data.frame
that stacks the output from all replications of the
expression.
repeat_and_stack(n, expr, stack = TRUE)
repeat_and_stack(n, expr, stack = TRUE)
n |
Number of times to repeat the expression |
expr |
An expression to be evaluated. |
stack |
Logical value indicating whether to organize the results into a
|
If stack = TRUE
(the default), the results of each evaluation
of expr
will be stacked together using rbind
. If stack
= FALSE
, a list of length n
with entries corresponding to the
output of each replication of expr
.
repeat_and_stack(n = 3, data.frame(x = rexp(2))) repeat_and_stack(n = 3, data.frame(x = rexp(2)), stack = FALSE)
repeat_and_stack(n = 3, data.frame(x = rexp(2))) repeat_and_stack(n = 3, data.frame(x = rexp(2)), stack = FALSE)
A dataset containing simulation results from a study that just runs a t-test.
t_res
t_res
A tibble with 1,000 rows and 5 variables:
estimate of the mean difference.
p-value from the t-test.
lower bound of the confidence interval.
upper bound of the confidence interval.
true mean difference used to generate the data.
A dataset containing simulation results comparing small sample correction methods for cluster robust variance estimation in meta-analysis.
Tipton_Pusto
Tipton_Pusto
A tibble with 15,300 rows and 8 variables:
the number of studies included in the meta-analysis.
correlation between outcomes.
measure of heterogeneity of true effects.
type of contrast that was tested.
small sample method used.
the number of parameters in the hypothesis test.
the Type 1 error rate.
the Monte Carlo standard error for the estimate of the Type 1 error rate.
Tipton E, Pustejovsky JE (2015). “Small-sample adjustments for tests of moderators and model fit using robust variance estimation in meta-regression.” Journal of Educational and Behavioral Statistics, 40(6), 604–634. doi:10.3102/1076998615606099.
A dataset containing simulation results from a study comparing Welch t-test to the conventional t-test.
welch_res
welch_res
A tibble with 16,000 rows and 11 variables:
sample size for Group 1.
sample size for Group 2.
true difference in means of two groups used to generate the data.
number of iterations.
seed used to generate data.
indicates whether Welch or conventional t-test was used.
estimate of the mean difference.
variance of the estimate.
p-value from the t-test.
lower bound of the confidence interval.
upper bound of the confidence interval.