Skip to content

Commit

Permalink
use sigma ~ 0 + time
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Apr 20, 2023
1 parent c83fbf2 commit d7b0038
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 31 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/brm_formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
24 changes: 10 additions & 14 deletions R/brm_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
...
) {
Expand All @@ -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, ...)
}
2 changes: 1 addition & 1 deletion R/brm_package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions man/brm_model.Rd

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

16 changes: 8 additions & 8 deletions tests/testthat/test-brm_formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
)
})
Expand All @@ -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"
)
)
})
Expand All @@ -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"
)
)
})
Expand All @@ -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"
)
)
})
Expand All @@ -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"
)
)
})
Expand All @@ -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"
)
)
})
Expand All @@ -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"
)
)
})
Expand All @@ -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"
)
)
})

0 comments on commit d7b0038

Please sign in to comment.