Skip to content

Commit

Permalink
Issue 72: Rename model (#97)
Browse files Browse the repository at this point in the history
* Rename Stan folder to latent_individual

* Rename ltcad to latent_individual in R code

* Rename ltcad in vignette

* Update document

* Update pkgdown
  • Loading branch information
athowes authored Jun 10, 2024
1 parent 1bfd4fb commit f308cd0
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 59 deletions.
10 changes: 5 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

S3method(epidist,default)
S3method(epidist_family,default)
S3method(epidist_family,epidist_ltcad)
S3method(epidist_family,epidist_latent_individual)
S3method(epidist_formula,default)
S3method(epidist_formula,epidist_ltcad)
S3method(epidist_formula,epidist_latent_individual)
S3method(epidist_prepare,default)
S3method(epidist_prepare,epidist_ltcad)
S3method(epidist_prepare,epidist_latent_individual)
S3method(epidist_priors,default)
S3method(epidist_priors,epidist_ltcad)
S3method(epidist_priors,epidist_latent_individual)
S3method(epidist_stancode,default)
S3method(epidist_stancode,epidist_ltcad)
S3method(epidist_stancode,epidist_latent_individual)
export(add_natural_scale_mean_sd)
export(calculate_censor_delay)
export(calculate_cohort_mean)
Expand Down
4 changes: 2 additions & 2 deletions R/defaults.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' Default method used when preparing data
#'
#' @param model Character string, model type to prepare to use.
#' Supported options are "ltcad".
#' Supported options are "latent_individual".
#' @param ... Additional arguments passed to model specific `epidist_prepare`
#' functions
#' @rdname epidist_prepare
#' @method epidist_prepare default
#' @family defaults
#' @export
epidist_prepare.default <- function(data, model, ...) {
model <- match.arg(model, choices = c("ltcad"))
model <- match.arg(model, choices = c("latent_individual"))
class(data) <- c(class(data), paste0("epidist_", model))
epidist_prepare(data, ...)
}
Expand Down
40 changes: 20 additions & 20 deletions R/ltcad.R → R/latent_individual.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @method epidist_prepare epidist_ltcad
#' @family ltcad
#' @method epidist_prepare epidist_latent_individual
#' @family latent_individual
#' @export
epidist_prepare.epidist_ltcad <- function(data, ...) {
epidist_prepare.epidist_latent_individual <- function(data, ...) {
id <- obs_t <- obs_at <- ptime_lwr <- pwindow_upr <- stime_lwr <- NULL
stime_upr <- woverlap <- swindow_upr <- stime_upr <- delay_central <- NULL
row_id <- ptime_upr <- NULL
Expand All @@ -25,24 +25,24 @@ epidist_prepare.epidist_ltcad <- function(data, ...) {
return(data)
}

#' @method epidist_priors epidist_ltcad
#' @family ltcad
#' @method epidist_priors epidist_latent_individual
#' @family latent_individual
#' @export
epidist_priors.epidist_ltcad <- function(data, ...) {
epidist_priors.epidist_latent_individual <- function(data, ...) {
return(NULL)
}

#' Define a formula for the ltcad model
#' Define a formula for the latent_individual model
#'
#' @param data ...
#' @param delay_central Formula for the delay mean. Defaults to intercept only.
#' @param sigma Formula for the delay standard deviation. Defaults to intercept
#' only.
#' @param ... ...
#' @method epidist_formula epidist_ltcad
#' @family ltcad
#' @method epidist_formula epidist_latent_individual
#' @family latent_individual
#' @export
epidist_formula.epidist_ltcad <- function(data, delay_central = ~ 1,
epidist_formula.epidist_latent_individual <- function(data, delay_central = ~ 1,
sigma = ~ 1, ...) {
delay_equation <- paste0(
"delay_central | vreal(obs_t, pwindow_upr, swindow_upr)",
Expand All @@ -54,10 +54,10 @@ epidist_formula.epidist_ltcad <- function(data, delay_central = ~ 1,
return(form)
}

#' @method epidist_family epidist_ltcad
#' @family ltcad
#' @method epidist_family epidist_latent_individual
#' @family latent_individual
#' @export
epidist_family.epidist_ltcad <- function(data, family = "lognormal", ...) {
epidist_family.epidist_latent_individual <- function(data, family = "lognormal", ...) {
brms::custom_family(
paste0("latent_", family),
dpars = c("mu", "sigma"),
Expand All @@ -70,15 +70,15 @@ epidist_family.epidist_ltcad <- function(data, family = "lognormal", ...) {
)
}

#' @method epidist_stancode epidist_ltcad
#' @family ltcad
#' @method epidist_stancode epidist_latent_individual
#' @family latent_individual
#' @export
epidist_stancode.epidist_ltcad <- function(data,
epidist_stancode.epidist_latent_individual <- function(data,
family = epidist_family(data), ...) {
stanvars_version <- epidist_version_stanvar()

stanvars_functions <- brms::stanvar(
block = "functions", scode = epidist_stan_chunk("ltcad/functions.stan")
block = "functions", scode = epidist_stan_chunk("latent_individual/functions.stan")
)

family_name <- gsub("latent_", "", family$name)
Expand Down Expand Up @@ -107,15 +107,15 @@ epidist_stancode.epidist_ltcad <- function(data,
)

stanvars_parameters <- brms::stanvar(
block = "parameters", scode = epidist_stan_chunk("ltcad/parameters.stan")
block = "parameters", scode = epidist_stan_chunk("latent_individual/parameters.stan")
)

stanvars_tparameters <- brms::stanvar(
block = "tparameters", scode = epidist_stan_chunk("ltcad/tparameters.stan")
block = "tparameters", scode = epidist_stan_chunk("latent_individual/tparameters.stan")
)

stanvars_priors <- brms::stanvar(
block = "model", scode = epidist_stan_chunk("ltcad/priors.stan")
block = "model", scode = epidist_stan_chunk("latent_individual/priors.stan")
)

stanvars_all <- stanvars_version + stanvars_functions + stanvars_data +
Expand Down
6 changes: 3 additions & 3 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ reference:
desc: Default methods for S3 generics
contents:
- has_concept("defaults")
- title: Latent truncation censoring adjusted delay model
desc: Model-specific methods
- title: Latent individual model
desc: Specific methods for the latent individual model
contents:
- has_concept("ltcad")
- has_concept("latent_individual")
- title: Postprocess
desc: Functions for postprocessing model output
contents:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions man/epidist_formula.epidist_latent_individual.Rd

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

22 changes: 0 additions & 22 deletions man/epidist_formula.epidist_ltcad.Rd

This file was deleted.

2 changes: 1 addition & 1 deletion man/epidist_prepare.Rd

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

10 changes: 4 additions & 6 deletions vignettes/epidist.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,16 @@ dplyr::bind_rows(
```

The main function you will use for modelling is called `epidist`^[Technically, `epidist` is an [S3 generic](http://adv-r.had.co.nz/S3.html) which allows it to work differently for inputs of different classes. This is why inputs must be prepared first via `epidist_prepare` so that they are of the appropriate class!].
We will fit the model `"ltcad"`, which stands for latent truncation censoring adjusted delay (LTCAD)^[Catchy!].
We will fit the model `"latent_individual"` which uses latent variables for the time of primary and secondary event of each individual^[In a future vignette, we will explain in more detail the structure of the model!].
To do so, we first prepare the `data` using `epidist_prepare`:

<!-- In a future vignette, we will explain in more detail the structure of the model. -->

```{r}
data <- epidist_prepare(obs_cens_trunc_samp, model = "ltcad")
data <- epidist_prepare(obs_cens_trunc_samp, model = "latent_individual")
class(data)
```

The `data` object now has the class `epidist_ltcad`.
Using this `data`, we now call `epidist::epidist` to fit the LTCAD model.
The `data` object now has the class `epidist_latent_individual`.
Using this `data`, we now call `epidist::epidist` to fit the model.
The parameters of the model are inferred using Bayesian inference.
In particular, we use the the No-U-Turn Sampler (NUTS) Markov chain Monte Carlo (MCMC) algorithm via the [`brms`](https://paul-buerkner.github.io/brms/) R package [@brms].

Expand Down

0 comments on commit f308cd0

Please sign in to comment.