summarize {SimEngine} | R Documentation |
Summarize simulation results
Description
This function calculates summary statistics for simulation results, including descriptive statistics (e.g. measures of center or spread) and inferential statistics (e.g. bias or confidence interval coverage). All summary statistics are calculated over simulation replicates within a single simulation level.
Usage
summarize(sim, ..., mc_se = FALSE)
Arguments
sim |
A simulation object of class |
... |
One or more lists, separated by commas, specifying desired
summaries of the
|
mc_se |
A logical argument indicating whether to compute Monte Carlo
standard error and associated confidence interval for inferential summary
statistics. This applies only to the |
Details
For all inferential summaries there are three ways to specify
truth
: (1) a single number, meaning the estimand is the same across all simulation replicates and levels, (2) a numeric vector of the same length as the number of rows insim$results
, or (3) the name of a variable insim$results
containing the estimand of interest.There are two ways to specify the confidence interval bounds for
coverage
. The first is to provide anestimate
and its associatedse
(standard error). These should both be variables insim$results
. The function constructs a 95% Wald-type confidence interval of the form(estimate-1.96*se, estimate+1.96*se)
. The alternative is to providelower
andupper
bounds, which should also be variables insim$results
. In this case, the confidence interval is (lower
,upper
). The coverage is the proportion of simulation replicates for a given level combination in whichtruth
lies within the interval.
Value
A data frame containing the result of each specified summary function
as a column, for each of the simulation levels. The column n_reps
returns the number of successful simulation replicates within each level.
Examples
sim <- new_sim()
create_data <- function(n) { rpois(n, lambda=5) }
est_mean <- function(dat, type) {
if (type=="M") { return(mean(dat)) }
if (type=="V") { return(var(dat)) }
}
sim %<>% set_levels(n=c(10,100,1000), est=c("M","V"))
sim %<>% set_config(num_sim=5)
sim %<>% set_script(function() {
dat <- create_data(L$n)
lambda_hat <- est_mean(dat=dat, type=L$est)
return (list("lambda_hat"=lambda_hat))
})
sim %<>% run()
sim %>% summarize(
list(stat = "mean", name="mean_lambda_hat", x="lambda_hat"),
list(stat = "mse", name="lambda_mse", estimate="lambda_hat", truth=5)
)