Skip to content

Commit

Permalink
Issue #374: Set default to mu ~ 1 as formula (#396)
Browse files Browse the repository at this point in the history
* Add test of mu ~ 1 working

* Set mu ~ 1 as default

* Improve wording

* Redocument following merge

* Include test of brms::bf(mu ~ 1, sigma ~ 1) producing the same thing as mu ~ 1

* Reword to use mu ~ 1
  • Loading branch information
athowes authored Oct 22, 2024
1 parent a9b339a commit cfeeadc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ epidist <- function(data, formula, family, prior, backend, fn, ...) {
#' @method epidist default
#' @family fit
#' @export
epidist.default <- function(data, formula = brms::bf(mu ~ 1),
epidist.default <- function(data, formula = mu ~ 1,
family = "lognormal", prior = NULL,
backend = "cmdstanr", fn = brms::brm, ...) {
epidist_validate(data)
Expand Down
2 changes: 1 addition & 1 deletion man/epidist.default.Rd

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

8 changes: 7 additions & 1 deletion tests/testthat/test-formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ as_string_formula <- function(formula) {

test_that("epidist_formula with default settings produces a brmsformula with the correct intercept only formula", { # nolint: line_length_linter.
form <- epidist_formula(
prep_obs, family = family_lognormal, formula = brms::bf(mu ~ 1, sigma ~ 1)
prep_obs, family = family_lognormal, formula = mu ~ 1
)
expect_s3_class(form, "brmsformula")
expect_equal(
Expand All @@ -22,6 +22,12 @@ test_that("epidist_formula with default settings produces a brmsformula with the
as_string_formula(form$pforms$sigma),
"sigma ~ 1"
)
form_explicit <- epidist_formula(
prep_obs, family = family_lognormal, formula = brms::bf(mu ~ 1, sigma ~ 1)
)
attr(form$pforms$sigma, ".Environment") <- NULL
attr(form_explicit$pforms$sigma, ".Environment") <- NULL
expect_identical(form, form_explicit)
})

test_that("epidist_formula with custom formulas produces a brmsformula with correct custom formulas", { # nolint: line_length_linter.
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-int-latent_individual.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test_that("epidist.epidist_latent_individual samples from the prior according to
epidist_formula <- epidist_formula(
data = prep_obs,
family = epidist_family,
formula = brms::bf(mu ~ 1, sigma ~ 1)
formula = mu ~ 1
)
epidist_prior <- epidist_prior(
data = prep_obs,
Expand Down Expand Up @@ -139,7 +139,7 @@ test_that("epidist.epidist_latent_individual Stan code has no syntax errors and
stancode_gamma <- epidist(
data = prep_obs_gamma,
family = stats::Gamma(link = "log"),
formula = brms::bf(mu ~ 1, shape ~ 1),
formula = mu ~ 1,
output_dir = fs::dir_create(tempfile()),
fn = brms::make_stancode
)
Expand All @@ -157,7 +157,7 @@ test_that("epidist.epidist_latent_individual fits and the MCMC converges in the
fit_gamma <- epidist(
data = prep_obs_gamma,
family = stats::Gamma(link = "log"),
formula = brms::bf(mu ~ 1, shape ~ 1),
formula = mu ~ 1,
seed = 1,
silent = 2,
output_dir = fs::dir_create(tempfile())
Expand All @@ -174,7 +174,7 @@ test_that("epidist.epidist_latent_individual recovers the simulation settings fo
fit_gamma <- epidist(
data = prep_obs_gamma,
family = stats::Gamma(link = "log"),
formula = brms::bf(mu ~ 1, shape ~ 1),
formula = mu ~ 1,
seed = 1,
silent = 2,
output_dir = fs::dir_create(tempfile())
Expand Down
7 changes: 4 additions & 3 deletions vignettes/ebola.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,14 @@ Now we are ready to fit the latent individual model.

We start by fitting a single lognormal distribution to the data.
This model assumes that a single distribution describes all delays in the data, regardless of the case's location, sex, or any other covariates.
To do this, we set `formula = bf(mu ~ 1, sigma ~ 1)` to place an model with only and intercept parameter (i.e. `~ 1` in R formula syntax) on the `mu` and `sigma` parameters of the lognormal distribution.
This distribution is specified using `family = lognormal()`.
To do this, we set `formula = mu ~ 1` to place an model with only an intercept parameter (i.e. `~ 1` in R formula syntax) on the `mu` parameter of the lognormal distribution specified using `family = lognormal()`.
(Note that the lognormal distribution has two distributional parameters `mu` and `sigma`.
As a model is not explicitly placed on `sigma`, a constant model `sigma ~ 1` is assumed.)

```{r}
fit <- epidist(
data = obs_prep,
formula = bf(mu ~ 1, sigma ~ 1),
formula = mu ~ 1,
family = lognormal(),
algorithm = "sampling",
refresh = 0,
Expand Down
6 changes: 3 additions & 3 deletions vignettes/faq.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ obs_cens_trunc_samp <- simulate_gillespie(seed = 101) |>
data <- as_latent_individual(obs_cens_trunc_samp)
fit <- epidist(
data,
formula = bf(mu ~ 1, sigma ~ 1),
formula = mu ~ 1,
seed = 1
)
```
Expand Down Expand Up @@ -108,7 +108,7 @@ family <- "lognormal"
epidist_family <- epidist_family(data, family)
epidist_formula <- epidist_formula(
data, family = epidist_family, formula = bf(mu ~ 1, sigma ~ 1)
data, family = epidist_family, formula = mu ~ 1
)
# NULL here means no replacing priors from the user!
Expand All @@ -130,7 +130,7 @@ Here are the distributions on the delay distribution mean and standard deviation
set.seed(1)
fit_ppc <- epidist(
data = data,
formula = bf(mu ~ 1, sigma ~ 1),
formula = mu ~ 1,
family = "lognormal",
sample_prior = "only",
seed = 1
Expand Down

0 comments on commit cfeeadc

Please sign in to comment.