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

Update <epiparameter> constructor function and internal refactor #381

Merged
merged 50 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
aae3791
renamed create_prob_dist to create_prob_distribution
joshwlambert Sep 19, 2024
db40db4
rename create_prob_dist R and test file to create_prob_distribution
joshwlambert Sep 19, 2024
7d0fcb0
return character from create_prob_distribution if parameters not given
joshwlambert Sep 19, 2024
08a39de
rename create_prob_distribution args to prob_distribution and prob_di…
joshwlambert Sep 19, 2024
e6ac77c
clean prob_distribution in create_prob_distribution to prevent string…
joshwlambert Sep 19, 2024
5909fa9
rename is_epiparameter_params args prob_distribution and prob_distrib…
joshwlambert Sep 19, 2024
d4f856f
rename .clean_params args to prob_distribution and prob_distribution_…
joshwlambert Sep 19, 2024
8b5f31c
add parameter standardisation to create_prob_distribution
joshwlambert Sep 19, 2024
3eef80f
update create_prob_distribution test error messages
joshwlambert Sep 19, 2024
0fa7bfa
rename .calc_dist_params args to prob_distribution and prob_distribut…
joshwlambert Sep 20, 2024
1e21f02
remove prob_distribution_params argument from .calc_dist_params and c…
joshwlambert Sep 23, 2024
c57d284
remove truncation argument from new_epiparameter
joshwlambert Sep 23, 2024
b0d2313
remove discretise argument from new_epiparameter
joshwlambert Sep 23, 2024
c550bb7
remove copying mean from summary_stats to prob_dist_params in new_epi…
joshwlambert Sep 23, 2024
260d67b
remove .clean_string call in new_epiparameter as its now called in cr…
joshwlambert Sep 23, 2024
e0297f6
remove stopifnot that checks uncertainty and parameters match as this…
joshwlambert Sep 23, 2024
4944f5e
replace prob_dist and prob_dist_params arguments in new_epiparameter …
joshwlambert Sep 23, 2024
7fd1988
simplified new_epiparameter with prob_distribution from create_prob_d…
joshwlambert Sep 23, 2024
c7611d8
remove prob_distribution_params arg from epiparameter() and use creat…
joshwlambert Sep 23, 2024
0b96ae8
replace assert_character with stopifnot to check for char or distribu…
joshwlambert Sep 23, 2024
cd6bb0e
move uncertainty checking after new_epiparameter call in epiparameter…
joshwlambert Sep 23, 2024
beafd36
remove prob_dist_params and rename prob_distribution argument from ne…
joshwlambert Sep 23, 2024
fa854b3
update assert_epiparameter and test_epiparameter to use prob_distribu…
joshwlambert Sep 23, 2024
9e3b911
move prob_distribution and prob_distribution_params args documentatio…
joshwlambert Sep 23, 2024
765a5f9
inherit arg documentation from create_prob_distribution in is_epipara…
joshwlambert Sep 23, 2024
a78fcca
remove discretise argument from epiparameter()
joshwlambert Sep 23, 2024
7346fbc
remove truncation argument from epiparameter()
joshwlambert Sep 23, 2024
169ea95
move auto_calc_params to last named argument in epiparameter() and ne…
joshwlambert Sep 23, 2024
34e0cd4
add documentation for prob_distribution arg in epiparameter()
joshwlambert Sep 23, 2024
fe2b7f4
update $prob_dist to $prob_distribution in functions that use <epipar…
joshwlambert Sep 24, 2024
fce47d8
update $prob_dist to $prob_distribution in tests that use <epiparameter>
joshwlambert Sep 24, 2024
df61837
reorganise logic in create_prob_distribution to handle NA parameters
joshwlambert Sep 24, 2024
e2d0190
add %||%
joshwlambert Sep 24, 2024
65ab591
improve handling of uncertainty in epiparameter()
joshwlambert Sep 24, 2024
f8abaca
temp fix for handling precision parameter in .format_params
joshwlambert Sep 24, 2024
52f92b5
update sysdata
joshwlambert Sep 24, 2024
2fef178
update .epireview_to_epiparameter to use user specified distribution …
joshwlambert Sep 25, 2024
2a159d9
add .clean_uncertainty to handle multitude of cases where uncertainty…
joshwlambert Sep 25, 2024
f2e146e
fix $prob_distribution in <epiparameter> methods
joshwlambert Sep 25, 2024
9b12516
simplify epiparameter() by calling .clean_uncertainty
joshwlambert Sep 25, 2024
2d634d2
update sysdata
joshwlambert Sep 25, 2024
23e83c7
update vignettes with new epiparameter constructor
joshwlambert Sep 25, 2024
245614e
update .calc_dist_params tests for removed argument
joshwlambert Sep 25, 2024
ffb870b
update tests and snapshots using new epiparameter constructor signature
joshwlambert Sep 25, 2024
c3172f4
update as_epiparameter test to expect error
joshwlambert Sep 25, 2024
48260cb
update function documentation for new function signatures
joshwlambert Sep 25, 2024
b183fff
update examples to use new epiparameter function signature
joshwlambert Sep 25, 2024
3c72803
remove outdated argument documentation from new_epiparameter
joshwlambert Sep 25, 2024
9b197d9
correctly specify uncertainty in epiparameter() example
joshwlambert Sep 25, 2024
d3453e0
fix epiparameter example
joshwlambert Sep 25, 2024
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
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export(convert_summary_stats_to_params)
export(create_citation)
export(create_metadata)
export(create_method_assess)
export(create_prob_dist)
export(create_prob_distribution)
export(create_region)
export(create_summary_stats)
export(create_uncertainty)
Expand Down
12 changes: 6 additions & 6 deletions R/accessors.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ get_parameters <- function(x, ...) {
get_parameters.epiparameter <- function(x, ...) {
chkDots(...)
# extract parameters depending on prob distribution class
if (inherits(x$prob_dist, "distcrete")) {
params <- unlist(x$prob_dist$parameters)
} else if (inherits(x$prob_dist, "distribution")) {
params <- unlist(distributional::parameters(x$prob_dist))
if (inherits(x$prob_distribution, "distcrete")) {
params <- unlist(x$prob_distribution$parameters)
} else if (inherits(x$prob_distribution, "distribution")) {
params <- unlist(distributional::parameters(x$prob_distribution))

# if dist is truncated clean names
if (is_truncated(x)) {
Expand All @@ -53,8 +53,8 @@ get_parameters.epiparameter <- function(x, ...) {

# convert to meanlog and sdlog names
params <- .clean_params(
prob_dist = family(x),
prob_dist_params = params
prob_distribution = family(x),
prob_distribution_params = params
)
} else {
return(NA)
Expand Down
54 changes: 32 additions & 22 deletions R/calc_dist_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
#'
#' @return A named `numeric` vector with parameters.
#' @keywords internal
.calc_dist_params <- function(prob_dist, # nolint cyclocomp
prob_dist_params,
.calc_dist_params <- function(prob_distribution, # nolint cyclocomp
summary_stats,
sample_size) {
if (is.na(prob_dist)) {
if (is.na(prob_distribution)) {
message(
"No adequate summary statistics available to calculate the parameters ",
"of the ", prob_dist, " distribution"
"of the ", prob_distribution, " distribution"
)
return(NA)
}
Expand All @@ -60,19 +59,21 @@
unlist(summary_stats$range)
)

# extract dispersion
disp <- unname(prob_dist_params[names(prob_dist_params) == "dispersion"])
median_disp <- c(median = summary_stats$median, dispersion = disp)

# extract mean and sd
mean_sd <- c(summary_stats$mean, summary_stats$sd)

is_mean_sd <- checkmate::test_numeric(
mean_sd, any.missing = FALSE, len = 2, finite = TRUE
)
is_median_disp <- checkmate::test_numeric(
median_disp, len = 2, finite = TRUE

is_median_disp <- checkmate::test_number(
summary_stats$median,
finite = TRUE
) && checkmate::test_number(
summary_stats$dispersion,
finite = TRUE
)

is_median_range <- checkmate::test_numeric(
median_range, len = 3, finite = TRUE
) && checkmate::test_count(sample_size, positive = TRUE)
Expand All @@ -93,40 +94,49 @@
)
summary_stats_ <- summary_stats_[idx]
# create flat list structure to be passed to ... in conversion
args <- unlist(list(prob_dist, as.list(summary_stats_)), recursive = FALSE)
prob_dist_params <- unlist(do.call(
args <- unlist(
list(prob_distribution, as.list(summary_stats_)),
recursive = FALSE
)
prob_distribution_params <- unlist(do.call(
convert_summary_stats_to_params,
args = args
))
} else if (is_median_disp) {
med <- summary_stats$median
meanlog <- log(med / sqrt(1 + disp^2))
sdlog <- sqrt(log(1 + disp^2))
prob_dist_params <- c(meanlog = meanlog, sdlog = sdlog)
args <- list(
x = prob_distribution,
median = summary_stats$median,
dispersion = summary_stats$dispersion
)
prob_distribution_params <- do.call(
convert_summary_stats_to_params,
args = args
)
prob_distribution_params <- unlist(prob_distribution_params)
} else if (!anyNA(percentiles)) {
# calculate the parameters from the percentiles
# percentiles required to be [0, 1] so divide by 100
prob_dist_params <- extract_param(
prob_distribution_params <- extract_param(
type = "percentile",
values = percentiles,
distribution = prob_dist,
distribution = prob_distribution,
percentiles = as.numeric(names(percentiles)) / 100
)
} else if (is_median_range) {
prob_dist_params <- extract_param(
prob_distribution_params <- extract_param(
type = "range",
values = median_range,
distribution = prob_dist,
distribution = prob_distribution,
samples = sample_size
)
} else {
message(
"No adequate summary statistics available to calculate the parameters ",
"of the ", prob_dist, " distribution"
"of the ", prob_distribution, " distribution"
)
return(NA)
}

# return params
prob_dist_params
prob_distribution_params
}
8 changes: 5 additions & 3 deletions R/checkers.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#' ep <- epiparameter(
#' disease = "ebola",
#' epi_dist = "incubation",
#' prob_distribution = "gamma",
#' prob_distribution_params = c(shape = 1, scale = 1)
#' prob_distribution = create_prob_distribution(
#' prob_distribution = "gamma",
#' prob_distribution_params = c(shape = 1, scale = 1)
#' )
#' )
#' is_parameterised(ep)
#'
Expand All @@ -43,7 +45,7 @@ is_parameterized <- is_parameterised
is_parameterised.epiparameter <- function(x, ...) {
chkDots(...)
# probability distribution object
return(is.object(x$prob_dist))
return(is.object(x$prob_distribution))
}

#' @export
Expand Down
28 changes: 20 additions & 8 deletions R/coercion.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ as.data.frame.epiparameter <- function(x, ...) {
disease = x$disease,
pathogen = x$pathogen,
epi_distribution = x$epi_dist,
prob_distribution = I(list(prob_dist = x$prob_dist)),
prob_distribution = I(list(prob_dist = x$prob_distribution)),
uncertainty = I(list(uncertainty = x$uncertainty)),
summary_stats = I(list(summary_stats = x$summary_stats)),
citation = I(x$citation),
Expand Down Expand Up @@ -220,15 +220,17 @@ is_epiparameter_df <- function(x) {
disease = x$disease,
pathogen = x$pathogen,
epi_dist = x$epi_distribution,
prob_distribution = family(x$prob_distribution[[1]]),
prob_distribution_params = prob_distribution_params,
prob_distribution = create_prob_distribution(
prob_distribution = family(x$prob_distribution[[1]]),
prob_distribution_params = prob_distribution_params,
discretise = inherits(x$prob_distribution[[1]], "distcrete"),
truncation = truncation
),
uncertainty = uncertainty,
summary_stats = x$summary_stats$summary_stats,
citation = x$citation,
metadata = x$metadata,
method_assess = x$method_assess,
discretise = inherits(x$prob_distribution[[1]], "distcrete"),
truncation = truncation,
notes = x$notes
)
}
Expand Down Expand Up @@ -268,7 +270,7 @@ is_epiparameter_df <- function(x) {
sd_ <- NULL
if (rlang::is_na(prob_dist)) {
prob_dist_params <- NA_real_
uncertainty <- create_uncertainty()
uncertainty <- list(uncertainty = create_uncertainty())
} else {
prob_dist <- switch(
prob_dist,
Expand Down Expand Up @@ -309,6 +311,14 @@ is_epiparameter_df <- function(x) {
)
prob_dist_params_names <- .clean_string(prob_dist_params_names)
names(prob_dist_params) <- prob_dist_params_names

# overwrite prob_dist with user specified if given to make use of conversion
if (!is.null(prob_dist_in)) {
prob_dist <- prob_dist_in
# remove user specified to not trigger overwriting prob_dist below
prob_dist_in <- NULL
}

if (all(c("mean", "sd") %in% names(prob_dist_params))) {
sd_ <- prob_dist_params[["sd"]]
prob_dist_params <- do.call(
Expand Down Expand Up @@ -422,8 +432,10 @@ is_epiparameter_df <- function(x) {
disease = disease,
pathogen = pathogen,
epi_dist = epi_dist,
prob_distribution = prob_dist,
prob_distribution_params = prob_dist_params,
prob_distribution = create_prob_distribution(
prob_distribution = prob_dist,
prob_distribution_params = prob_dist_params
),
uncertainty = uncertainty,
summary_stats = summary_stats,
citation = citation,
Expand Down
2 changes: 1 addition & 1 deletion R/convert_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ convert_params_to_summary_stats.epiparameter <- function(x, ...) {
if (!is_parameterised(x)) {
if (length(dots) > 0) {
# unparameterised with parameters supplied through `...`
return(convert_params_to_summary_stats(x$prob_dist, ...))
return(convert_params_to_summary_stats(x$prob_distribution, ...))
}
# unparameterised with no parameters supplied through `...`
stop(
Expand Down
Loading
Loading