Skip to content

Add support for missing NAs in estimate_infection() model #528

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

Merged
merged 18 commits into from
Jan 9, 2024
Merged
Prev Previous commit
Next Next commit
error check create_clean_reported_cases and add unit tests to cover f…
…unction
  • Loading branch information
seabbs committed Jan 8, 2024
commit 3ee24d32be4a6349986a42c2ecc1f00b40d16759
6 changes: 5 additions & 1 deletion R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#' @author Sam Abbott
#' @author Lloyd Chapman
#' @export
#' @examples
#' create_clean_reported_cases(example_confirmed, 7)
create_clean_reported_cases <- function(reported_cases, horizon,
filter_leading_zeros = TRUE,
zero_threshold = Inf) {
Expand All @@ -38,7 +40,9 @@ create_clean_reported_cases <- function(reported_cases, horizon,
reported_cases <- data.table::setorder(reported_cases, date)
## Filter out 0 reported cases from the beginning of the data
if (filter_leading_zeros) {
reported_cases <- reported_cases[order(date)][min(date[confirm > 0])]
reported_cases <- reported_cases[order(date)][
date >= min(date[confirm[!is.na(confirm)] > 0])
]
}

# Check case counts preceding zero case counts and set to 7 day average if
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-create_clean_reported_cases.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

test_that("create_clean_reported_cases runs without errors", {
expect_no_error(create_clean_reported_cases(example_confirmed, 7))
})

test_that("create_clean_reported_cases returns a data table", {
result <- create_clean_reported_cases(example_confirmed, 7)
expect_s3_class(result, "data.table")
})

test_that("create_clean_reported_cases filters leading zeros correctly", {
# Modify example_confirmed to have leading zeros
modified_data <- example_confirmed
modified_data[1:3, "confirm"] <- 0

result <- create_clean_reported_cases(modified_data, 7)
# Check if the first row with non-zero cases is retained
expect_equal(result$date[1], min(modified_data$date[modified_data$confirm > 0]))
})

test_that("create_clean_reported_cases replaces zero cases correctly", {
# Modify example_confirmed to have zero cases that should be replaced
modified_data <- example_confirmed
modified_data$confirm[10:16] <- 0
threshold <- 10

result <- create_clean_reported_cases(
modified_data, 0, zero_threshold = threshold
)
# Check if zero cases within the threshold are replaced
expect_equal(sum(result$confirm == 0, na.rm = TRUE), 0)
})