Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open-ended prior argument in brm_model() #27

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,16 @@ Imports:
trialr,
utils
Suggests:
BH,
knitr (>= 1.30),
markdown (>= 1.1),
rmarkdown (>= 2.4),
testthat (>= 3.0.0)
LinkingTo:
BH,
Rcpp,
RcppEigen,
RcppParallel,
rmarkdown (>= 2.4),
rstan,
StanHeaders
StanHeaders,
testthat (>= 3.0.0)
Encoding: UTF-8
Language: en-US
VignetteBuilder: knitr
Expand Down
35 changes: 8 additions & 27 deletions R/brm_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@
#' of the model, including fixed effects, residual correlation,
#' and heterogeneity in the discrete-time-specific residual variance
#' components.
#' @param sd_intercept Positive numeric of length 1, prior standard deviation
#' of the "intercept" class of parameters.
#' @param sd_b Positive numeric of length 1, prior standard deviation
#' of the "b" class of parameters.
#' @param sd_sigma Positive numeric of length 1,
#' prior standard deviation
#' of the "b" class of parameters with `dpar = "sigma"`.
#' @param shape_cor Positive numeric of length 1. For unstructured
#' correlation, this is the LKJ shape parameter.
#' @param prior Either `NULL` or a `"brmsprior"` object from `brms::prior()`.
#' @param ... Arguments to `brms::brm()` other than `data`, `formula`,
#' and `prior`.
#' @examples
Expand Down Expand Up @@ -50,31 +42,20 @@
#' brms::prior_summary(model)
brm_model <- function(
data,
formula = brm_formula(),
sd_intercept = 100,
sd_b = 100,
sd_sigma = 100,
shape_cor = 1,
formula = brms.mmrm::brm_formula(),
prior = brms::prior("lkj_corr_cholesky(1)", class = "Lcortime"),
...
) {
assert(is.data.frame(data), message = "data arg must be a data frame.")
assert(
inherits(formula, "brmsformula"),
message = "formula arg must be a \"brmsformula\" object."
)
assert_pos(sd_intercept)
assert_pos(sd_b)
assert_pos(sd_sigma)
prior_0 <- sprintf("normal(0, %s)", sd_intercept)
prior_b <- sprintf("normal(0, %s)", sd_b)
prior_sigma <- sprintf("normal(0, %s)", sd_sigma)
prior_cor <- sprintf("lkj_corr_cholesky(%s)", shape_cor)
prior <- brms::set_prior(prior = prior_b, class = "b") +
brms::set_prior(prior_sigma, class = "b", dpar = "sigma") +
brms::set_prior(prior_cor, class = "Lcortime")
prior_classes <- brms::get_prior(formula = formula, data = data)$class
if ("Intercept" %in% prior_classes) {
prior <- prior + brms::set_prior(prior = prior_0, class = "Intercept")
if (!is.null(prior)) {
assert(
inherits(prior, "brmsprior"),
message = "prior arg must be a \"brmsprior\" object or NULL."
)
}
brms::brm(data = data, formula = formula, prior = prior, ...)
}
20 changes: 3 additions & 17 deletions man/brm_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.