Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues 82 and 83: Use term generics and add generic defaults with stop message #91

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Generated by roxygen2: do not edit by hand

S3method(epidist,default)
S3method(epidist_family,default)
S3method(epidist_family,epidist_ltcad)
S3method(epidist_formula,default)
S3method(epidist_formula,epidist_ltcad)
S3method(epidist_prepare,default)
S3method(epidist_prepare,epidist_ltcad)
S3method(epidist_priors,default)
S3method(epidist_priors,epidist_ltcad)
S3method(epidist_stancode,default)
S3method(epidist_stancode,epidist_ltcad)
export(add_natural_scale_mean_sd)
export(calculate_censor_delay)
Expand Down
90 changes: 90 additions & 0 deletions R/defaults.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' Default method used when preparing data
#'
#' @param model Character string, model type to prepare to use.
#' Supported options are "ltcad".
#' @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"))
class(data) <- c(class(data), paste0("epidist_", model))
epidist_prepare(data, ...)
}

#' Default method for defining a model specific formula
#'

Check warning on line 18 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=18,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' @inheritParams epidist_formula
#' @param ... Additional arguments for method.
#' @family defaults
#' @export
epidist_formula.default <- function(data, ...) {
stop(
"No epidist_formula method implemented for the class ", class(x), "\n",

Check warning on line 25 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=25,col=67,[object_usage_linter] no visible binding for global variable 'x'
"See methods(epidist_formula) for available methods"
)
}

#' Default method for defining a model specific family
#'

Check warning on line 31 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=31,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' @inheritParams epidist_family
#' @param ... Additional arguments for method.
#' @family defaults
#' @export
epidist_family.default <- function(data, ...) {
stop(
"No epidist_family method implemented for the class ", class(x), "\n",

Check warning on line 38 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=38,col=66,[object_usage_linter] no visible binding for global variable 'x'
"See methods(epidist_family) for available methods"
)
}

#' Default method for defining model specific priors
#'

Check warning on line 44 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=44,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' @inheritParams epidist_priors
#' @param ... Additional arguments for method.
#' @family defaults
#' @export
epidist_priors.default <- function(data, ...) {
stop(
"No epidist_priors method implemented for the class ", class(x), "\n",

Check warning on line 51 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=51,col=66,[object_usage_linter] no visible binding for global variable 'x'
"See methods(epidist_priors) for available methods"
)
}

#' Default method for defining model specific Stan code
#'

Check warning on line 57 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=57,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' @inheritParams epidist_stancode
#' @param ... Additional arguments for method.
#' @family defaults
#' @export
epidist_stancode.default <- function(data, ...) {
stop(
"No epidist_stancode method implemented for the class ", class(x), "\n",

Check warning on line 64 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=64,col=68,[object_usage_linter] no visible binding for global variable 'x'
"See methods(epidist_stancode) for available methods"
)
}

#' Default method used for interface using `brms`
#'
#' @inheritParams epidist
#' @rdname epidist.default
#' @method epidist default
#' @family defaults
#' @export
epidist.default <- function(data, formula = epidist_formula(data),
family = epidist_family(data),
priors = epidist_priors(data),
stancode = epidist_stancode(data), fn = brms::brm,
...) {

Check warning on line 81 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=81,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
fit <- fn(
formula = formula, family = family, stanvars = stancode,
backend = "cmdstanr", data = data, ...
)

Check warning on line 86 in R/defaults.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/defaults.R,line=86,col=1,[trailing_whitespace_linter] Trailing whitespace is superfluous.
class(fit) <- c(class(fit), "epidist_fit")

return(fit)
}
51 changes: 6 additions & 45 deletions R/methods.R → R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,17 @@
#'
#' @param data A dataframe to be used for modelling.
#' @rdname epidist_prepare
#' @family methods
#' @family generics
#' @export
epidist_prepare <- function(data, ...) {
UseMethod("epidist_prepare")
}

#' Default method used when preparing data
#'
#' @param model Character string, model type to prepare to use.
#' Supported options are "ltcad".
#' @param ... Additional arguments passed to model specific `epidist_prepare`
#' functions
#' @rdname epidist_prepare
#' @method epidist_prepare default
#' @family methods
#' @export
epidist_prepare.default <- function(data, model, ...) {
model <- match.arg(model, choices = c("ltcad"))
class(data) <- c(class(data), paste0("epidist_", model))
epidist_prepare(data, ...)
}

#' Define a model specific formula
#'
#' @inheritParams epidist_prepare
#' @param ... Additional arguments for method.
#' @family methods
#' @family generics
#' @export
epidist_formula <- function(data, ...) {
UseMethod("epidist_formula")
Expand All @@ -38,7 +22,7 @@ epidist_formula <- function(data, ...) {
#'
#' @inheritParams epidist_prepare
#' @param ... Additional arguments for method.
#' @family methods
#' @family generics
#' @export
epidist_family <- function(data, ...) {
UseMethod("epidist_family")
Expand All @@ -49,7 +33,7 @@ epidist_family <- function(data, ...) {
#' @inheritParams epidist_prepare
#' @param ... Additional arguments for method.
#' @rdname epidist_priors
#' @family methods
#' @family generics
#' @export
epidist_priors <- function(data, ...) {
UseMethod("epidist_priors")
Expand All @@ -60,7 +44,7 @@ epidist_priors <- function(data, ...) {
#' @inheritParams epidist_prepare
#' @param ... Additional arguments for method.
#' @rdname epidist_stancode
#' @family methods
#' @family generics
#' @export
epidist_stancode <- function(data, ...) {
UseMethod("epidist_stancode")
Expand All @@ -76,31 +60,8 @@ epidist_stancode <- function(data, ...) {
#' `brms::make_standata`.
#' @inheritParams epidist_prepare
#' @param ... Additional arguments for method.
#' @family methods
#' @family generics
#' @export
epidist <- function(data, formula, family, priors, custom_stancode, fn, ...) {
UseMethod("epidist")
}

#' Default method used for interface using `brms`
#'
#' @inheritParams epidist
#' @rdname epidist
#' @method epidist default
#' @family methods
#' @export
epidist.default <- function(data, formula = epidist_formula(data),
family = epidist_family(data),
priors = epidist_priors(data),
stancode = epidist_stancode(data), fn = brms::brm,
...) {

fit <- fn(
formula = formula, family = family, stanvars = stancode,
backend = "cmdstanr", data = data, ...
)

class(fit) <- c(class(fit), "epidist_fit")

return(fit)
}
12 changes: 8 additions & 4 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ reference:
desc: Functions for observing data
contents:
- has_concept("observe")
- title: Methods
desc: Functions for estimating epidemiological delays
- title: S3 generics
desc: S3 generics for delay modelling
contents:
- has_concept("methods")
- has_concept("generics")
- title: Method default
desc: Default methods for S3 generics
contents:
- has_concept("defaults")
- title: Latent truncation censoring adjusted delay model
desc: Model-specific functions
desc: Model-specific methods
contents:
- has_concept("ltcad")
- title: Postprocess
Expand Down
9 changes: 9 additions & 0 deletions inst/s3-testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ fit_gamma <- epidist(
family = epidist_family(prep_obs, family = "gamma")
)
)

# Test the defaults

x <- list()
epidist_prepare(x)
epidist_family(x)
epidist_formula(x)
epidist_priors(x)
epidist_stancode(x)
28 changes: 4 additions & 24 deletions man/epidist.Rd

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

42 changes: 42 additions & 0 deletions man/epidist.default.Rd

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

8 changes: 4 additions & 4 deletions man/epidist_family.Rd

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

25 changes: 25 additions & 0 deletions man/epidist_family.default.Rd

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

8 changes: 4 additions & 4 deletions man/epidist_formula.Rd

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

Loading
Loading