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 proportion_*() functions #81

Merged
merged 4 commits into from
Jan 22, 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
17 changes: 8 additions & 9 deletions R/proportion_cluster_size.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#' Proportion of new cases that originated with a transmission event of a
#' given size (useful to inform backwards contact tracing efforts, i.e. how
#' many cases are associated with large clusters)
#' Estimate what proportion of new cases originated within a transmission
#' event of a given size
#'
#' @description Calculates the proportion of new cases that originated with a
#' transmission event of a given size (useful to inform backwards contact
#' tracing efforts, i.e. how many cases are associated with large clusters).
#' Here we define a cluster to as a transmission of a primary case
#' transmission event of a given size. It can be useful to inform backwards
#' contact tracing efforts, i.e. how many cases are associated with large
#' clusters. Here we define a cluster to as a transmission of a primary case
#' to at least one secondary case.
#'
#' @details This function calculates the proportion of secondary cases that
#' are caused transmission events of a certain size. It does not calculate
#' are caused by transmission events of a certain size. It does not calculate
#' the proportion of transmission events that cause a cluster of secondary
#' cases of a certain size. In other words it is the number of cases above a
#' threshold divided by the total number of cases, not the number of
Expand Down Expand Up @@ -71,8 +70,8 @@ proportion_cluster_size <- function(R, k, cluster_size, ..., offspring_dist,
size = df[i, "k"]
)
propn_cluster <- vapply(cluster_size, function(x) {
sum(simulate_secondary[simulate_secondary >= x]) / sum(simulate_secondary)
}, FUN.VALUE = numeric(1))
sum(simulate_secondary[simulate_secondary >= x])
}, FUN.VALUE = numeric(1)) / sum(simulate_secondary)
if (format_prop) {
propn_cluster <- paste0(round(propn_cluster * 100, digits = 1), "%")
}
Expand Down
10 changes: 5 additions & 5 deletions R/proportion_transmission.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Estimate what proportion of cases that cause a certain proportion of
#' Estimate what proportion of cases cause a certain proportion of
#' transmission
#'
#' @description Calculates the proportion of cases that cause a certain
Expand All @@ -19,7 +19,7 @@
#' @inheritParams probability_epidemic
#' @param percent_transmission A `number` of the percentage transmission
#' for which a proportion of cases has produced.
#' @param sim A `logical` whether the calculation should be done numerically
#' @param simulate A `logical` whether the calculation should be done numerically
#' (i.e. simulate secondary contacts) or analytically.
#' @inheritParams proportion_cluster_size
#'
Expand Down Expand Up @@ -64,7 +64,7 @@
#' )
proportion_transmission <- function(R, k,
percent_transmission,
sim = FALSE,
simulate = FALSE,
...,
offspring_dist,
format_prop = TRUE) {
Expand All @@ -83,14 +83,14 @@ proportion_transmission <- function(R, k,
checkmate::assert_numeric(R, lower = 0, finite = TRUE)
checkmate::assert_numeric(k, lower = 0)
checkmate::assert_number(percent_transmission, lower = 0, upper = 1)
checkmate::assert_logical(sim, any.missing = FALSE, len = 1)
checkmate::assert_logical(simulate, any.missing = FALSE, len = 1)
checkmate::assert_logical(format_prop, any.missing = FALSE, len = 1)

df <- expand.grid(R = R, k = k, NA_real_)
colnames(df) <- c("R", "k", paste0("prop_", percent_transmission * 100))

for (i in seq_len(nrow(df))) {
if (sim) {
if (simulate) {
prop <- .prop_transmission_numerical(
R = df[i, "R"],
k = df[i, "k"],
Expand Down
13 changes: 6 additions & 7 deletions man/proportion_cluster_size.Rd

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

6 changes: 3 additions & 3 deletions man/proportion_transmission.Rd

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

10 changes: 5 additions & 5 deletions tests/testthat/test-proportion_transmission.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("proportion_transmission works as expected for single R and k", {
R = 2,
k = 0.5,
percent_transmission = 0.8,
sim = TRUE
simulate = TRUE
)

expect_s3_class(res, "data.frame")
Expand Down Expand Up @@ -41,7 +41,7 @@ test_that("proportion_transmission works as expected for multiple R", {
R = c(1, 2, 3),
k = 0.5,
percent_transmission = 0.8,
sim = TRUE
simulate = TRUE
)

expect_s3_class(res, "data.frame")
Expand Down Expand Up @@ -70,7 +70,7 @@ test_that("proportion_transmission works as expected for multiple R & k", {
R = c(1, 2, 3),
k = c(0.1, 0.2, 0.3),
percent_transmission = 0.8,
sim = TRUE
simulate = TRUE
)

expect_s3_class(res, "data.frame")
Expand Down Expand Up @@ -118,9 +118,9 @@ test_that("proportion_transmission fails as expected", {
R = 1,
k = 0.1,
percent_transmission = 0.8,
sim = 1
simulate = 1
),
regexp = "Assertion on 'sim' failed"
regexp = "Assertion on 'simulate' failed"
)
})

Expand Down