Skip to content

Commit d3531b4

Browse files
committed
new function that spits out final brms formula
1 parent ccb6790 commit d3531b4

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export(ggbnec_data)
6060
export(is_manecsummary)
6161
export(is_prebayesnecfit)
6262
export(log_lik_beta_binomial2)
63+
export(make_brmsformula)
6364
export(models)
6465
export(nec)
6566
export(nsec)

R/bayesnecformula.R

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,58 @@ crf <- function(x, model, arg_to_retrieve = "x") {
612612
trials <- function(...) {
613613
identity(...)
614614
}
615+
616+
#' Expose the final \code{\link[brms]{brmsformula}}
617+
#'
618+
#' Checks the input formula according to
619+
#' \code{\link[bayesnec:bayesnec-package]{bayesnec}} requirements and
620+
#' expose the final \code{\link[brms]{brmsformula}} which is to be fitted via
621+
#' package \pkg{brms}.
622+
#'
623+
#' @param formula Either a \code{\link[base]{character}} string defining an
624+
#' R formula or an actual \code{\link[stats]{formula}} object. See details.
625+
#' @param data A \code{\link[base]{data.frame}} containing the variables
626+
#' specified in \code{formula}.
627+
#'
628+
#' @importFrom stats model.frame
629+
#'
630+
#' @return A named \code{\link[base]{list}}, with each element containing the
631+
#' final \code{\link[brms]{brmsformula}} to be passed to
632+
#' \code{\link[brms]{brm}}.
633+
#'
634+
#' @seealso
635+
#' \code{\link{bayesnecformula}},
636+
#' \code{\link{check_formula}}
637+
#'
638+
#' @examples
639+
#' library(bayesnec)
640+
#' nec3param <- function(beta, nec, top, x) {
641+
#' top * exp(-exp(beta) * (x - nec) *
642+
#' ifelse(x - nec < 0, 0, 1))
643+
#' }
644+
#'
645+
#' data <- data.frame(x = seq(1, 20, length.out = 10), tr = 100, wght = c(1, 2),
646+
#' group_1 = sample(c("a", "b"), 10, replace = TRUE),
647+
#' group_2 = sample(c("c", "d"), 10, replace = TRUE))
648+
#' data$y <- nec3param(beta = -0.2, nec = 4, top = 100, data$x)
649+
#'
650+
#' # make one single model
651+
#' f_1 <- "log(y) | trials(tr) ~ crf(sqrt(x), \"nec3param\")"
652+
#' make_brmsformula(f_1, data)
653+
#' # make an entire class of models
654+
#' f_2 <- y ~ crf(x, "ecx") + ogl(group_1) + pgl(group_2)
655+
#' make_brmsformula(f_2, data)
656+
#'
657+
#' @export
658+
make_brmsformula <- function(formula, data) {
659+
formula <- bnf(formula)
660+
all_models <- get_model_from_formula(formula)
661+
out <- list()
662+
for (i in seq_along(all_models)) {
663+
formula_i <- single_model_formula(formula, all_models[i])
664+
bdat_i <- model.frame(formula_i, data = data, run_par_checks = FALSE)
665+
out[[i]] <- wrangle_model_formula(all_models[i], formula_i, bdat_i)
666+
}
667+
names(out) <- all_models
668+
out
669+
}

man/make_brmsformula.Rd

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)