Skip to content

Commit

Permalink
Issue #372: Refactor formula dispatch (#383)
Browse files Browse the repository at this point in the history
* Move formula functions into formula.R

* Put formula functions into group together

* Refactor to use epidist_formula then dispatch into model-specific parts
  • Loading branch information
athowes authored Oct 14, 2024
1 parent 339a5ae commit 9cc8abd
Show file tree
Hide file tree
Showing 26 changed files with 194 additions and 168 deletions.
5 changes: 3 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ S3method(epidist_family_prior,default)
S3method(epidist_family_prior,lognormal)
S3method(epidist_family_reparam,default)
S3method(epidist_family_reparam,gamma)
S3method(epidist_formula,default)
S3method(epidist_formula,epidist_latent_individual)
S3method(epidist_formula_model,default)
S3method(epidist_formula_model,epidist_latent_individual)
S3method(epidist_model_prior,default)
S3method(epidist_stancode,default)
S3method(epidist_stancode,epidist_latent_individual)
Expand All @@ -27,6 +27,7 @@ export(epidist_family_model)
export(epidist_family_prior)
export(epidist_family_reparam)
export(epidist_formula)
export(epidist_formula_model)
export(epidist_model_prior)
export(epidist_prior)
export(epidist_stancode)
Expand Down
13 changes: 0 additions & 13 deletions R/defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ epidist_validate.default <- function(data, ...) {
)
}

#' Default method for defining a model specific formula
#'
#' @inheritParams epidist_formula
#' @param ... Additional arguments passed to method.
#' @family defaults
#' @export
epidist_formula.default <- function(data, ...) {
cli_abort(
"No epidist_formula method implemented for the class ", class(data), "\n",
"See methods(epidist_formula) for available methods"
)
}

#' Default method for defining model specific Stan code
#'
#' @inheritParams epidist_stancode
Expand Down
2 changes: 1 addition & 1 deletion R/epidist-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#' @importFrom brms bf prior
#' @importFrom checkmate assert_data_frame assert_names assert_integer
#' assert_true assert_factor assert_numeric
#' @importFrom cli cli_abort cli_inform
#' @importFrom cli cli_abort cli_inform cli_abort
## usethis namespace: end
NULL
7 changes: 2 additions & 5 deletions R/family.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ epidist_family_model <- function(data, family, ...) {
#' @param ... Additional arguments passed to method.
#' @family family
#' @export
epidist_family_model.default <- function(data, ...) {
cli_abort(
"No epidist_family_model method implemented for the class ", class(data),
"\n", "See methods(epidist_family_model) for available methods"
)
epidist_family_model.default <- function(data, family, ...) {
return(family)
}

#' Reparameterise an `epidist` family to align `brms` and Stan
Expand Down
45 changes: 45 additions & 0 deletions R/formula.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#' Define a model specific formula
#'
#' This function is used within [epidist()] to create the formula object passed
#' to `brms`. It is unlikely that as a user you will need this function, but we
#' export it nonetheless to be transparent about what exactly is happening
#' inside of a call to [epidist()].
#'
#' @param data A `data.frame` containing line list data
#' @param family Output of a call to `brms::brmsfamily()`
#' @param formula As produced by [brms::brmsformula()]
#' @param ... ...
#'
#' @family formula
#' @export
epidist_formula <- function(data, family, formula, ...) {
epidist_validate(data)
formula <- brms:::validate_formula(formula, data = data, family = family)
formula <- .make_intercepts_explicit(formula)
formula <- epidist_formula_model(data, formula)
# Using this here for checking purposes
bterms <- brms::brmsterms(formula)
brms:::validate_data(data, bterms)
return(formula)
}

#' The model-specific parts of an `epidist_formula()` call
#'
#' @inheritParams epidist_formula
#' @param ... Additional arguments passed to method.
#' @rdname epidist_family_model
#' @family formula
#' @export
epidist_formula_model <- function(data, formula, ...) {
UseMethod("epidist_formula_model")
}

#' Default method for defining a model specific formula
#'
#' @inheritParams epidist_formula
#' @param ... Additional arguments passed to method.
#' @family formula
#' @export
epidist_formula_model.default <- function(data, formula, ...) {
return(formula)
}
15 changes: 0 additions & 15 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,6 @@ epidist_validate <- function(data) {
UseMethod("epidist_validate")
}

#' Define a model specific formula
#'
#' This function is used within [epidist()] to create the formula object passed
#' to `brms`. It is unlikely that as a user you will need this function, but we
#' export it nonetheless to be transparent about what exactly is happening
#' inside of a call to [epidist()].
#'
#' @inheritParams epidist_validate
#' @param ... Additional arguments passed to method.
#' @family generics
#' @export
epidist_formula <- function(data, ...) {
UseMethod("epidist_formula")
}

#' Define model specific Stan code
#'
#' This function is used within [epidist()] to create any custom Stan code which
Expand Down
19 changes: 6 additions & 13 deletions R/latent_individual.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,21 @@ epidist_family_model.epidist_latent_individual <- function(
return(custom_family)
}

#' Define a formula for the latent_individual model
#' Define the model-specific component of an `epidist` custom formula
#'
#' @param data A `data.frame` containing line list data
#' @param family The output of [epidist_family()]
#' @param formula As produced by [brms::brmsformula()]
#' @param ... ...
#' @method epidist_formula epidist_latent_individual
#' @method epidist_formula_model epidist_latent_individual
#' @family latent_individual
#' @export
epidist_formula.epidist_latent_individual <- function(data, family, formula,
...) {
epidist_validate(data)
formula <- brms:::validate_formula(formula, data = data, family = family)
formula <- .make_intercepts_explicit(formula)
epidist_formula_model.epidist_latent_individual <- function(
data, formula, ...
) {
# data is only used to dispatch on
formula <- stats::update(
formula, delay | vreal(relative_obs_time, pwindow, swindow) ~ .
)

# Using this here for checking purposes
bterms <- brms::brmsterms(formula)
brms:::validate_data(data, bterms)

return(formula)
}

Expand Down
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ reference:
desc: Functions related to specifying custom `brms` families
contents:
- has_concept("family")
- title: Formula
desc: Functions related to specifying custom `brms` formula
contents:
- has_concept("formula")
- title: Priors
desc: Functions for specifying prior distributions
contents:
Expand Down
4 changes: 2 additions & 2 deletions man/as_latent_individual.Rd

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

1 change: 0 additions & 1 deletion man/epidist.Rd

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

1 change: 0 additions & 1 deletion man/epidist.default.Rd

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

14 changes: 13 additions & 1 deletion man/epidist_family_model.Rd

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

5 changes: 4 additions & 1 deletion man/epidist_family_model.default.Rd

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

2 changes: 1 addition & 1 deletion man/epidist_family_model.epidist_latent_individual.Rd

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

21 changes: 12 additions & 9 deletions man/epidist_formula.Rd

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

23 changes: 0 additions & 23 deletions man/epidist_formula.default.Rd

This file was deleted.

24 changes: 24 additions & 0 deletions man/epidist_formula_model.default.Rd

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

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

1 change: 0 additions & 1 deletion man/epidist_stancode.Rd

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

Loading

0 comments on commit 9cc8abd

Please sign in to comment.