Run a summarise step for results that have been saved to the hard drive
Source:R/reSummarise.R
reSummarise.RdWhen runSimulation() uses the option save_results = TRUE
the R replication results from the Generate-Analyse functions are
stored to the hard drive. As such, additional summarise components
may be required at a later time, whereby the respective .rds files
must be read back into R to be summarised. This function performs
the reading of these files, application of a provided summarise function,
and final collection of the respective results.
Usage
reSummarise(
summarise,
dir = NULL,
files = NULL,
results = NULL,
Design = NULL,
fixed_objects = NULL,
boot_method = "none",
boot_draws = 1000L,
CI = 0.95,
prefix = "results-row"
)Arguments
- summarise
a summarise function to apply to the read-in files. See
runSimulationfor details.Note that if the simulation contained only one row then the new summarise function can be defined as either
summarise <- function(results, fixed_objects), iffixed_objectsis required, orsummarise <- function(results),- dir
directory pointing to the .rds files to be read-in that were saved from
runSimulation(..., save_results=TRUE). IfNULL, it is assumed the current working directory contains the .rds files- files
(optional) names of files to read-in. If
NULLall files located withindirwill be used- results
(optional) the results of
runSimulationwhen nosummarisefunction was provided. Can be either atibbleormatrix(indicating that exactly one design condition was evaluated), or alistofmatrix/tibbleobjects indicating that multiple conditions were performed with no summarise evaluation.Alternatively, if
store_results = TRUEin therunSimulation()execution then the final SimDesign object may be passed, where the generate-analyse information will be extracted from the object instead- Design
(optional) if
resultsinput used, and design condition information important in the summarise step, then the originaldesignobject fromrunSimulationshould be included- fixed_objects
(optional) see
runSimulationfor details- boot_method
method for performing non-parametric bootstrap confidence intervals for the respective meta-statistics computed by the
Summarisefunction. SeerunSimulationfor details- boot_draws
number of non-parametric bootstrap draws to sample for the
summarisefunction after the generate-analyse replications are collected. Default is 1000- CI
bootstrap confidence interval level (default is 95%)
- prefix
character indicating prefix used for stored files
References
Chalmers, R. P., & Adkins, M. C. (2020). Writing Effective and Reliable Monte Carlo Simulations
with the SimDesign Package. The Quantitative Methods for Psychology, 16(4), 248-280.
doi:10.20982/tqmp.16.4.p248
Sigal, M. J., & Chalmers, R. P. (2016). Play it again: Teaching statistics with Monte
Carlo simulation. Journal of Statistics Education, 24(3), 136-156.
doi:10.1080/10691898.2016.1246953
Author
Phil Chalmers rphilip.chalmers@gmail.com
Examples
Design <- createDesign(N = c(10, 20, 30))
Generate <- function(condition, fixed_objects) {
dat <- with(condition, rnorm(N, 10, 5)) # distributed N(10, 5)
dat
}
Analyse <- function(condition, dat, fixed_objects) {
ret <- c(mean=mean(dat), median=median(dat)) # mean/median of sample data
ret
}
Summarise <- function(condition, results, fixed_objects){
colMeans(results)
}
if (FALSE) { # \dontrun{
# run the simulation
runSimulation(design=Design, replications=50,
generate=Generate, analyse=Analyse,
summarise=Summarise, save_results=TRUE,
save_details = list(save_results_dirname='simresults'))
res <- reSummarise(Summarise, dir = 'simresults/')
res
Summarise2 <- function(condition, results, fixed_objects){
ret <- c(mean_ests=colMeans(results), SE=colSDs(results))
ret
}
res2 <- reSummarise(Summarise2, dir = 'simresults/')
res2
SimClean(dir='simresults/')
} # }
###
# Similar, but with results stored within the final object
res <- runSimulation(design=Design, replications=50, store_results = TRUE,
generate=Generate, analyse=Analyse, summarise=Summarise)
#>
#>
Design: 1/3; Replications: 50; RAM Used: 196.9 Mb; Total Time: 0.00s
#> Conditions: N=10
#>
|
| | 0%
|
|= | 2%
|
|== | 4%
|
|=== | 6%
|
|==== | 8%
|
|===== | 10%
|
|====== | 12%
|
|======= | 14%
|
|======== | 16%
|
|========= | 18%
|
|========== | 20%
|
|=========== | 22%
|
|============ | 24%
|
|============= | 26%
|
|============== | 28%
|
|=============== | 30%
|
|================ | 32%
|
|================= | 34%
|
|================== | 36%
|
|=================== | 38%
|
|==================== | 40%
|
|===================== | 42%
|
|====================== | 44%
|
|======================= | 46%
|
|======================== | 48%
|
|========================= | 50%
|
|========================== | 52%
|
|=========================== | 54%
|
|============================ | 56%
|
|============================= | 58%
|
|============================== | 60%
|
|=============================== | 62%
|
|================================ | 64%
|
|================================= | 66%
|
|================================== | 68%
|
|=================================== | 70%
|
|==================================== | 72%
|
|===================================== | 74%
|
|====================================== | 76%
|
|======================================= | 78%
|
|======================================== | 80%
|
|========================================= | 82%
|
|========================================== | 84%
|
|=========================================== | 86%
|
|============================================ | 88%
|
|============================================= | 90%
|
|============================================== | 92%
|
|=============================================== | 94%
|
|================================================ | 96%
|
|================================================= | 98%
|
|==================================================| 100%
#>
#>
Design: 2/3; Replications: 50; RAM Used: 196.8 Mb; Total Time: 0.01s
#> Conditions: N=20
#>
|
| | 0%
|
|= | 2%
|
|== | 4%
|
|=== | 6%
|
|==== | 8%
|
|===== | 10%
|
|====== | 12%
|
|======= | 14%
|
|======== | 16%
|
|========= | 18%
|
|========== | 20%
|
|=========== | 22%
|
|============ | 24%
|
|============= | 26%
|
|============== | 28%
|
|=============== | 30%
|
|================ | 32%
|
|================= | 34%
|
|================== | 36%
|
|=================== | 38%
|
|==================== | 40%
|
|===================== | 42%
|
|====================== | 44%
|
|======================= | 46%
|
|======================== | 48%
|
|========================= | 50%
|
|========================== | 52%
|
|=========================== | 54%
|
|============================ | 56%
|
|============================= | 58%
|
|============================== | 60%
|
|=============================== | 62%
|
|================================ | 64%
|
|================================= | 66%
|
|================================== | 68%
|
|=================================== | 70%
|
|==================================== | 72%
|
|===================================== | 74%
|
|====================================== | 76%
|
|======================================= | 78%
|
|======================================== | 80%
|
|========================================= | 82%
|
|========================================== | 84%
|
|=========================================== | 86%
|
|============================================ | 88%
|
|============================================= | 90%
|
|============================================== | 92%
|
|=============================================== | 94%
|
|================================================ | 96%
|
|================================================= | 98%
|
|==================================================| 100%
#>
#>
Design: 3/3; Replications: 50; RAM Used: 196.8 Mb; Total Time: 0.02s
#> Conditions: N=30
#>
|
| | 0%
|
|= | 2%
|
|== | 4%
|
|=== | 6%
|
|==== | 8%
|
|===== | 10%
|
|====== | 12%
|
|======= | 14%
|
|======== | 16%
|
|========= | 18%
|
|========== | 20%
|
|=========== | 22%
|
|============ | 24%
|
|============= | 26%
|
|============== | 28%
|
|=============== | 30%
|
|================ | 32%
|
|================= | 34%
|
|================== | 36%
|
|=================== | 38%
|
|==================== | 40%
|
|===================== | 42%
|
|====================== | 44%
|
|======================= | 46%
|
|======================== | 48%
|
|========================= | 50%
|
|========================== | 52%
|
|=========================== | 54%
|
|============================ | 56%
|
|============================= | 58%
|
|============================== | 60%
|
|=============================== | 62%
|
|================================ | 64%
|
|================================= | 66%
|
|================================== | 68%
|
|=================================== | 70%
|
|==================================== | 72%
|
|===================================== | 74%
|
|====================================== | 76%
|
|======================================= | 78%
|
|======================================== | 80%
|
|========================================= | 82%
|
|========================================== | 84%
|
|=========================================== | 86%
|
|============================================ | 88%
|
|============================================= | 90%
|
|============================================== | 92%
|
|=============================================== | 94%
|
|================================================ | 96%
|
|================================================= | 98%
|
|==================================================| 100%
#>
#> Simulation complete. Total execution time: 0.03s
res
#> # A tibble: 3 × 8
#> N mean median REPLICATIONS SIM_TIME RAM_USED SEED COMPLETED
#> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <int> <chr>
#> 1 10 10.192 10.230 50 0.01s 196.8 Mb 1771457935 Tue Jul 14 19:3…
#> 2 20 10.146 10.084 50 0.01s 196.8 Mb 590590962 Tue Jul 14 19:3…
#> 3 30 10.257 10.328 50 0.01s 196.8 Mb 273992574 Tue Jul 14 19:3…
# same summarise but with bootstrapping
res2 <- reSummarise(Summarise, results = res, boot_method = 'basic')
res2
#> # A tibble: 3 × 7
#> N mean median BOOT_mean_2.5 BOOT_mean_97.5 BOOT_median_2.5
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 10 10.2 10.2 9.76 10.6 9.69
#> 2 20 10.1 10.1 9.87 10.4 9.75
#> 3 30 10.3 10.3 9.99 10.5 10.1
#> # ℹ 1 more variable: BOOT_median_97.5 <dbl>