Extractor function in situations where runSimulation returned a simulation
with detected WARNINGS.
SimWarnings(obj, subset = TRUE)object returned from runSimulation containing an WARNINGS column
logical; take a subset of the design object showing only conditions that
returned warnings?
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
sample_sizes <- c(10, 20)
standard_deviations <- 1
Design <- createDesign(N1=sample_sizes,
N2=sample_sizes,
SD=standard_deviations)
Design
#> # A tibble: 4 × 3
#> N1 N2 SD
#> <dbl> <dbl> <dbl>
#> 1 10 10 1
#> 2 20 10 1
#> 3 10 20 1
#> 4 20 20 1
Generate <- function(condition, fixed_objects){
Attach(condition)
group1 <- rnorm(N1)
group2 <- rnorm(N2, sd=SD)
dat <- data.frame(group = c(rep('g1', N1), rep('g2', N2)),
DV = c(group1, group2))
dat
}
# functions to throw warnings
fn1 <- function(){
if(sample(c(TRUE, FALSE), 1, prob = c(.1, .9))) warning('Show this warning')
1
}
fn2 <- function(){
if(sample(c(TRUE, FALSE), 1, prob = c(.1, .9))) warning('Show a different warning')
1
}
Analyse <- function(condition, dat, fixed_objects){
if(with(condition, N1 != N2)){
out1 <- fn1()
out2 <- fn2()
}
c(ret = 1)
}
Summarise <- function(condition, results, fixed_objects) {
ret <- colMeans(results)
ret
}
# print warning messages and their frequency
res <- runSimulation(design=Design, replications=10, generate=Generate,
analyse=Analyse, summarise=Summarise)
res |> select(N1, N2, SD, WARNINGS)
#> # A tibble: 4 × 4
#> N1 N2 SD WARNINGS
#> <dbl> <dbl> <dbl> <int>
#> 1 10 10 1 0
#> 2 20 10 1 2
#> 3 10 20 1 1
#> 4 20 20 1 0
SimWarnings(res)
#> N1 N2 SD WARNING: Warning in fn1() : Show this warning
#> 2 20 10 1 2
#> 3 10 20 1 1
SimWarnings(res, subset=FALSE)
#> N1 N2 SD WARNING: Warning in fn1() : Show this warning
#> 1 10 10 1 0
#> 2 20 10 1 2
#> 3 10 20 1 1
#> 4 20 20 1 0