From afdbf0875bf7e0671c52edd6a4ab390ebdfc73d4 Mon Sep 17 00:00:00 2001 From: athowes Date: Mon, 18 Nov 2024 12:06:39 +0000 Subject: [PATCH] Move to marginal model name and lint --- R/cohort_model.R | 50 ------------------------------- R/marginal_model.R | 55 ++++++++++++++++++++++++++++++++++ _pkgdown.yml | 4 +++ vignettes/approx-inference.Rmd | 2 +- vignettes/ebola.Rmd | 2 +- 5 files changed, 61 insertions(+), 52 deletions(-) delete mode 100644 R/cohort_model.R create mode 100644 R/marginal_model.R diff --git a/R/cohort_model.R b/R/cohort_model.R deleted file mode 100644 index d1d25d91e..000000000 --- a/R/cohort_model.R +++ /dev/null @@ -1,50 +0,0 @@ -#' Prepare cohort model -#' -#' @param data A `data.frame` containing line list data -#' @family cohort_model -#' @export -as_cohort_model <- function(data) { - UseMethod("as_cohort_model") -} - -assert_cohort_model_input <- function(data) { - # ... -} - -#' Prepare cohort model -#' -#' @param data A `data.frame` containing line list data -#' @rdname as_cohort_model -#' @method as_cohort_model data.frame -#' @family cohort_model -#' @autoglobal -#' @export -as_cohort_model.data.frame <- function(data) { - assert_cohort_model_input(data) - class(data) <- c("epidist_cohort_model", class(data)) - data <- data |> - mutate(delay = .data$stime - .data$ptime) - epidist_validate(data) - return(data) -} - -#' Validate cohort model data -#' -#' @param data A `data.frame` containing line list data -#' @param ... ... -#' @method epidist_validate epidist_cohort_model -#' @family cohort_model -#' @export -epidist_validate.epidist_cohort_model <- function(data, ...) { - assert_true(is_cohort_model(data)) - assert_cohort_model_input(data) -} - -#' Check if data has the `epidist_cohort_model` class -#' -#' @param data A `data.frame` containing line list data -#' @family cohort_model -#' @export -is_cohort_model <- function(data) { - inherits(data, "epidist_cohort_model") -} diff --git a/R/marginal_model.R b/R/marginal_model.R new file mode 100644 index 000000000..cae63adf6 --- /dev/null +++ b/R/marginal_model.R @@ -0,0 +1,55 @@ +#' Prepare marginal model to pass through to `brms` +#' +#' @param data A `data.frame` containing line list data +#' @family marginal_model +#' @export +as_epidist_marginal_model <- function(data) { + UseMethod("as_epidist_marginal_model") +} + +#' The marginal model method for `epidist_linelist_data` objects +#' +#' @param data An `epidist_linelist_data` object +#' @method as_epidist_marginal_model epidist_linelist_data +#' @family marginal_model +#' @autoglobal +#' @export +as_epidist_marginal_model.epidist_linelist_data <- function(data) { + assert_epidist(data) + + data <- data |> + mutate(delay = .data$stime_lwr - .data$ptime_lwr) + + data <- new_epidist_marginal_model(data) + assert_epidist(data) + return(data) +} + +#' Class constructor for `epidist_marginal_model` objects +#' +#' @param data A data.frame to convert +#' @returns An object of class `epidist_marginal_model` +#' @family marginal_model +#' @export +new_epidist_marginal_model <- function(data) { + class(data) <- c("epidist_marginal_model", class(data)) + return(data) +} + +#' @method assert_epidist epidist_marginal_model +#' @family marginal_model +#' @export +assert_epidist.epidist_marginal_model <- function(data, ...) { + assert_data_frame(data) + assert_names(names(data), must.include = "delay") + assert_numeric(data$delay, lower = 0) +} + +#' Check if data has the `epidist_marginal_model` class +#' +#' @param data A `data.frame` containing line list data +#' @family marginal_model +#' @export +is_epidist_marginal_model <- function(data) { + inherits(data, "epidist_marginal_model") +} diff --git a/_pkgdown.yml b/_pkgdown.yml index b93d1a6ee..e2c7724a5 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -65,6 +65,10 @@ reference: desc: Functions for fitting delay distribution models using `brms` contents: - has_concept("fit") +- title: Marginal model + desc: Specific methods for the marginal model + contents: + - has_concept("marginal_model") - title: Latent model desc: Specific methods for the latent model contents: diff --git a/vignettes/approx-inference.Rmd b/vignettes/approx-inference.Rmd index c8fd2f3af..215487606 100644 --- a/vignettes/approx-inference.Rmd +++ b/vignettes/approx-inference.Rmd @@ -99,7 +99,7 @@ library(purrr) library(tidyr) library(tibble) library(tidybayes) -library(cmdstanr) +library(cmdstanr) # nolint ``` To access the approximate inference methods used in this vignette we will need to use the `cmdstanr` backend for `brms` (we generally recommend using this backend for fitting models). To do this, we first need to install CmdStan (see the README for more details). We can check we have everything we need as follows: diff --git a/vignettes/ebola.Rmd b/vignettes/ebola.Rmd index 0da1d6bfe..536ebd4f5 100644 --- a/vignettes/ebola.Rmd +++ b/vignettes/ebola.Rmd @@ -53,7 +53,7 @@ library(tidybayes) library(modelr) library(patchwork) library(lubridate) -library(cmdstanr) +library(cmdstanr) # nolint ``` For users new to `epidist`, before reading this article we recommend beginning with the "[Getting started with `epidist`](http://epidist.epinowcast.org/articles/epidist.html)" vignette.