# NOT RUN {
rng <- function(data, ...) {
ret <- range(data)
names(ret) <- c("min", "max")
ret
}
### The following line is only necessary
### if the examples are not executed in the global
### environment, which for instance is the case when
### the oneline-documentation
### http://marselscheer.github.io/simTool/reference/eval_tibbles.html
### is build. In such case eval_tibble() would search the
### above defined function rng() in the global environment where
### it does not exist!
eval_tibbles <- purrr::partial(eval_tibbles, envir = environment())
dg <- expand_tibble(fun = "rnorm", n = c(5L, 10L))
pg <- expand_tibble(proc = c("rng", "median", "length"))
eval_tibbles(dg, pg, rep = 2, simplify = FALSE)
eval_tibbles(dg, pg, rep = 2)
eval_tibbles(dg, pg,
rep = 2,
post_analyze = purrr::compose(as.data.frame, t)
)
eval_tibbles(dg, pg, rep = 2, summary_fun = list(mean = mean, sd = sd))
regData <- function(n, SD) {
data.frame(
x = seq(0, 1, length = n),
y = rnorm(n, sd = SD)
)
}
eg <- eval_tibbles(
expand_tibble(fun = "regData", n = 5L, SD = 1:2),
expand_tibble(proc = "lm", formula = c("y~x", "y~I(x^2)")),
replications = 3
)
eg
presever_rownames <- function(mat) {
rn <- rownames(mat)
ret <- tibble::as_tibble(mat)
ret$term <- rn
ret
}
eg <- eval_tibbles(
expand_tibble(fun = "regData", n = 5L, SD = 1:2),
expand_tibble(proc = "lm", formula = c("y~x", "y~I(x^2)")),
post_analyze = purrr::compose(presever_rownames, coef, summary),
# post_analyze = broom::tidy, # is a nice out of the box alternative
summary_fun = list(mean = mean, sd = sd),
group_for_summary = "term",
replications = 3
)
eg$simulation
dg <- expand_tibble(fun = "rexp", rate = c(10, 100), n = c(50L, 100L))
pg <- expand_tibble(proc = c("t.test"), conf.level = c(0.8, 0.9, 0.95))
et <- eval_tibbles(dg, pg,
ncpus = 1,
replications = 10^1,
post_analyze = function(ttest, .truth) {
mu <- 1 / .truth$rate
ttest$conf.int[1] <= mu && mu <= ttest$conf.int[2]
},
summary_fun = list(mean = mean, sd = sd)
)
et
dg <- dplyr::bind_rows(
expand_tibble(fun = "rexp", rate = 10, .truth = 1 / 10, n = c(50L, 100L)),
expand_tibble(fun = "rnorm", .truth = 0, n = c(50L, 100L))
)
pg <- expand_tibble(proc = c("t.test"), conf.level = c(0.8, 0.9, 0.95))
et <- eval_tibbles(dg, pg,
ncpus = 1,
replications = 10^1,
post_analyze = function(ttest, .truth) {
ttest$conf.int[1] <= .truth && .truth <= ttest$conf.int[2]
},
summary_fun = list(mean = mean, sd = sd)
)
et
### need to remove the locally adapted eval_tibbles()
### otherwise executing the examples would mask
### eval_tibbles from simTool-namespace.
rm(eval_tibbles)
# }
Run the code above in your browser using DataLab