Skip to content

Commit

Permalink
Document and lint, plus move default formula to delay ~ .
Browse files Browse the repository at this point in the history
  • Loading branch information
athowes committed Oct 17, 2024
1 parent c38f18e commit d25ad2a
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 21 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
S3method(add_mean_sd,default)
S3method(add_mean_sd,gamma_samples)
S3method(add_mean_sd,lognormal_samples)
S3method(as_direct_model,data.frame)
S3method(as_latent_individual,data.frame)
S3method(epidist,default)
S3method(epidist_family_model,default)
Expand All @@ -17,9 +18,11 @@ S3method(epidist_model_prior,default)
S3method(epidist_stancode,default)
S3method(epidist_stancode,epidist_latent_individual)
S3method(epidist_validate,default)
S3method(epidist_validate,epidist_direct_model)
S3method(epidist_validate,epidist_latent_individual)
export(add_event_vars)
export(add_mean_sd)
export(as_direct_model)
export(as_latent_individual)
export(epidist)
export(epidist_diagnostics)
Expand All @@ -35,6 +38,7 @@ export(epidist_stancode)
export(epidist_validate)
export(filter_obs_by_obs_time)
export(filter_obs_by_ptime)
export(is_direct_model)
export(is_latent_individual)
export(observe_process)
export(predict_delay_parameters)
Expand Down
47 changes: 37 additions & 10 deletions R/direct_model.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#' Prepare direct model to pass through to `brms`
#'
#' @param data A `data.frame` containing line list data
#' @family direct_model
#' @export
as_direct_model <- function(data) {
UseMethod("as_direct_model")
}
Expand All @@ -10,6 +15,21 @@ assert_direct_model_input <- function(data) {
assert_numeric(data$stime, lower = 0)
}

#' Prepare latent individual model
#'
#' This function prepares data for use with the direct model. It does this by
#' adding columns used in the model to the `data` object provided. To do this,
#' the `data` must already have columns for the case number (integer),
#' (positive, numeric) times for the primary and secondary event times. The
#' output of this function is a `epidist_direct_model` class object, which may
#' be passed to [epidist()] to perform inference for the model.
#'
#' @param data A `data.frame` containing line list data
#' @rdname as_direct_model
#' @method as_direct_model data.frame
#' @family direct_model
#' @autoglobal
#' @export
as_direct_model.data.frame <- function(data) {
assert_direct_model_input(data)
class(data) <- c("epidist_direct_model", class(data))
Expand All @@ -19,23 +39,30 @@ as_direct_model.data.frame <- function(data) {
return(data)
}

#' Validate direct model data
#'
#' This function checks whether the provided `data` object is suitable for
#' running the direct model. As well as making sure that
#' `is_direct_model()` is true, it also checks that `data` is a `data.frame`
#' with the correct columns.
#'
#' @param data A `data.frame` containing line list data
#' @param ... ...
#' @method epidist_validate epidist_direct_model
#' @family direct_model
#' @export
epidist_validate.epidist_direct_model <- function(data, ...) {
assert_true(is_direct_model(data))
assert_direct_model_input(data)
assert_names(names(data), must.include = c("case", "ptime", "stime", "delay"))
assert_numeric(data$delay, lower = 0)
}

#' Check if data has the `epidist_direct_model` class
#'
#' @param data A `data.frame` containing line list data
#' @family latent_individual
#' @export
is_direct_model <- function(data) {
inherits(data, "epidist_direct_model")
}

epidist_formula_model.epidist_direct_model <- function(
data, formula, ...
) {
# data is only used to dispatch on
formula <- stats::update(
formula, delay ~ .
)
return(formula)
}
3 changes: 3 additions & 0 deletions R/formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ epidist_formula_model <- function(data, formula, ...) {
#' @family formula
#' @export
epidist_formula_model.default <- function(data, formula, ...) {
formula <- stats::update(
formula, delay ~ .
)
return(formula)
}
30 changes: 30 additions & 0 deletions man/as_direct_model.Rd

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

2 changes: 2 additions & 0 deletions man/as_latent_individual.Rd

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

10 changes: 5 additions & 5 deletions man/epidist.Rd

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

10 changes: 5 additions & 5 deletions man/epidist.default.Rd

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

1 change: 1 addition & 0 deletions 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.

1 change: 1 addition & 0 deletions man/epidist_formula_model.epidist_latent_individual.Rd

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

24 changes: 24 additions & 0 deletions man/epidist_validate.epidist_direct_model.Rd

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

1 change: 1 addition & 0 deletions man/epidist_validate.epidist_latent_individual.Rd

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

23 changes: 23 additions & 0 deletions man/is_direct_model.Rd

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

3 changes: 2 additions & 1 deletion man/is_latent_individual.Rd

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

0 comments on commit d25ad2a

Please sign in to comment.