From ab1b7ca41705b9b64d755a3522096d84b77d086d Mon Sep 17 00:00:00 2001 From: athowes Date: Sat, 16 Nov 2024 11:35:02 +0000 Subject: [PATCH 01/11] Add 3 families and bf as reexports --- NAMESPACE | 7 +++++++ R/reexports.R | 15 +++++++++++++++ _pkgdown.yml | 3 +++ 3 files changed, 25 insertions(+) create mode 100644 R/reexports.R diff --git a/NAMESPACE b/NAMESPACE index 83cc4fcb4..c7d355347 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/R/reexports.R b/R/reexports.R new file mode 100644 index 000000000..d4a60e421 --- /dev/null +++ b/R/reexports.R @@ -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 diff --git a/_pkgdown.yml b/_pkgdown.yml index a371ea562..7d0e1cf9c 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -84,3 +84,6 @@ reference: - title: Diagnostic functions contents: - has_concept("diagnostics") +- title: Reexported functions + contents: + - reexports From f0095201d56f75e768f4eb40ee61a82cde5a6192 Mon Sep 17 00:00:00 2001 From: athowes Date: Sat, 16 Nov 2024 11:46:57 +0000 Subject: [PATCH 02/11] Use lognormal() as default and document it --- R/epidist.R | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/R/epidist.R b/R/epidist.R index 74c1632e1..c7d5035ae 100644 --- a/R/epidist.R +++ b/R/epidist.R @@ -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. @@ -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) From 5ed11899473648128016162660c1fce41a720a6c Mon Sep 17 00:00:00 2001 From: athowes Date: Sat, 16 Nov 2024 11:51:33 +0000 Subject: [PATCH 03/11] Use lognormal() not "lognormal" --- R/family.R | 2 +- tests/testthat/test-int-latent_model.R | 2 +- tests/testthat/test-utils.R | 4 ++-- vignettes/faq.Rmd | 7 ++++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/R/family.R b/R/family.R index a3758f45b..9512b9019 100644 --- a/R/family.R +++ b/R/family.R @@ -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)) diff --git a/tests/testthat/test-int-latent_model.R b/tests/testthat/test-int-latent_model.R index db269ffe1..c7f1d9870 100644 --- a/tests/testthat/test-int-latent_model.R +++ b/tests/testthat/test-int-latent_model.R @@ -86,7 +86,7 @@ test_that("epidist.epidist_latent_model Stan code has no syntax errors", { # nol set.seed(1) stancode_string <- epidist( data = prep_obs, - family = "lognormal", + family = lognormal(), seed = 1, silent = 2, refresh = 0, fn = brms::make_stancode diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index e7b602a75..ababc67de 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -40,7 +40,7 @@ test_that(".add_dpar_info works as expected for the lognormal and gamma families test_that(".make_intercepts_explicit creates a formula which is the same as if it had been explicitly created", { # nolint: line_length_linter. prep_obs <- as_epidist_latent_model(sim_obs) - epidist_family <- epidist_family(prep_obs, family = "lognormal") + epidist_family <- epidist_family(prep_obs, family = lognormal()) formula <- brms:::validate_formula( # nolint formula = brms::bf(mu ~ 1), data = prep_obs, @@ -59,7 +59,7 @@ test_that(".make_intercepts_explicit creates a formula which is the same as if i test_that(".make_intercepts_explicit does not add an intercept if the distributional parameter is set to be fixed", { # nolint: line_length_linter. prep_obs <- as_epidist_latent_model(sim_obs) - epidist_family <- epidist_family(prep_obs, family = "lognormal") + epidist_family <- epidist_family(prep_obs, family = lognormal()) formula <- brms:::validate_formula( # nolint formula = brms::bf(mu ~ 1, sigma = 1), data = prep_obs, diff --git a/vignettes/faq.Rmd b/vignettes/faq.Rmd index 5a9a9e08f..3f8081435 100644 --- a/vignettes/faq.Rmd +++ b/vignettes/faq.Rmd @@ -112,12 +112,13 @@ Instead, we used [prior predictive checking](https://mc-stan.org/docs/stan-users For example, for the `brms::lognormal()` latent individual model, we suggest the following prior distributions for the `brms` `mu` and `sigma` intercept parameters: ```{r} -family <- "lognormal" +family <- lognormal() epidist_family <- epidist_family(data, family) epidist_formula <- epidist_formula( data, - family = epidist_family, formula = mu ~ 1 + family = epidist_family, + formula = mu ~ 1 ) # NULL here means no replacing priors from the user! @@ -140,7 +141,7 @@ set.seed(1) fit_ppc <- epidist( data = data, formula = mu ~ 1, - family = "lognormal", + family = lognormal(), sample_prior = "only", seed = 1 ) From 035efeca0de5b6f3a2053fbc6d8a707c133e47a1 Mon Sep 17 00:00:00 2001 From: athowes Date: Sat, 16 Nov 2024 11:54:59 +0000 Subject: [PATCH 04/11] Stop with brms::bf and use bf --- tests/testthat/setup.R | 2 +- tests/testthat/test-formula.R | 12 ++++++------ tests/testthat/test-int-latent_model.R | 4 ++-- tests/testthat/test-latent_model.R | 2 +- tests/testthat/test-prior.R | 2 +- tests/testthat/test-utils.R | 6 +++--- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index 0ce7a4f78..2b7abb648 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -119,7 +119,7 @@ if (not_on_cran()) { 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" ) diff --git a/tests/testthat/test-formula.R b/tests/testthat/test-formula.R index f2ceeb9ba..759e4d291 100644 --- a/tests/testthat/test-formula.R +++ b/tests/testthat/test-formula.R @@ -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 @@ -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( @@ -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) ) ) }) diff --git a/tests/testthat/test-int-latent_model.R b/tests/testthat/test-int-latent_model.R index c7f1d9870..75fa34725 100644 --- a/tests/testthat/test-int-latent_model.R +++ b/tests/testthat/test-int-latent_model.R @@ -67,7 +67,7 @@ test_that("epidist.epidist_latent_model fits, the MCMC converges, and the draws set.seed(1) fit_constant <- epidist( data = prep_obs, - formula = brms::bf(mu ~ 1, sigma = 1), + formula = bf(mu ~ 1, sigma = 1), seed = 1, silent = 2, refresh = 0, cores = 2, @@ -153,7 +153,7 @@ test_that("epidist.epidist_latent_model Stan code has no syntax errors for an al skip_on_cran() stancode_sex <- epidist( data = prep_obs_sex, - formula = brms::bf(mu ~ 1 + sex, sigma ~ 1 + sex), + formula = bf(mu ~ 1 + sex, sigma ~ 1 + sex), fn = brms::make_stancode, cores = 2 ) diff --git a/tests/testthat/test-latent_model.R b/tests/testthat/test-latent_model.R index 357008fde..9d0d6e2f8 100644 --- a/tests/testthat/test-latent_model.R +++ b/tests/testthat/test-latent_model.R @@ -48,7 +48,7 @@ test_that("epidist_stancode.epidist_latent_model produces valid stanvars", { # n epidist_family <- epidist_family(prep_obs) epidist_formula <- epidist_formula( prep_obs, epidist_family, - formula = brms::bf(mu ~ 1) + formula = bf(mu ~ 1) ) stancode <- epidist_stancode( prep_obs, diff --git a/tests/testthat/test-prior.R b/tests/testthat/test-prior.R index fa77fba4e..a62fec816 100644 --- a/tests/testthat/test-prior.R +++ b/tests/testthat/test-prior.R @@ -1,7 +1,7 @@ test_that("epidist_prior with default settings produces an object of the right class", { # nolint: line_length_linter. data <- as_epidist_latent_model(sim_obs) family <- brms::lognormal() - formula <- brms::bf(mu ~ 1, sigma ~ 1) + formula <- bf(mu ~ 1, sigma ~ 1) epidist_family <- epidist_family(data, family) epidist_formula <- epidist_formula( data = data, family = epidist_family, formula = formula diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index ababc67de..67d905560 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -42,13 +42,13 @@ test_that(".make_intercepts_explicit creates a formula which is the same as if i prep_obs <- as_epidist_latent_model(sim_obs) epidist_family <- epidist_family(prep_obs, family = lognormal()) formula <- brms:::validate_formula( # nolint - formula = brms::bf(mu ~ 1), + formula = bf(mu ~ 1), data = prep_obs, family = epidist_family ) formula <- .make_intercepts_explicit(formula) formula_explicit <- brms:::validate_formula( # nolint - formula = brms::bf(mu ~ 1, sigma ~ 1), + formula = bf(mu ~ 1, sigma ~ 1), data = prep_obs, family = epidist_family ) @@ -61,7 +61,7 @@ test_that(".make_intercepts_explicit does not add an intercept if the distributi prep_obs <- as_epidist_latent_model(sim_obs) epidist_family <- epidist_family(prep_obs, family = lognormal()) formula <- brms:::validate_formula( # nolint - formula = brms::bf(mu ~ 1, sigma = 1), + formula = bf(mu ~ 1, sigma = 1), data = prep_obs, family = epidist_family ) From a2542da15e8ca996331294568e5b9e54030e21bc Mon Sep 17 00:00:00 2001 From: athowes Date: Sat, 16 Nov 2024 11:58:33 +0000 Subject: [PATCH 05/11] Remove more calls to brms::lognormal that can be lognormal --- tests/testthat/test-family.R | 2 +- tests/testthat/test-latent_model.R | 2 +- tests/testthat/test-naive_model.R | 2 +- tests/testthat/test-prior.R | 2 +- tests/testthat/test-utils.R | 2 +- vignettes/faq.Rmd | 1 + 6 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-family.R b/tests/testthat/test-family.R index 8fb1f0f46..adeb50443 100644 --- a/tests/testthat/test-family.R +++ b/tests/testthat/test-family.R @@ -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") diff --git a/tests/testthat/test-latent_model.R b/tests/testthat/test-latent_model.R index 9d0d6e2f8..579af69fc 100644 --- a/tests/testthat/test-latent_model.R +++ b/tests/testthat/test-latent_model.R @@ -10,7 +10,7 @@ test_that("as_epidist_latent_model.epidist_linelist_data errors when passed inco }) # Make this data available for other tests -family_lognormal <- epidist_family(prep_obs, family = brms::lognormal()) +family_lognormal <- epidist_family(prep_obs, family = lognormal()) test_that("is_epidist_latent_model returns TRUE for correct input", { # nolint: line_length_linter. expect_true(is_epidist_latent_model(prep_obs)) diff --git a/tests/testthat/test-naive_model.R b/tests/testthat/test-naive_model.R index b63c95865..de691d49a 100644 --- a/tests/testthat/test-naive_model.R +++ b/tests/testthat/test-naive_model.R @@ -10,7 +10,7 @@ test_that("as_epidist_naive_model.data.frame errors when passed incorrect inputs }) # Make this data available for other tests -family_lognormal <- epidist_family(sim_obs, family = brms::lognormal()) +family_lognormal <- epidist_family(sim_obs, family = lognormal()) test_that("is_epidist_naive_model returns TRUE for correct input", { # nolint: line_length_linter. expect_true(is_epidist_naive_model(prep_naive_obs)) diff --git a/tests/testthat/test-prior.R b/tests/testthat/test-prior.R index a62fec816..aab1d08f8 100644 --- a/tests/testthat/test-prior.R +++ b/tests/testthat/test-prior.R @@ -1,6 +1,6 @@ test_that("epidist_prior with default settings produces an object of the right class", { # nolint: line_length_linter. data <- as_epidist_latent_model(sim_obs) - family <- brms::lognormal() + family <- lognormal() formula <- bf(mu ~ 1, sigma ~ 1) epidist_family <- epidist_family(data, family) epidist_formula <- epidist_formula( diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 67d905560..838b34986 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -30,7 +30,7 @@ cli::test_that_cli(".replace_prior errors when passed a new prior without a matc }) test_that(".add_dpar_info works as expected for the lognormal and gamma families", { # nolint: line_length_linter. - lognormal_extra <- .add_dpar_info(brms::lognormal()) + lognormal_extra <- .add_dpar_info(lognormal()) expect_identical(lognormal_extra$other_links, "log") expect_identical(lognormal_extra$other_bounds, list(list(lb = "0", ub = ""))) gamma_extra <- .add_dpar_info(brms:::validate_family(stats::Gamma())) # nolint diff --git a/vignettes/faq.Rmd b/vignettes/faq.Rmd index 3f8081435..8121f0355 100644 --- a/vignettes/faq.Rmd +++ b/vignettes/faq.Rmd @@ -112,6 +112,7 @@ Instead, we used [prior predictive checking](https://mc-stan.org/docs/stan-users For example, for the `brms::lognormal()` latent individual model, we suggest the following prior distributions for the `brms` `mu` and `sigma` intercept parameters: ```{r} +# Note that we export lognormal() as part of epidist hence no need for brms:: family <- lognormal() epidist_family <- epidist_family(data, family) From 6685e2545d39e872a8790a0bfb6693682f6a3938 Mon Sep 17 00:00:00 2001 From: athowes Date: Sat, 16 Nov 2024 11:59:30 +0000 Subject: [PATCH 06/11] We are also exporting stats::Gamma --- tests/testthat/setup.R | 2 +- tests/testthat/test-int-latent_model.R | 4 ++-- tests/testthat/test-utils.R | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index 2b7abb648..1d638f3a9 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -112,7 +112,7 @@ 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" ) diff --git a/tests/testthat/test-int-latent_model.R b/tests/testthat/test-int-latent_model.R index 75fa34725..e0d7db070 100644 --- a/tests/testthat/test-int-latent_model.R +++ b/tests/testthat/test-int-latent_model.R @@ -29,7 +29,7 @@ test_that("epidist.epidist_latent_model samples from the prior according to marg cores = 2 ) pred <- predict_delay_parameters(prior_samples) - family <- brms::lognormal() + family <- lognormal() epidist_family <- epidist_family(data = prep_obs, family = family) epidist_formula <- epidist_formula( data = prep_obs, @@ -111,7 +111,7 @@ test_that("epidist.epidist_latent_model Stan code has no syntax errors and compi skip_on_cran() stancode_gamma <- epidist( data = prep_obs_gamma, - family = stats::Gamma(link = "log"), + family = Gamma(link = "log"), formula = mu ~ 1, cores = 2, fn = brms::make_stancode diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 838b34986..f0012d6ed 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -33,7 +33,7 @@ test_that(".add_dpar_info works as expected for the lognormal and gamma families lognormal_extra <- .add_dpar_info(lognormal()) expect_identical(lognormal_extra$other_links, "log") expect_identical(lognormal_extra$other_bounds, list(list(lb = "0", ub = ""))) - gamma_extra <- .add_dpar_info(brms:::validate_family(stats::Gamma())) # nolint + gamma_extra <- .add_dpar_info(brms:::validate_family(Gamma())) # nolint expect_null(gamma_extra$other_links) expect_identical(gamma_extra$other_bounds, list(list(lb = "0", ub = ""))) }) From bc9c953a6802311f620a77c7f506117c25ca63da Mon Sep 17 00:00:00 2001 From: athowes Date: Sat, 16 Nov 2024 12:00:18 +0000 Subject: [PATCH 07/11] Another one --- tests/testthat/test-formula.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-formula.R b/tests/testthat/test-formula.R index 759e4d291..c93f8f829 100644 --- a/tests/testthat/test-formula.R +++ b/tests/testthat/test-formula.R @@ -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( From 2fe98c7ce01044276bd3494eaf7590577c94cfc2 Mon Sep 17 00:00:00 2001 From: athowes Date: Sat, 16 Nov 2024 12:01:32 +0000 Subject: [PATCH 08/11] Document --- man/dot-add_dpar_info.Rd | 5 +++-- man/epidist.Rd | 5 +++-- man/epidist.default.Rd | 7 ++++--- man/epidist_family.Rd | 7 ++++--- man/epidist_family_prior.Rd | 5 +++-- man/epidist_family_prior.default.Rd | 5 +++-- man/epidist_family_prior.lognormal.Rd | 5 +++-- man/epidist_family_reparam.Rd | 5 +++-- man/epidist_family_reparam.default.Rd | 5 +++-- man/epidist_family_reparam.gamma.Rd | 5 +++-- man/reexports.Rd | 21 +++++++++++++++++++++ 11 files changed, 53 insertions(+), 22 deletions(-) create mode 100644 man/reexports.Rd diff --git a/man/dot-add_dpar_info.Rd b/man/dot-add_dpar_info.Rd index 444cbbc75..ac4460d66 100644 --- a/man/dot-add_dpar_info.Rd +++ b/man/dot-add_dpar_info.Rd @@ -10,8 +10,9 @@ \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} } \description{ Includes additional information (link functions and parameter bound) about diff --git a/man/epidist.Rd b/man/epidist.Rd index f6663d66c..a9a4ab7c0 100644 --- a/man/epidist.Rd +++ b/man/epidist.Rd @@ -18,8 +18,9 @@ parameters.} \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{prior}{One or more \code{brmsprior} objects created by \code{\link[brms:set_prior]{brms::set_prior()}} or related functions. These priors are passed to \code{\link[=epidist_prior]{epidist_prior()}} in the diff --git a/man/epidist.default.Rd b/man/epidist.default.Rd index 18353ca08..3de4f4b8f 100644 --- a/man/epidist.default.Rd +++ b/man/epidist.default.Rd @@ -7,7 +7,7 @@ \method{epidist}{default}( data, formula = mu ~ 1, - family = "lognormal", + family = lognormal(), prior = NULL, fn = brms::brm, ... @@ -25,8 +25,9 @@ parameters.} \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{prior}{One or more \code{brmsprior} objects created by \code{\link[brms:set_prior]{brms::set_prior()}} or related functions. These priors are passed to \code{\link[=epidist_prior]{epidist_prior()}} in the diff --git a/man/epidist_family.Rd b/man/epidist_family.Rd index bb4bdc615..ba7960fc7 100644 --- a/man/epidist_family.Rd +++ b/man/epidist_family.Rd @@ -4,7 +4,7 @@ \alias{epidist_family} \title{Define \code{epidist} family} \usage{ -epidist_family(data, family = "lognormal", ...) +epidist_family(data, family = lognormal(), ...) } \arguments{ \item{data}{A \code{data.frame} containing line list data.} @@ -12,8 +12,9 @@ epidist_family(data, family = "lognormal", ...) \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{...}{Additional arguments passed to \code{fn} method.} } diff --git a/man/epidist_family_prior.Rd b/man/epidist_family_prior.Rd index 82ce122dd..48344ce2b 100644 --- a/man/epidist_family_prior.Rd +++ b/man/epidist_family_prior.Rd @@ -10,8 +10,9 @@ epidist_family_prior(family, ...) \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{...}{Additional arguments passed to \code{fn} method.} } diff --git a/man/epidist_family_prior.default.Rd b/man/epidist_family_prior.default.Rd index eb2677626..3139ac0a4 100644 --- a/man/epidist_family_prior.default.Rd +++ b/man/epidist_family_prior.default.Rd @@ -10,8 +10,9 @@ \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{formula}{An object of class \link[stats:formula]{stats::formula} or \link[brms:brmsformula]{brms::brmsformula} (or one that can be coerced to those classes). A symbolic description of the diff --git a/man/epidist_family_prior.lognormal.Rd b/man/epidist_family_prior.lognormal.Rd index 2708ba9ff..2830dd82e 100644 --- a/man/epidist_family_prior.lognormal.Rd +++ b/man/epidist_family_prior.lognormal.Rd @@ -10,8 +10,9 @@ \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{formula}{An object of class \link[stats:formula]{stats::formula} or \link[brms:brmsformula]{brms::brmsformula} (or one that can be coerced to those classes). A symbolic description of the diff --git a/man/epidist_family_reparam.Rd b/man/epidist_family_reparam.Rd index 27ecba0a4..13a39547b 100644 --- a/man/epidist_family_reparam.Rd +++ b/man/epidist_family_reparam.Rd @@ -10,8 +10,9 @@ epidist_family_reparam(family, ...) \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{...}{Additional arguments passed to \code{fn} method.} } diff --git a/man/epidist_family_reparam.default.Rd b/man/epidist_family_reparam.default.Rd index 4fc4531de..8ab4d39ce 100644 --- a/man/epidist_family_reparam.default.Rd +++ b/man/epidist_family_reparam.default.Rd @@ -10,8 +10,9 @@ \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{...}{Additional arguments passed to \code{fn} method.} } diff --git a/man/epidist_family_reparam.gamma.Rd b/man/epidist_family_reparam.gamma.Rd index 70acfa454..090cae603 100644 --- a/man/epidist_family_reparam.gamma.Rd +++ b/man/epidist_family_reparam.gamma.Rd @@ -10,8 +10,9 @@ \item{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 \code{\link[=brmsfamily]{brmsfamily()}}.} +If not specified, default links are used. For details of all supported +families see \code{\link[=brmsfamily]{brmsfamily()}}. Commonly used, such as \code{\link[=lognormal]{lognormal()}}, are also +reexported as part of \code{epidist}.} \item{...}{Additional arguments passed to \code{fn} method.} } diff --git a/man/reexports.Rd b/man/reexports.Rd new file mode 100644 index 000000000..afd1c9f79 --- /dev/null +++ b/man/reexports.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/reexports.R +\docType{import} +\name{reexports} +\alias{reexports} +\alias{lognormal} +\alias{weibull} +\alias{Gamma} +\alias{bf} +\title{Objects exported from other packages} +\keyword{internal} +\description{ +These objects are imported from other packages. Follow the links +below to see their documentation. + +\describe{ + \item{brms}{\code{\link[brms:brmsformula]{bf}}, \code{\link[brms:brmsfamily]{lognormal}}, \code{\link[brms:brmsfamily]{weibull}}} + + \item{stats}{\code{\link[stats:family]{Gamma}}} +}} + From 7d278a1ef50a9e1fb698c3c13c53532c30c114c1 Mon Sep 17 00:00:00 2001 From: athowes Date: Mon, 18 Nov 2024 10:55:13 +0000 Subject: [PATCH 09/11] Use predictions in test of sex effect --- tests/testthat/test-int-latent_model.R | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-int-latent_model.R b/tests/testthat/test-int-latent_model.R index e0d7db070..93b74179f 100644 --- a/tests/testthat/test-int-latent_model.R +++ b/tests/testthat/test-int-latent_model.R @@ -167,9 +167,21 @@ test_that("epidist.epidist_latent_model recovers a sex effect", { # nolint: line # Note: this test is stochastic. See note at the top of this script skip_on_cran() set.seed(1) - draws <- posterior::as_draws_df(fit_sex$fit) - expect_equal(mean(draws$b_sex), -0.73, tolerance = 0.2) - expect_equal(mean(draws$b_sigma_sex), 0.43, tolerance = 0.2) + sex_effect <- predict_delay_parameters(fit_sex) |> + left_join( + mutate(sim_obs_sex, index = row_number()) |> + select(index, sex), + by = "index" + ) |> + group_by(sex) |> + summarise( + mu = mean(mu), + sigma = mean(sigma) + ) + expect_equal(sex_effect$mu[1], meanlog_m, tolerance = 0.1) + expect_equal(sex_effect$sigma[1], sdlog_m, tolerance = 0.1) + expect_equal(sex_effect$mu[2], meanlog_f, tolerance = 0.1) + expect_equal(sex_effect$sigma[2], sdlog_f, tolerance = 0.1) expect_s3_class(fit_sex, "brmsfit") expect_s3_class(fit_sex, "epidist_fit") expect_convergence(fit_sex) From 32ec9a6e504a288947badf5d836fc9bec05a94ad Mon Sep 17 00:00:00 2001 From: athowes Date: Mon, 18 Nov 2024 11:06:07 +0000 Subject: [PATCH 10/11] Can do this without predict_delay_pars --- tests/testthat/test-int-latent_model.R | 27 ++++++++++++-------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/tests/testthat/test-int-latent_model.R b/tests/testthat/test-int-latent_model.R index 93b74179f..e4cd0109a 100644 --- a/tests/testthat/test-int-latent_model.R +++ b/tests/testthat/test-int-latent_model.R @@ -167,21 +167,18 @@ test_that("epidist.epidist_latent_model recovers a sex effect", { # nolint: line # Note: this test is stochastic. See note at the top of this script skip_on_cran() set.seed(1) - sex_effect <- predict_delay_parameters(fit_sex) |> - left_join( - mutate(sim_obs_sex, index = row_number()) |> - select(index, sex), - by = "index" - ) |> - group_by(sex) |> - summarise( - mu = mean(mu), - sigma = mean(sigma) - ) - expect_equal(sex_effect$mu[1], meanlog_m, tolerance = 0.1) - expect_equal(sex_effect$sigma[1], sdlog_m, tolerance = 0.1) - expect_equal(sex_effect$mu[2], meanlog_f, tolerance = 0.1) - expect_equal(sex_effect$sigma[2], sdlog_f, tolerance = 0.1) + draws <- posterior::as_draws_df(fit_sex$fit) + expect_equal(mean(draws$b_Intercept), meanlog_m, tolerance = 0.1) + expect_equal( + mean(draws$b_Intercept + draws$b_sex), meanlog_f, + tolerance = 0.1 + ) + expect_equal(mean(exp(draws$b_sigma_Intercept)), sdlog_m, tolerance = 0.1) + expect_equal( + mean(exp(draws$b_sigma_Intercept + draws$b_sigma_sex)), + sdlog_f, + tolerance = 0.1 + ) expect_s3_class(fit_sex, "brmsfit") expect_s3_class(fit_sex, "epidist_fit") expect_convergence(fit_sex) From 9c96cb5dc1584763fefd0d56a01f59aa16f7b549 Mon Sep 17 00:00:00 2001 From: athowes Date: Mon, 18 Nov 2024 11:48:11 +0000 Subject: [PATCH 11/11] Change to link function here --- tests/testthat/test-int-latent_model.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-int-latent_model.R b/tests/testthat/test-int-latent_model.R index e4cd0109a..5f8cc3842 100644 --- a/tests/testthat/test-int-latent_model.R +++ b/tests/testthat/test-int-latent_model.R @@ -45,7 +45,7 @@ test_that("epidist.epidist_latent_model samples from the prior according to marg param1 <- extract_normal_parameters_brms(epidist_prior[1, ]) param2 <- extract_normal_parameters_brms(epidist_prior[2, ]) samples1 <- rnorm(1000, mean = param1$mean, sd = param1$sd) - samples2 <- rnorm(1000, mean = param2$mean, sd = param2$sd) + samples2 <- exp(rnorm(1000, mean = param2$mean, sd = param2$sd)) # suppressWarnings here used to prevent warnings about ties ks1 <- suppressWarnings(stats::ks.test(pred$mu, samples1)) ks2 <- suppressWarnings(stats::ks.test(pred$sigma, samples2))