Skip to content

Commit c060e83

Browse files
committed
add and link new funs
1 parent 4d83cfd commit c060e83

22 files changed

Lines changed: 407 additions & 19 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: SimDesign
22
Title: Structure for Organizing Monte Carlo Simulation Designs
3-
Version: 2.24.4
3+
Version: 2.24.5
44
Authors@R: c(person("Phil", "Chalmers", email = "rphilip.chalmers@gmail.com", role = c("aut", "cre"),
55
comment = c(ORCID="0000-0001-5332-2810")),
66
person("Matthew", "Sigal", role = c("ctb")),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Changes in SimDesign 2.25
44

5+
- `SimErrors()` and `SimWarnings()` functions added to better track and
6+
extract error/warning information. Makes it easier to track down specific
7+
seed states to replicate any of the recorded messages
8+
59
- Default cluster type changed from `parallel::makeCluster()` to
610
`mirai::make_cluster()`. To switch back to the default use the `control`
711
logical element `use_mirai = FALSE`

R/RobbinsMonro.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Robbins-Monro (1951) stochastic root-finding algorithm
22
#'
3-
#' Function performs stochastic root solving for the provided \code{f(x)}
3+
#' Performs stochastic root solving for the provided \code{f(x)}
44
#' using the Robbins-Monro (1951) algorithm. Differs from deterministic
55
#' cousins such as \code{\link{uniroot}} in that \code{f} may contain stochastic error
66
#' components, where the root is obtained through the running average method

R/SimAnova.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Function for decomposing the simulation into ANOVA-based effect sizes
1+
#' Decompose the simulation into ANOVA-based effects
22
#'
33
#' Given the results from a simulation with \code{\link{runSimulation}} form an ANOVA table (without
44
#' p-values) with effect sizes based on the eta-squared statistic. These results provide approximate

R/SimErrors.R

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#' Extract Simulation Errors
2+
#'
3+
#' Extractor function in situations where \code{\link{runSimulation}} returned a simulation
4+
#' with detected \code{ERRORS}.
5+
#'
6+
#' @param obj object returned from \code{\link{runSimulation}} containing an \code{ERRORS} column
7+
#'
8+
#' @param seeds logical; locate \code{.Random.seed} state that caused the error message?
9+
#'
10+
#' @param subset logical; take a subset of the \code{design} object showing only conditions that
11+
#' returned errors?
12+
#'
13+
#' @references
14+
#'
15+
#' Chalmers, R. P., & Adkins, M. C. (2020). Writing Effective and Reliable Monte Carlo Simulations
16+
#' with the SimDesign Package. \code{The Quantitative Methods for Psychology, 16}(4), 248-280.
17+
#' \doi{10.20982/tqmp.16.4.p248}
18+
#'
19+
#' Sigal, M. J., & Chalmers, R. P. (2016). Play it again: Teaching statistics with Monte
20+
#' Carlo simulation. \code{Journal of Statistics Education, 24}(3), 136-156.
21+
#' \doi{10.1080/10691898.2016.1246953}
22+
#'
23+
#' @author Phil Chalmers \email{rphilip.chalmers@@gmail.com}
24+
#'
25+
#' @seealso \code{\link{SimWarnings}}, \code{\link{SimExtract}}
26+
#'
27+
#' @examples
28+
#'
29+
#' sample_sizes <- c(10, 20)
30+
#' standard_deviations <- 1
31+
#'
32+
#' Design <- createDesign(N1=sample_sizes,
33+
#' N2=sample_sizes,
34+
#' SD=standard_deviations)
35+
#' Design
36+
#'
37+
#' Generate <- function(condition, fixed_objects){
38+
#' Attach(condition)
39+
#' group1 <- rnorm(N1)
40+
#' group2 <- rnorm(N2, sd=SD)
41+
#' dat <- data.frame(group = c(rep('g1', N1), rep('g2', N2)),
42+
#' DV = c(group1, group2))
43+
#' dat
44+
#' }
45+
#'
46+
#' Analyse <- function(condition, dat, fixed_objects){
47+
#'
48+
#' # raise errors with unequal sample sizes only
49+
#' if(with(condition, N1 != N2)){
50+
#' if(runif(1, 0, 1) < .9) t.test('char')
51+
#' if(runif(1, 0, 1) < .9) aov('char')
52+
#' if(runif(1, 0, 1) < .2) stop('my error')
53+
#' }
54+
#'
55+
#' welch <- t.test(DV ~ group, dat)
56+
#' ind <- stats::t.test(DV ~ group, dat, var.equal=TRUE)
57+
#' ret <- c(welch = welch$p.value, independent = ind$p.value)
58+
#' ret
59+
#' }
60+
#'
61+
#' Summarise <- function(condition, results, fixed_objects) {
62+
#' ret <- EDR(results)
63+
#' ret
64+
#' }
65+
#'
66+
#' # print any error messages and their frequency
67+
#' res <- runSimulation(design=Design, replications=3, generate=Generate,
68+
#' analyse=Analyse, summarise=Summarise, max_errors = Inf)
69+
#' res |> select(N1, N2, SD, ERRORS)
70+
#' SimErrors(res)
71+
#' SimErrors(res, subset=FALSE)
72+
#'
73+
#' # for specific seeds (organized list of SEEDS returned)
74+
#' (seeds <- SimErrors(res, seeds=TRUE))
75+
#' names(seeds$SEEDS[[1]]) # first row errors
76+
#'
77+
#' # extract one .Random.seed state for the first design condition where
78+
#' # error occurred, pointing to the second uniquely recorded error message
79+
#' seeds$SEEDS[[1]][[2]][, 1] -> seed_state
80+
#' seed_state # note that Design_row_2 is where the error occurred
81+
#'
82+
#' \dontrun{
83+
#' # pass to runSimulation() to replicate issue (not run as this calls debug())
84+
#' runSimulation(design=Design[2,], replications=3, generate=Generate,
85+
# analyse=Analyse, summarise=Summarise,
86+
# load_seed=seed_state, debug='analyse')
87+
#' }
88+
#'
89+
#'
90+
#'
91+
SimErrors <- function(obj, seeds=FALSE, subset=TRUE){
92+
if(!any(colnames(obj) == 'ERRORS')) return(dplyr::tibble())
93+
errors <- obj$ERRORS
94+
pick <- which(errors > 0)
95+
ret <- SimExtract(obj, what='errors')
96+
if(seeds){
97+
eseeds <- SimExtract(obj, what = 'error_seeds')
98+
design <- SimExtract(obj, what = 'design')
99+
out <- vector('list', nrow(design))
100+
for(i in seq_along(pick)){
101+
p <- pick[i]
102+
ecol <- eseeds[,grepl(paste0('Design_row_', p, '.'), colnames(eseeds))]
103+
nms <- gsub(".*Error", 'Error', colnames(ecol))
104+
nms <- gsub("\\.",' ', nms)
105+
u <- unique(nms)
106+
out[[p]] <- lapply(as.list(u), \(mtc) ecol[ ,which(mtc == nms)])
107+
names(out[[p]]) <- u
108+
}
109+
ret <- dplyr::tibble(design, SEEDS=out)
110+
}
111+
if(subset) ret <- ret[pick, ]
112+
ret
113+
}

R/SimExtract.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#' Function to extract extra information from SimDesign objects
1+
#' Extract extra information from SimDesign objects
22
#'
3-
#' Function used to extract any error or warnings messages, the seeds associated
3+
#' Extracts any error or warnings messages, the seeds associated
44
#' with any error or warning messages, and any analysis results that were stored in the
55
#' final simulation object.
66
#'
@@ -60,6 +60,8 @@
6060
#'
6161
#' @export
6262
#'
63+
#' @seealso \code{\link{SimErrors}}, \code{\link{SimWarnings}}
64+
#'
6365
#' @references
6466
#'
6567
#' Chalmers, R. P., & Adkins, M. C. (2020). Writing Effective and Reliable Monte Carlo Simulations

R/SimResults.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Function to read in saved simulation results
1+
#' Read in saved simulation results
22
#'
33
#' If \code{\link{runSimulation}} was passed the flag \code{save_results = TRUE} then the
44
#' row results corresponding to the \code{design} object will be stored to a suitable

R/SimSolve.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' One Dimensional Root (Zero) Finding in Simulation Experiments
22
#'
3-
#' Function provides a stochastic root-finding approach to solve
3+
#' Provides a stochastic root-finding approach to solve
44
#' specific quantities in simulation experiments (e.g., solving for a specific
55
#' sample size to meet a target power rate) using the
66
#' Probablistic Bisection Algorithm with Bolstering and Interpolations

R/SimWarnings.R

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#' Extract Simulation Warnings
2+
#'
3+
#' Extractor function in situations where \code{\link{runSimulation}} returned a simulation
4+
#' with detected \code{WARNINGS}.
5+
#'
6+
#' @param obj object returned from \code{\link{runSimulation}} containing an \code{WARNINGS} column
7+
#'
8+
#' @param subset logical; take a subset of the \code{design} object showing only conditions that
9+
#' returned warnings?
10+
#'
11+
#' @references
12+
#'
13+
#' Chalmers, R. P., & Adkins, M. C. (2020). Writing Effective and Reliable Monte Carlo Simulations
14+
#' with the SimDesign Package. \code{The Quantitative Methods for Psychology, 16}(4), 248-280.
15+
#' \doi{10.20982/tqmp.16.4.p248}
16+
#'
17+
#' Sigal, M. J., & Chalmers, R. P. (2016). Play it again: Teaching statistics with Monte
18+
#' Carlo simulation. \code{Journal of Statistics Education, 24}(3), 136-156.
19+
#' \doi{10.1080/10691898.2016.1246953}
20+
#'
21+
#' @author Phil Chalmers \email{rphilip.chalmers@@gmail.com}
22+
#'
23+
#' @seealso \code{\link{SimErrors}}, \code{\link{SimExtract}}
24+
#'
25+
#' @examples
26+
#'
27+
#' sample_sizes <- c(10, 20)
28+
#' standard_deviations <- 1
29+
#'
30+
#' Design <- createDesign(N1=sample_sizes,
31+
#' N2=sample_sizes,
32+
#' SD=standard_deviations)
33+
#' Design
34+
#'
35+
#' Generate <- function(condition, fixed_objects){
36+
#' Attach(condition)
37+
#' group1 <- rnorm(N1)
38+
#' group2 <- rnorm(N2, sd=SD)
39+
#' dat <- data.frame(group = c(rep('g1', N1), rep('g2', N2)),
40+
#' DV = c(group1, group2))
41+
#' dat
42+
#' }
43+
#'
44+
#' # functions to throw warnings
45+
#' fn1 <- function(){
46+
#' if(sample(c(TRUE, FALSE), 1, prob = c(.1, .9))) warning('Show this warning')
47+
#' 1
48+
#' }
49+
#'
50+
#' fn2 <- function(){
51+
#' if(sample(c(TRUE, FALSE), 1, prob = c(.1, .9))) warning('Show a different warning')
52+
#' 1
53+
#' }
54+
#'
55+
#' Analyse <- function(condition, dat, fixed_objects){
56+
#' if(with(condition, N1 != N2)){
57+
#' out1 <- fn1()
58+
#' out2 <- fn2()
59+
#' }
60+
#' c(ret = 1)
61+
#' }
62+
#'
63+
#' Summarise <- function(condition, results, fixed_objects) {
64+
#' ret <- colMeans(results)
65+
#' ret
66+
#' }
67+
#'
68+
#' # print warning messages and their frequency
69+
#' res <- runSimulation(design=Design, replications=10, generate=Generate,
70+
#' analyse=Analyse, summarise=Summarise)
71+
#' res |> select(N1, N2, SD, WARNINGS)
72+
#' SimWarnings(res)
73+
#' SimWarnings(res, subset=FALSE)
74+
#'
75+
#'
76+
SimWarnings <- function(obj, subset=TRUE){
77+
if(!any(colnames(obj) == 'WARNINGS')) return(dplyr::tibble())
78+
warnings <- obj$WARNINGS
79+
pick <- which(warnings > 0)
80+
ret <- SimExtract(obj, what='warnings')
81+
if(subset) ret <- ret[pick, ]
82+
ret
83+
}

R/manageMessages.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Increase the intensity or suppress the output of an observed message
22
#'
3-
#' Function provides more nuanced management of known message outputs
3+
#' Provides more nuanced management of known message outputs
44
#' messages that appear in function calls outside the front-end users control
55
#' (e.g., functions written in third-party packages). Specifically,
66
#' this function provides a less nuclear approach than

0 commit comments

Comments
 (0)