Skip to content

Commit

Permalink
Tests endemic channel (#25)
Browse files Browse the repository at this point in the history
* #23 create endemic channel test

* #23 update endemic channel assertions

* Update documentation

* Update lint

* Uptade lint

* Update lint
  • Loading branch information
Juanmontenegro99 authored Aug 14, 2023
1 parent 691de1d commit 434b205
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 26 deletions.
29 changes: 24 additions & 5 deletions R/endemic_channel.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ auto_endemic_channel <- function(disease_name, divipola_code, year,
outliers_handling = "ignored",
ci = 0.95,
plot = TRUE) {
stopifnot(
"`window` must be a number" =
is.numeric(window)
)
## Data import and cleaning ####

years_to_analyze <- seq(year - window + 1, year)
Expand Down Expand Up @@ -94,7 +98,8 @@ auto_endemic_channel <- function(disease_name, divipola_code, year,
sep = "0"
)),
nchar(.data$COD_MUN_R) == 3 ~
as.numeric(paste0(.data$COD_DPTO_R,
as.numeric(paste0(
.data$COD_DPTO_R,
.data$COD_MUN_R
)),
TRUE ~ NA_real_
Expand All @@ -113,7 +118,8 @@ auto_endemic_channel <- function(disease_name, divipola_code, year,
sep = "0"
)),
nchar(.data$COD_MUN_O) == 3 ~
as.numeric(paste0(.data$COD_DPTO_O,
as.numeric(paste0(
.data$COD_DPTO_O,
.data$COD_MUN_O
)),
TRUE ~ NA_real_
Expand Down Expand Up @@ -200,10 +206,23 @@ endemic_channel <- function(incidence_historic, observations = NULL,
"`incidence_historic` must be an incidence object" =
inherits(incidence_historic, "incidence"),
"`observations`must be numeric and positive" =
(is.numeric(observations) & sign(observations) != -1),
(!is.null(observations) & is.numeric(observations) &
sign(observations) != -1),
"incidence interval should be `1 month`, `1 week` or `1 epiweek`" =
incidence_historic$interval %in%
c("1 month", "1 week", "1 epiweek")
c("1 month", "1 week", "1 epiweek"),
"`observations` size doesn't correspond to incidence interval" =
(!is.null(observations) & ((incidence_historic$interval == "1 week" &
length(observations) == 52) ||
(incidence_historic$interval == "1 month" &
length(observations) == 12))),
"`method` should be `median`, `mean`, `geometric` or `unusual behavior`" =
method %in%
c("median", "mean", "geometric", "unusual_behavior"),
"`ci` must be a number between 0 and 1" =
(ci >= 0 & ci <= 1 & is.numeric(ci)),
"`plot` must be a boolean" =
(is.logical(plot))
)
ifelse(incidence_historic$interval == "1 month",
period <- 12,
Expand Down Expand Up @@ -290,7 +309,7 @@ endemic_channel <- function(incidence_historic, observations = NULL,
low_lim <- c(low_lim, poiss_test$conf.int[1])
}
} else {
return("Error in central tendency method")
stop("Error in central tendency method")
}

channel_data <- data.frame(
Expand Down
20 changes: 1 addition & 19 deletions man/endemic_outliers.Rd

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

96 changes: 94 additions & 2 deletions tests/testthat/test-endemic_channel.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,95 @@
test_that("multiplication works", {
expect_equal(2 * 2, 4)
## data for incidence rate

# dates
set.seed(3)
sample_data <- as.integer(sample(1:3285, 500, replace = TRUE))
sample_dates <- as.Date("2013-01-01") + sample_data

sample_dates <- sample_dates[lubridate::year(lubridate::as_date(sample_dates,
origin = "1970-01-01"
)) < 2020]
sample_dates <- c(as.Date("2013-01-01"), sample_dates, as.Date("2019-12-28"))

sample_df <- data.frame(CASES = sample_dates)

# incidence objects
historic_data <- incidence::incidence(sample_df$CASES, interval = "1 epiweek")
historic_data_mon <- incidence::incidence(sample_df$CASES, interval = "1 month")
historic_data_day <- incidence::incidence(sample_df$CASES, interval = "1 day")


test_that("Endemic channel throws expected erors", {
expect_error(endemic_channel(incidence_historic = c(20, 53, 90, 63)))
expect_error(endemic_channel(
incidence_historic = historic_data,
observations = c(0, 0, -1)
))
expect_error(endemic_channel(
incidence_historic = historic_data_day,
observations = seq(1, 52)
))
expect_error(endemic_channel(
incidence_historic = historic_data,
observations = seq(1, 54)
))
expect_error(endemic_channel(
incidence_historic = historic_data_mon,
observations = seq(1, 14)
))
expect_error(endemic_channel(
incidence_historic = historic_data_mon,
observations = seq(1, 14), method = "poisson"
))
expect_error(endemic_channel(
incidence_historic = historic_data_mon,
observations = seq(1, 14), ci = "0.95"
))
expect_error(endemic_channel(
incidence_historic = historic_data_mon,
observations = seq(1, 14), ci = 1.2
))
expect_error(endemic_channel(
incidence_historic = historic_data_mon,
observations = seq(1, 14), plot = "TRUE"
))
})

test_that("Endemic channel works as expected", {
expect_type(endemic_channel(
incidence_historic = historic_data,
observations = seq(1, 52)
), "list")
expect_type(endemic_channel(
incidence_historic = historic_data,
observations = seq(1, 52),
method = "geometric"
), "list")
expect_type(endemic_channel(
incidence_historic = historic_data,
observations = seq(1, 52),
method = "median"
), "list")
expect_type(endemic_channel(
incidence_historic = historic_data,
observations = seq(1, 52),
method = "mean"
), "list")
expect_type(endemic_channel(
incidence_historic = historic_data,
observations = seq(1, 52),
method = "unusual_behavior"
), "list")
expect_type(endemic_channel(
incidence_historic = historic_data,
observations = seq(1, 52),
plot = TRUE
), "list")
expect_identical(dim(endemic_channel(
incidence_historic = historic_data,
observations = seq(1, 52)
)), c(52, 4))
expect_identical(dim(endemic_channel(
incidence_historic = historic_data_mon,
observations = seq(1, 12)
)), c(12, 4))
})

0 comments on commit 434b205

Please sign in to comment.