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} usage #100

Merged
merged 1 commit into from
Aug 23, 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
11 changes: 7 additions & 4 deletions R/probability_contain.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ probability_contain <- function(R,

input_params <- missing(R) && missing(k)
if (!xor(input_params, missing(offspring_dist))) {
stop("Only one of R and k or <epidist> must be supplied.", call. = FALSE)
stop(
"Only one of R and k or <epiparameter> must be supplied.",
call. = FALSE
)
}
# check inputs
if (input_params) {
checkmate::assert_class(offspring_dist, classes = "epidist")
R <- get_epidist_param(epidist = offspring_dist, parameter = "R")
k <- get_epidist_param(epidist = offspring_dist, parameter = "k")
checkmate::assert_class(offspring_dist, classes = "epiparameter")
R <- get_epiparameter_param(epiparameter = offspring_dist, parameter = "R")
k <- get_epiparameter_param(epiparameter = offspring_dist, parameter = "k")
}
checkmate::assert_number(R, lower = 0, finite = TRUE)
checkmate::assert_number(k, lower = 0)
Expand Down
16 changes: 10 additions & 6 deletions R/probability_epidemic.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
#' default optimisation settings. Currently only `"fit_method"` is accepted
#' and can be either `"optim"` (default) or `"grid"` for numerical
#' optimisation routine or grid search, respectively.
#' @param offspring_dist An `<epidist>` object. An S3 class for working with
#' epidemiological parameters/distributions, see [epiparameter::epidist()].
#' @param offspring_dist An `<epiparameter>` object. An S3 class for working
#' with epidemiological parameters/distributions, see
#' [epiparameter::epiparameter()].
#'
#' @return A value with the probability of a large epidemic.
#' @export
Expand Down Expand Up @@ -89,14 +90,17 @@ probability_extinct <- function(R,
offspring_dist) {
input_params <- missing(R) && missing(k)
if (!xor(input_params, missing(offspring_dist))) {
stop("Only one of R and k or <epidist> must be supplied.", call. = FALSE)
stop(
"Only one of R and k or <epiparameter> must be supplied.",
call. = FALSE
)
}

# check inputs
if (input_params) {
checkmate::assert_class(offspring_dist, classes = "epidist")
R <- get_epidist_param(epidist = offspring_dist, parameter = "R")
k <- get_epidist_param(epidist = offspring_dist, parameter = "k")
checkmate::assert_class(offspring_dist, classes = "epiparameter")
R <- get_epiparameter_param(epiparameter = offspring_dist, parameter = "R")
k <- get_epiparameter_param(epiparameter = offspring_dist, parameter = "k")
}

checkmate::assert_number(R, lower = 0, finite = TRUE)
Expand Down
11 changes: 7 additions & 4 deletions R/proportion_cluster_size.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ proportion_cluster_size <- function(R, k, cluster_size, ..., offspring_dist,
format_prop = TRUE) {
input_params <- missing(R) && missing(k)
if (!xor(input_params, missing(offspring_dist))) {
stop("Only one of R and k or <epidist> must be supplied.", call. = FALSE)
stop(
"Only one of R and k or <epiparameter> must be supplied.",
call. = FALSE
)
}

# check inputs
chkDots(...)
if (input_params) {
checkmate::assert_class(offspring_dist, classes = "epidist")
R <- get_epidist_param(epidist = offspring_dist, parameter = "R")
k <- get_epidist_param(epidist = offspring_dist, parameter = "k")
checkmate::assert_class(offspring_dist, classes = "epiparameter")
R <- get_epiparameter_param(epiparameter = offspring_dist, parameter = "R")
k <- get_epiparameter_param(epiparameter = offspring_dist, parameter = "k")
}
checkmate::assert_numeric(R, lower = 0, finite = TRUE)
checkmate::assert_numeric(k, lower = 0)
Expand Down
11 changes: 7 additions & 4 deletions R/proportion_transmission.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,18 @@ proportion_transmission <- function(R, k,
format_prop = TRUE) {
input_params <- missing(R) && missing(k)
if (!xor(input_params, missing(offspring_dist))) {
stop("Only one of R and k or <epidist> must be supplied.", call. = FALSE)
stop(
"Only one of R and k or <epiparameter> must be supplied.",
call. = FALSE
)
}

# check inputs
chkDots(...)
if (input_params) {
checkmate::assert_class(offspring_dist, classes = "epidist")
R <- get_epidist_param(epidist = offspring_dist, parameter = "R")
k <- get_epidist_param(epidist = offspring_dist, parameter = "k")
checkmate::assert_class(offspring_dist, classes = "epiparameter")
R <- get_epiparameter_param(epiparameter = offspring_dist, parameter = "R")
k <- get_epiparameter_param(epiparameter = offspring_dist, parameter = "k")
}
checkmate::assert_numeric(R, lower = 0, finite = TRUE)
checkmate::assert_numeric(k, lower = 0)
Expand Down
17 changes: 10 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#' Get a parameter out of <epidist>
#' Get a parameter out of <epiparameter>
#'
#' @inheritParams probability_epidemic
#' @param parameter A character string, either `"R"` or `"k"`.
#'
#' @return An unnamed numeric.
#' @keywords internal
#' @noRd
get_epidist_param <- function(epidist,
parameter = c("R", "k")) {
# check inputs (<epidist> already checked)
get_epiparameter_param <- function(epiparameter,
parameter = c("R", "k")) {
# check inputs (<epiparameter> already checked)
parameter <- match.arg(parameter)

# extract parameters from <epidist>
params <- epiparameter::get_parameters(epidist)
# extract parameters from <epiparameter>
params <- epiparameter::get_parameters(epiparameter)

regexpr_pattern <- switch(parameter,
R = "^r$|^r0$|^mean$",
Expand All @@ -28,7 +28,10 @@ get_epidist_param <- function(epidist,
if (length(idx) == 0) {
stop(
sprintf(
"Cannot find %s in <epidist>, check if parameters have correct names.",
paste(
"Cannot find %s in <epiparameter>,",
"check if parameters have correct names."
),
parameter
),
call. = FALSE
Expand Down
5 changes: 3 additions & 2 deletions man/probability_contain.Rd

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

5 changes: 3 additions & 2 deletions man/probability_epidemic.Rd

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

5 changes: 3 additions & 2 deletions man/probability_extinct.Rd

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

5 changes: 3 additions & 2 deletions man/proportion_cluster_size.Rd

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

5 changes: 3 additions & 2 deletions man/proportion_transmission.Rd

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

8 changes: 4 additions & 4 deletions tests/testthat/_snaps/probability_contain.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,25 @@
"value": [0.7636]
}

# probability_contain works with <epidist>
# probability_contain works with <epiparameter>

Code
probability_contain(num_init_infect = 1, pop_control = 0.1, offspring_dist = edist)
probability_contain(num_init_infect = 1, pop_control = 0.1, offspring_dist = od)
Output
[1] 0.9037105

---

Code
probability_contain(num_init_infect = 1, ind_control = 0.1, offspring_dist = edist)
probability_contain(num_init_infect = 1, ind_control = 0.1, offspring_dist = od)
Output
[1] 0.9133394

---

Code
probability_contain(num_init_infect = 5, ind_control = 0.1, pop_control = 0.1,
offspring_dist = edist)
offspring_dist = od)
Output
[1] 0.7168911

4 changes: 2 additions & 2 deletions tests/testthat/_snaps/probability_epidemic.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
Output
[1] 0.1084924

# probability_epidemic works with <epidist>
# probability_epidemic works with <epiparameter>

Code
probability_epidemic(num_init_infect = 1, offspring_dist = edist)
probability_epidemic(num_init_infect = 1, offspring_dist = od)
Output
[1] 0.1198705

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/proportion_transmission.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
Output
[1] 0.264419

# proportion_transmission works with <epidist>
# proportion_transmission works with <epiparameter>

Code
proportion_transmission(percent_transmission = 0.8, offspring_dist = edist)
proportion_transmission(percent_transmission = 0.8, offspring_dist = od)
Output
R k prop_80
1 1.63 0.16 13%
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-probability_contain.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,29 @@ test_that("probability_contain works as when using dots", {
)
})

test_that("probability_contain works with <epidist>", {
test_that("probability_contain works with <epiparameter>", {
skip_if_not_installed(pkg = "epiparameter")
edist <- suppressMessages(
epiparameter::epidist_db(
od <- suppressMessages(
epiparameter::epiparameter_db(
disease = "SARS",
epi_dist = "offspring distribution",
author = "Lloyd-Smith",
single_epidist = TRUE
single_epiparameter = TRUE
)
)
expect_snapshot(
probability_contain(
num_init_infect = 1,
pop_control = 0.1,
offspring_dist = edist
offspring_dist = od
)
)

expect_snapshot(
probability_contain(
num_init_infect = 1,
ind_control = 0.1,
offspring_dist = edist
offspring_dist = od
)
)

Expand All @@ -122,7 +122,7 @@ test_that("probability_contain works with <epidist>", {
num_init_infect = 5,
ind_control = 0.1,
pop_control = 0.1,
offspring_dist = edist
offspring_dist = od
)
)
})
Expand All @@ -148,9 +148,9 @@ test_that("probability_contain fails using dots with incorrect name", {
)
})

test_that("probability_contain fails without R and k or <epidist>", {
test_that("probability_contain fails without R and k or <epiparameter>", {
expect_error(
probability_contain(num_init_infect = 1, pop_control = 0.5),
regexp = "Only one of R and k or <epidist> must be supplied."
regexp = "Only one of R and k or <epiparameter> must be supplied."
)
})
14 changes: 7 additions & 7 deletions tests/testthat/test-probability_epidemic.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,25 @@ test_that("probability_epidemic works for R > 1", {
)
})

test_that("probability_epidemic works with <epidist>", {
test_that("probability_epidemic works with <epiparameter>", {
skip_if_not_installed(pkg = "epiparameter")
edist <- suppressMessages(
epiparameter::epidist_db(
od <- suppressMessages(
epiparameter::epiparameter_db(
disease = "SARS",
epi_dist = "offspring distribution",
author = "Lloyd-Smith",
single_epidist = TRUE
single_epiparameter = TRUE
)
)
expect_snapshot(
probability_epidemic(num_init_infect = 1, offspring_dist = edist)
probability_epidemic(num_init_infect = 1, offspring_dist = od)
)
})

test_that("probability_epidemic fails without R and k or <epidist>", {
test_that("probability_epidemic fails without R and k or <epiparameter>", {
expect_error(
probability_epidemic(num_init_infect = 1),
regexp = "Only one of R and k or <epidist> must be supplied."
regexp = "Only one of R and k or <epiparameter> must be supplied."
)
})

Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-proportion_cluster_size.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ test_that("proportion_cluster_size fails as expected", {
)
})

test_that("proportion_cluster_size works with <epidist>", {
test_that("proportion_cluster_size works with <epiparameter>", {
skip_if_not_installed(pkg = "epiparameter")
edist <- suppressMessages(
epiparameter::epidist_db(
od <- suppressMessages(
epiparameter::epiparameter_db(
disease = "SARS",
epi_dist = "offspring distribution",
author = "Lloyd-Smith",
single_epidist = TRUE
single_epiparameter = TRUE
)
)
res <- proportion_cluster_size(cluster_size = 20, offspring_dist = edist)
res <- proportion_cluster_size(cluster_size = 20, offspring_dist = od)

expect_s3_class(res, "data.frame")
expect_identical(dim(res), c(1L, 3L))
Expand All @@ -104,9 +104,9 @@ test_that("proportion_cluster_size works with <epidist>", {
)
})

test_that("proportion_cluster_size fails without R and k or <epidist>", {
test_that("proportion_cluster_size fails without R and k or <epiparameter>", {
expect_error(
proportion_cluster_size(cluster_size = 10),
regexp = "Only one of R and k or <epidist> must be supplied."
regexp = "Only one of R and k or <epiparameter> must be supplied."
)
})
Loading
Loading