From cfeeadce18baece0a32431b436dc57e18fbdf7c3 Mon Sep 17 00:00:00 2001 From: Adam Howes Date: Tue, 22 Oct 2024 11:46:41 +0100 Subject: [PATCH] Issue #374: Set default to `mu ~ 1` as formula (#396) * 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 --- R/fit.R | 2 +- man/epidist.default.Rd | 2 +- tests/testthat/test-formula.R | 8 +++++++- tests/testthat/test-int-latent_individual.R | 8 ++++---- vignettes/ebola.Rmd | 7 ++++--- vignettes/faq.Rmd | 6 +++--- 6 files changed, 20 insertions(+), 13 deletions(-) diff --git a/R/fit.R b/R/fit.R index 411741075..b2c844721 100644 --- a/R/fit.R +++ b/R/fit.R @@ -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) diff --git a/man/epidist.default.Rd b/man/epidist.default.Rd index 3b2999755..dc9a5a2b3 100644 --- a/man/epidist.default.Rd +++ b/man/epidist.default.Rd @@ -6,7 +6,7 @@ \usage{ \method{epidist}{default}( data, - formula = brms::bf(mu ~ 1), + formula = mu ~ 1, family = "lognormal", prior = NULL, backend = "cmdstanr", diff --git a/tests/testthat/test-formula.R b/tests/testthat/test-formula.R index 8f535d371..89bf04086 100644 --- a/tests/testthat/test-formula.R +++ b/tests/testthat/test-formula.R @@ -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( @@ -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. diff --git a/tests/testthat/test-int-latent_individual.R b/tests/testthat/test-int-latent_individual.R index f691f1b33..236981ba3 100644 --- a/tests/testthat/test-int-latent_individual.R +++ b/tests/testthat/test-int-latent_individual.R @@ -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, @@ -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 ) @@ -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()) @@ -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()) diff --git a/vignettes/ebola.Rmd b/vignettes/ebola.Rmd index 2b09a1744..de73a04fc 100644 --- a/vignettes/ebola.Rmd +++ b/vignettes/ebola.Rmd @@ -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, diff --git a/vignettes/faq.Rmd b/vignettes/faq.Rmd index 78a3c0c58..148e80e28 100644 --- a/vignettes/faq.Rmd +++ b/vignettes/faq.Rmd @@ -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 ) ``` @@ -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! @@ -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