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

Use internal checking function to reduce duplication #161

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 1 addition & 28 deletions R/cfr_rolling.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,7 @@ cfr_rolling <- function(data,
" the course of the outbreak."
)
# input checking
checkmate::assert_data_frame(
data,
min.rows = 1, min.cols = 3
)
# check that input `<data.frame>` has columns date, cases, and deaths
checkmate::assert_names(
colnames(data),
must.include = c("date", "cases", "deaths")
)
# check for any NAs among data
checkmate::assert_data_frame(
data[, c("date", "cases", "deaths")],
types = c("Date", "integerish"),
any.missing = FALSE
)
# check that data$date is a date column
checkmate::assert_date(data$date, any.missing = FALSE, all.missing = FALSE)
# check for excessive missing date and throw an error
# also check delay_density
stopifnot(
"Input data must have sequential dates with none missing or duplicated" =
identical(unique(diff(data$date)), 1) # use numeric 1, not integer
# this solution works when df$date is `Date`
# this may need more thought for dates that are integers, POSIXct,
# or other units; consider the units package
)
.check_input_data(data)
checkmate::assert_count(poisson_threshold, positive = TRUE)

# NOTE: delay_density is checked in estimate_outcomes() if passed and not NULL
Expand All @@ -97,8 +72,6 @@ cfr_rolling <- function(data,
cumulative_cases <- cumsum(data$cases)
cumulative_deaths <- cumsum(data$deaths)

# Check cumulative sums for count type
checkmate::assert_integerish(cumulative_cases, lower = 0)
# use assert_number to set upper limit at total_cases
checkmate::assert_integerish(
cumulative_deaths,
Expand Down
30 changes: 1 addition & 29 deletions R/cfr_static.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,7 @@
cfr_static <- function(data,
delay_density = NULL,
poisson_threshold = 100) {
# input checking
checkmate::assert_data_frame(
data,
min.rows = 1, min.cols = 3
)
# check that input `<data.frame>` has columns date, cases, and deaths
checkmate::assert_names(
colnames(data),
must.include = c("date", "cases", "deaths")
)
# check for any NAs among data
checkmate::assert_data_frame(
data[, c("date", "cases", "deaths")],
types = c("Date", "integerish"),
any.missing = FALSE
)
# check that data$date is a date column
checkmate::assert_date(data$date, any.missing = FALSE, all.missing = FALSE)

# check for excessive missing date and throw an error
stopifnot(
"Input data must have sequential dates with none missing or duplicated" =
identical(unique(diff(data$date)), 1) # use numeric 1, not integer
# this solution works when df$date is `Date`
# this may need more thought for dates that are integers, POSIXct,
# or other units; consider the units package
)
.check_input_data(data)
checkmate::assert_count(poisson_threshold, positive = TRUE)

# NOTE: delay_density is checked in estimate_outcomes() if passed and not NULL
Expand All @@ -160,8 +134,6 @@ cfr_static <- function(data,
total_cases <- sum(data$cases, na.rm = TRUE)
total_deaths <- sum(data$deaths, na.rm = TRUE)

# Add input checking for total cases and deaths
checkmate::assert_count(total_cases)
# use assert_number to set upper limit at total_cases
checkmate::assert_number(total_deaths, upper = total_cases, lower = 0)

Expand Down
18 changes: 1 addition & 17 deletions R/cfr_time_varying.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,10 @@ cfr_time_varying <- function(data,
# input checking
# zero count allowed to include all data
checkmate::assert_count(burn_in)

# expect rows more than burn in value
checkmate::assert_data_frame(data, min.cols = 3, min.rows = burn_in + 1)
# check that input `<data.frame>` has columns date, cases, and deaths
checkmate::assert_names(
colnames(data),
must.include = c("date", "cases", "deaths")
)
# check for any NAs among data
checkmate::assert_data_frame(
data[, c("date", "cases", "deaths")],
any.missing = FALSE
)
# check that data$date is a date column
checkmate::assert_date(data$date, any.missing = FALSE, all.missing = FALSE)
checkmate::assert_count(smoothing_window, null.ok = TRUE)
.check_input_data(data)

stopifnot(
"Input data must have sequential dates with none missing or duplicated" =
identical(unique(diff(data$date)), 1), # use numeric 1, not integer
"`smoothing_window` must be an odd number greater than 0" =
(smoothing_window %% 2 != 0),
"`delay_density` must be a function with a single required argument,
Expand Down
43 changes: 43 additions & 0 deletions R/check_input_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.check_input_data <- function(data) {
coll <- checkmate::makeAssertCollection()

checkmate::assert_data_frame(
data,
min.rows = 1, min.cols = 3,
add = coll
)
# check that input `<data.frame>` has columns date, cases, and deaths
checkmate::assert_names(
colnames(data),
must.include = c("date", "cases", "deaths"),
add = coll
)
# check for any NAs among data
checkmate::assert_data_frame(
data[, c("date", "cases", "deaths")],
types = c("Date", "integerish"),
any.missing = FALSE,
add = coll
)
# check that data$date is a date column
checkmate::assert_date(
data$date,
any.missing = FALSE,
all.missing = FALSE,
add = coll
)

# Check count types
checkmate::assert_integerish(data$cases, lower = 0, add = coll)
checkmate::assert_integerish(data$deaths, lower = 0, add = coll)

stopifnot(
"Input data must have sequential dates with none missing or duplicated" =
identical(unique(diff(data$date)), 1) # use numeric 1, not integer
# this solution works when df$date is `Date`
# this may need more thought for dates that are integers, POSIXct,
# or other units; consider the units package
)

checkmate::reportAssertions(coll)
}