Skip to content

Commit

Permalink
Issue 122: Check family input valid (#144)
Browse files Browse the repository at this point in the history
      * Add string and LPDF checker for epidist_family

* Add unit tests

* Correct the namespace handling

* Remove whitespace
  • Loading branch information
athowes authored Jul 11, 2024
1 parent 17219e8 commit 476c36e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ Imports:
ggridges,
here,
stats,
cli
cli,
checkmate,
rstan
Suggests:
bookdown,
testthat (>= 3.0.0),
Expand Down
13 changes: 13 additions & 0 deletions R/latent_individual.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,24 @@ epidist_formula.epidist_latent_individual <- function(data, delay_central = ~ 1,
return(form)
}

#' @importFrom rstan lookup
#' @method epidist_family epidist_latent_individual
#' @family latent_individual
#' @export
epidist_family.epidist_latent_individual <- function(data, family = "lognormal",
...) {
checkmate::assert_string(family)

pdf_lookup <- rstan::lookup("pdf")
valid_pdfs <- gsub("_lpdf", "", pdf_lookup$StanFunction)
if (!family %in% valid_pdfs) {
cli::cli_warn(
"The provided family {.code family} does not correspond to a valid LPDF
function available in rstan. (It is possible [but unlikely] that there is
such an LPDF in Stan available via cmdstanr, as rstan is behind Stan.)"
)
}

brms::custom_family(
paste0("latent_", family),
dpars = c("mu", "sigma"),
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-unit-latent_individual.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ test_that("epidist_family.epidist_latent_individual with default settings produc
expect_s3_class(family, "family")
})

test_that("epidist_family.epidist_latent_individual warns users or gives an error when passed inappropriate family input", { # nolint: line_length_linter.
expect_error(epidist_family(prep_obs, family = 1))
expect_warning(epidist_family(prep_obs, family = "not_a_real_lpdf"))
})

test_that("the family argument in epidist_family.epidist_latent_individual passes as expected", { # nolint: line_length_linter.
family_gamma <- epidist_family(prep_obs, family = "gamma")
expect_equal(family_gamma$name, "latent_gamma")
Expand Down

0 comments on commit 476c36e

Please sign in to comment.