diff --git a/NAMESPACE b/NAMESPACE index 9140b26d..22b1991e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(brm_simulate) importFrom(MASS,mvrnorm) importFrom(brms,brm) importFrom(brms,brmsformula) +importFrom(brms,get_prior) importFrom(brms,prior) importFrom(brms,unstr) importFrom(rlang,warn) diff --git a/R/brm_formula.R b/R/brm_formula.R index dbe746da..c1948ec7 100644 --- a/R/brm_formula.R +++ b/R/brm_formula.R @@ -89,7 +89,7 @@ brm_formula <- function( ) right <- paste(terms, collapse = " + ") formula <- stats::as.formula(paste(response, "~", right)) - formula_sigma <- stats::as.formula(paste("sigma ~", time)) + formula_sigma <- stats::as.formula(paste("sigma ~ 0 +", time)) brms::brmsformula(formula = formula, formula_sigma) } diff --git a/R/brm_model.R b/R/brm_model.R index f574ac50..e5489faf 100644 --- a/R/brm_model.R +++ b/R/brm_model.R @@ -14,10 +14,7 @@ #' 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_intercept_sigma Positive numeric of length 1, -#' prior standard deviation -#' of the "intercept" class of parameters with `dpar = "sigma"`. -#' @param sd_b_sigma Positive numeric of length 1, +#' @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 @@ -56,8 +53,7 @@ brm_model <- function( formula = brm_formula(), sd_intercept = 100, sd_b = 100, - sd_intercept_sigma = 100, - sd_b_sigma = 100, + sd_sigma = 100, shape_cor = 1, ... ) { @@ -68,17 +64,17 @@ brm_model <- function( ) assert_pos(sd_intercept) assert_pos(sd_b) - assert_pos(sd_intercept_sigma) - assert_pos(sd_b_sigma) + assert_pos(sd_sigma) prior_0 <- sprintf("normal(0, %s)", sd_intercept) prior_b <- sprintf("normal(0, %s)", sd_b) - prior_0_sigma <- sprintf("normal(0, %s)", sd_intercept_sigma) - prior_b_sigma <- sprintf("normal(0, %s)", sd_b_sigma) + prior_sigma <- sprintf("normal(0, %s)", sd_sigma) prior_cor <- sprintf("lkj_corr_cholesky(%s)", shape_cor) - prior <- brms::set_prior(prior = prior_0, class = "Intercept") + - brms::set_prior(prior = prior_b, class = "b") + - brms::set_prior(prior_0_sigma, class = "Intercept", dpar = "sigma") + - brms::set_prior(prior_b_sigma, class = "b", dpar = "sigma") + + 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") + } brms::brm(data = data, formula = formula, prior = prior, ...) } diff --git a/R/brm_package.R b/R/brm_package.R index fc3db6ad..f23e5e92 100644 --- a/R/brm_package.R +++ b/R/brm_package.R @@ -19,7 +19,7 @@ #' Ther Innov Regul Sci 42, 303–319 (2008). #' doi:10.1177/009286150804200402 #' @family help -#' @importFrom brms brm brmsformula prior unstr +#' @importFrom brms brm brmsformula get_prior prior unstr #' @importFrom MASS mvrnorm #' @importFrom rlang warn #' @importFrom stats as.formula model.matrix rnorm runif diff --git a/man/brm_model.Rd b/man/brm_model.Rd index 5f565fa2..6e4e44d7 100644 --- a/man/brm_model.Rd +++ b/man/brm_model.Rd @@ -9,8 +9,7 @@ brm_model( formula = brm_formula(), sd_intercept = 100, sd_b = 100, - sd_intercept_sigma = 100, - sd_b_sigma = 100, + sd_sigma = 100, shape_cor = 1, ... ) @@ -31,11 +30,7 @@ of the "intercept" class of parameters.} \item{sd_b}{Positive numeric of length 1, prior standard deviation of the "b" class of parameters.} -\item{sd_intercept_sigma}{Positive numeric of length 1, -prior standard deviation -of the "intercept" class of parameters with \code{dpar = "sigma"}.} - -\item{sd_b_sigma}{Positive numeric of length 1, +\item{sd_sigma}{Positive numeric of length 1, prior standard deviation of the "b" class of parameters with \code{dpar = "sigma"}.} diff --git a/tests/testthat/test-brm_formula.R b/tests/testthat/test-brm_formula.R index 99151328..283696fc 100644 --- a/tests/testthat/test-brm_formula.R +++ b/tests/testthat/test-brm_formula.R @@ -18,7 +18,7 @@ test_that("brm_formula() with default names and all terms", { expect_equal( deparse(out[[2L]][[1L]], width.cutoff = 500L), paste( - "sigma ~ AVISIT" + "sigma ~ 0 + AVISIT" ) ) }) @@ -45,7 +45,7 @@ test_that("brm_formula() with all user-supplied columns and all terms", { expect_equal( deparse(out[[2L]][[1L]], width.cutoff = 500L), paste( - "sigma ~ t" + "sigma ~ 0 + t" ) ) }) @@ -69,7 +69,7 @@ test_that("brm_formula() without intercept", { expect_equal( deparse(out[[2L]][[1L]], width.cutoff = 500L), paste( - "sigma ~ AVISIT" + "sigma ~ 0 + AVISIT" ) ) }) @@ -93,7 +93,7 @@ test_that("brm_formula() without group effect", { expect_equal( deparse(out[[2L]][[1L]], width.cutoff = 500L), paste( - "sigma ~ AVISIT" + "sigma ~ 0 + AVISIT" ) ) }) @@ -117,7 +117,7 @@ test_that("brm_formula() without time effect", { expect_equal( deparse(out[[2L]][[1L]], width.cutoff = 500L), paste( - "sigma ~ AVISIT" + "sigma ~ 0 + AVISIT" ) ) }) @@ -141,7 +141,7 @@ test_that("brm_formula() without baseline effect", { expect_equal( deparse(out[[2L]][[1L]], width.cutoff = 500L), paste( - "sigma ~ AVISIT" + "sigma ~ 0 + AVISIT" ) ) }) @@ -165,7 +165,7 @@ test_that("brm_formula() without baseline interaction", { expect_equal( deparse(out[[2L]][[1L]], width.cutoff = 500L), paste( - "sigma ~ AVISIT" + "sigma ~ 0 + AVISIT" ) ) }) @@ -189,7 +189,7 @@ test_that("brm_formula() without group interaction", { expect_equal( deparse(out[[2L]][[1L]], width.cutoff = 500L), paste( - "sigma ~ AVISIT" + "sigma ~ 0 + AVISIT" ) ) })