Skip to content

Commit a495062

Browse files
committed
Use internal checking function to reduce duplication
1 parent df01959 commit a495062

File tree

4 files changed

+35
-74
lines changed

4 files changed

+35
-74
lines changed

R/cfr_rolling.R

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,7 @@ cfr_rolling <- function(data,
6363
" the course of the outbreak."
6464
)
6565
# input checking
66-
checkmate::assert_data_frame(
67-
data,
68-
min.rows = 1, min.cols = 3
69-
)
70-
# check that input `<data.frame>` has columns date, cases, and deaths
71-
checkmate::assert_names(
72-
colnames(data),
73-
must.include = c("date", "cases", "deaths")
74-
)
75-
# check for any NAs among data
76-
checkmate::assert_data_frame(
77-
data[, c("date", "cases", "deaths")],
78-
types = c("Date", "integerish"),
79-
any.missing = FALSE
80-
)
81-
# check that data$date is a date column
82-
checkmate::assert_date(data$date, any.missing = FALSE, all.missing = FALSE)
83-
# check for excessive missing date and throw an error
84-
# also check delay_density
85-
stopifnot(
86-
"Input data must have sequential dates with none missing or duplicated" =
87-
identical(unique(as.numeric(diff(data$date))), 1) # use numeric 1
88-
# this solution works when df$date is `Date`
89-
# this may need more thought for dates that are integers, POSIXct,
90-
# or other units; consider the units package
91-
)
66+
.check_input_data(data)
9267
checkmate::assert_count(poisson_threshold, positive = TRUE)
9368

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

100-
# Check cumulative sums for count type
101-
checkmate::assert_integerish(cumulative_cases, lower = 0)
10275
# use assert_number to set upper limit at total_cases
10376
checkmate::assert_integerish(
10477
cumulative_deaths,

R/cfr_static.R

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,33 +124,7 @@
124124
cfr_static <- function(data,
125125
delay_density = NULL,
126126
poisson_threshold = 100) {
127-
# input checking
128-
checkmate::assert_data_frame(
129-
data,
130-
min.rows = 1, min.cols = 3
131-
)
132-
# check that input `<data.frame>` has columns date, cases, and deaths
133-
checkmate::assert_names(
134-
colnames(data),
135-
must.include = c("date", "cases", "deaths")
136-
)
137-
# check for any NAs among data
138-
checkmate::assert_data_frame(
139-
data[, c("date", "cases", "deaths")],
140-
types = c("Date", "integerish"),
141-
any.missing = FALSE
142-
)
143-
# check that data$date is a date column
144-
checkmate::assert_date(data$date, any.missing = FALSE, all.missing = FALSE)
145-
146-
# check for excessive missing date and throw an error
147-
stopifnot(
148-
"Input data must have sequential dates with none missing or duplicated" =
149-
identical(unique(as.numeric(diff(data$date))), 1) # use numeric 1
150-
# this solution works when df$date is `Date`
151-
# this may need more thought for dates that are integers, POSIXct,
152-
# or other units; consider the units package
153-
)
127+
.check_input_data(data)
154128
checkmate::assert_count(poisson_threshold, positive = TRUE)
155129

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

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

R/cfr_time_varying.R

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,10 @@ cfr_time_varying <- function(data,
101101
# input checking
102102
# zero count allowed to include all data
103103
checkmate::assert_count(burn_in)
104-
105-
# expect rows more than burn in value
106-
checkmate::assert_data_frame(data, min.cols = 3, min.rows = burn_in + 1)
107-
# check that input `<data.frame>` has columns date, cases, and deaths
108-
checkmate::assert_names(
109-
colnames(data),
110-
must.include = c("date", "cases", "deaths")
111-
)
112-
# check for any NAs among data
113-
checkmate::assert_data_frame(
114-
data[, c("date", "cases", "deaths")],
115-
any.missing = FALSE
116-
)
117-
# check that data$date is a date column
118-
checkmate::assert_date(data$date, any.missing = FALSE, all.missing = FALSE)
119104
checkmate::assert_count(smoothing_window, null.ok = TRUE)
105+
.check_input_data(data)
120106

121107
stopifnot(
122-
"Input data must have sequential dates with none missing or duplicated" =
123-
identical(unique(as.numeric(diff(data$date))), 1), # use numeric 1
124108
"`smoothing_window` must be an odd number greater than 0" =
125109
(smoothing_window %% 2 != 0),
126110
"`delay_density` must be a function with a single required argument,

R/check_input_data.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.check_input_data <- function(data) {
2+
3+
checkmate::assert_data_frame(
4+
data,
5+
min.rows = 1, min.cols = 3
6+
)
7+
# check that input `<data.frame>` has columns date, cases, and deaths
8+
checkmate::assert_names(
9+
colnames(data),
10+
must.include = c("date", "cases", "deaths")
11+
)
12+
# check for any NAs among data
13+
checkmate::assert_data_frame(
14+
data[, c("date", "cases", "deaths")],
15+
types = c("Date", "integerish"),
16+
any.missing = FALSE,
17+
)
18+
# check that data$date is a date column
19+
checkmate::assert_date(data$date, any.missing = FALSE, all.missing = FALSE)
20+
21+
# Check count types
22+
checkmate::assert_integerish(data$cases, lower = 0)
23+
checkmate::assert_integerish(data$deaths, lower = 0)
24+
25+
stopifnot(
26+
"Input data must have sequential dates with none missing or duplicated" =
27+
identical(unique(as.numeric(diff(data$date))), 1) # use numeric 1
28+
# this solution works when df$date is `Date`
29+
# this may need more thought for dates that are integers, POSIXct,
30+
# or other units; consider the units package
31+
)
32+
}

0 commit comments

Comments
 (0)