Skip to content

Commit

Permalink
Issue #438: Use class as default for family and reexport common famil…
Browse files Browse the repository at this point in the history
…ies and formula function (#449)

* Add 3 families and bf as reexports

* Use lognormal() as default and document it

* Use lognormal() not "lognormal"

* Stop with brms::bf and use bf

* Remove more calls to brms::lognormal that can be lognormal

* We are also exporting stats::Gamma

* Another one

* Document

* Use predictions in test of sex effect

* Can do this without predict_delay_pars

* Change to link function here
  • Loading branch information
athowes authored Nov 19, 2024
1 parent 3cf20fa commit 3991f2f
Show file tree
Hide file tree
Showing 25 changed files with 127 additions and 59 deletions.
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ S3method(epidist_formula_model,epidist_latent_model)
S3method(epidist_model_prior,default)
S3method(epidist_stancode,default)
S3method(epidist_stancode,epidist_latent_model)
export(Gamma)
export(add_mean_sd)
export(as_epidist_latent_model)
export(as_epidist_linelist_data)
export(as_epidist_naive_model)
export(assert_epidist)
export(bf)
export(epidist)
export(epidist_diagnostics)
export(epidist_family)
Expand All @@ -42,6 +44,7 @@ export(epidist_stancode)
export(is_epidist_latent_model)
export(is_epidist_linelist_data)
export(is_epidist_naive_model)
export(lognormal)
export(new_epidist_latent_model)
export(new_epidist_linelist_data)
export(new_epidist_naive_model)
Expand All @@ -52,10 +55,13 @@ export(simulate_exponential_cases)
export(simulate_gillespie)
export(simulate_secondary)
export(simulate_uniform_cases)
export(weibull)
import(ggplot2)
importFrom(brms,bf)
importFrom(brms,lognormal)
importFrom(brms,prior)
importFrom(brms,stanvar)
importFrom(brms,weibull)
importFrom(checkmate,assert_class)
importFrom(checkmate,assert_data_frame)
importFrom(checkmate,assert_date)
Expand All @@ -75,6 +81,7 @@ importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(lubridate,days)
importFrom(lubridate,is.timepoint)
importFrom(stats,Gamma)
importFrom(stats,as.formula)
importFrom(stats,setNames)
importFrom(tibble,tibble)
Expand Down
7 changes: 4 additions & 3 deletions R/epidist.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#' @param family A description of the response distribution and link function to
#' be used in the model. Every family function has a link argument allowing
#' users to specify the link function to be applied on the response variable.
#' If not specified, default links are used. For details of supported families
#' see [brmsfamily()].
#' If not specified, default links are used. For details of all supported
#' families see [brmsfamily()]. Commonly used, such as [lognormal()], are also
#' reexported as part of `epidist`.
#' @param prior One or more `brmsprior` objects created by [brms::set_prior()]
#' or related functions. These priors are passed to [epidist_prior()] in the
#' `prior` argument.
Expand All @@ -34,7 +35,7 @@ epidist <- function(data, formula, family, prior, fn, ...) {
#' @family fit
#' @export
epidist.default <- function(data, formula = mu ~ 1,
family = "lognormal", prior = NULL,
family = lognormal(), prior = NULL,
fn = brms::brm, ...) {
assert_epidist(data)
epidist_family <- epidist_family(data, family)
Expand Down
2 changes: 1 addition & 1 deletion R/family.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#' @inheritParams epidist
#' @family family
#' @export
epidist_family <- function(data, family = "lognormal", ...) {
epidist_family <- function(data, family = lognormal(), ...) {
assert_epidist(data)
family <- brms:::validate_family(family) # nolint
class(family) <- c(family$family, class(family))
Expand Down
15 changes: 15 additions & 0 deletions R/reexports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' @importFrom brms lognormal
#' @export
brms::lognormal

#' @importFrom brms weibull
#' @export
brms::weibull

#' @importFrom stats Gamma
#' @export
stats::Gamma

#' @importFrom brms bf
#' @export
brms::bf
3 changes: 3 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ reference:
- title: Diagnostic functions
contents:
- has_concept("diagnostics")
- title: Reexported functions
contents:
- reexports
5 changes: 3 additions & 2 deletions man/dot-add_dpar_info.Rd

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

5 changes: 3 additions & 2 deletions man/epidist.Rd

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

7 changes: 4 additions & 3 deletions man/epidist.default.Rd

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

7 changes: 4 additions & 3 deletions man/epidist_family.Rd

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

5 changes: 3 additions & 2 deletions man/epidist_family_prior.Rd

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

5 changes: 3 additions & 2 deletions man/epidist_family_prior.default.Rd

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

5 changes: 3 additions & 2 deletions man/epidist_family_prior.lognormal.Rd

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

5 changes: 3 additions & 2 deletions man/epidist_family_reparam.Rd

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

5 changes: 3 additions & 2 deletions man/epidist_family_reparam.default.Rd

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

5 changes: 3 additions & 2 deletions man/epidist_family_reparam.gamma.Rd

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

21 changes: 21 additions & 0 deletions man/reexports.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ if (not_on_cran()) {
)

fit_gamma <- epidist(
data = prep_obs_gamma, family = stats::Gamma(link = "log"),
data = prep_obs_gamma, family = Gamma(link = "log"),
seed = 1, chains = 2, cores = 2, silent = 2, refresh = 0,
backend = "cmdstanr"
)

fit_sex <- epidist(
data = prep_obs_sex,
formula = brms::bf(mu ~ 1 + sex, sigma ~ 1 + sex),
formula = bf(mu ~ 1 + sex, sigma ~ 1 + sex),
seed = 1, silent = 2, refresh = 0,
cores = 2, chains = 2, backend = "cmdstanr"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-family.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("epidist_family gives an error when passed inappropriate family input"
})

test_that("the family argument in epidist_family passes as expected for brms and stats family objects, as well as strings", { # nolint: line_length_linter.
family_lognormal <- epidist_family(prep_obs, family = brms::lognormal())
family_lognormal <- epidist_family(prep_obs, family = lognormal())
expect_identical(family_lognormal$name, "latent_lognormal")
family_gamma <- epidist_family(prep_obs, family = Gamma(link = "log"))
expect_identical(family_gamma$name, "latent_gamma")
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-formula.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
prep_obs_gamma <- as_epidist_latent_model(sim_obs_gamma)

family_lognormal <- epidist_family(prep_obs, family = brms::lognormal())
family_lognormal <- epidist_family(prep_obs, family = lognormal())

test_that("epidist_formula with default settings produces a brmsformula with the correct intercept only formula", { # nolint: line_length_linter.
form <- epidist_formula(
Expand All @@ -18,7 +18,7 @@ test_that("epidist_formula with default settings produces a brmsformula with the
)
form_explicit <- epidist_formula(
prep_obs,
family = family_lognormal, formula = brms::bf(mu ~ 1, sigma ~ 1)
family = family_lognormal, formula = bf(mu ~ 1, sigma ~ 1)
)
attr(form$pforms$sigma, ".Environment") <- NULL
attr(form_explicit$pforms$sigma, ".Environment") <- NULL
Expand All @@ -30,7 +30,7 @@ test_that("epidist_formula with custom formulas produces a brmsformula with corr
form_sex <- epidist_formula(
prep_obs,
family = family_lognormal,
formula = brms::bf(mu ~ 1 + sex, sigma ~ 1 + sex)
formula = bf(mu ~ 1 + sex, sigma ~ 1 + sex)
)
expect_s3_class(form_sex, "brmsformula")
expect_identical(
Expand All @@ -48,28 +48,28 @@ test_that("epidist_formula with custom formulas errors for incorrect custom form
epidist_formula(
prep_obs,
family = family_lognormal,
formula = brms::bf(mu ~ 1 + age, sigma ~ 1)
formula = bf(mu ~ 1 + age, sigma ~ 1)
)
)
expect_error(
epidist_formula(
prep_obs,
family = family_lognormal,
formula = brms::bf(mu ~ 1, sigma ~ 1 + age)
formula = bf(mu ~ 1, sigma ~ 1 + age)
)
)
expect_error(
epidist_formula(
prep_obs,
family = family_lognormal,
formula = brms::bf(list(), sigma ~ 1)
formula = bf(list(), sigma ~ 1)
)
)
expect_error(
epidist_formula(
prep_obs,
family = family_lognormal,
formula = brms::bf(mu ~ 1, shape ~ 1)
formula = bf(mu ~ 1, shape ~ 1)
)
)
})
Loading

0 comments on commit 3991f2f

Please sign in to comment.