-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Sketching out refactor * Some restructure for clarity * Refactor along new date approach * Clear tests * Refactor lines to save col_names * Refactor validation functionality * Redocument * Remove epidist_validate and move to _model and _data approach plus some linting * Add documentation of as_epidist_linelist arguments * Move assert_class into imports and use in place of "check" class * Documentation for epidist_validate_data.epidist_linelist * Clear up the direct model file a bit * Add creating the row_id back in to as_latent_individual * Passing test-direct_model * Start working to make data use dates * Add start of unit tests and bug fix for datetime class check * Use .row_id rather than row_id * Use as_epidist_linelist_time function so that tests work with time data * Fixes to tests * Group into preprocessing functions * Update FAQ vignette to run * Update get started vignette to run * Update ebola vignette to run * Update approximate inference vignette to run * Add documentation * Methods consistency * Document ... * Again on ... * Remove comment moved to issue * Include as_epidist_linelist_time ad-hoc * Add test for datetime column * Update text in vignettes and add note about the ad-hoc function being included in package soon * Refactor .rename_columns Former-commit-id: c573ba836b76170c03d5c493cbb378781db5fa23 [formerly bac50e38d758dfe0fdcfd98722dc50a5a98c0357] Former-commit-id: 4e9d3ee55e7e0c90bd35366990f38aa4894b3439 Former-commit-id: 84a5299
Showing
42 changed files
with
446 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,77 @@ | ||
#' Add columns for interval censoring of primary and secondary events | ||
#' Prepare date data in the `epidist_linelist` format | ||
#' | ||
#' @param linelist ... | ||
#' @param ptime_lwr ... | ||
#' @param ptime_upr ... | ||
#' @param pwindow ... | ||
#' @param stime_lwr ... | ||
#' @param stime_upr ... | ||
#' @param swindow ... | ||
#' @param data A `data.frame` containing line list data | ||
#' @param pdate_lwr,pdate_upr,sdate_lwr,sdate_upr Strings giving the column of | ||
#' `data` containing the primary and secondary event upper and lower bounds. | ||
#' These columns of `data` must be as datetime. | ||
#' @param obs_date A string giving the column of `data` containing the | ||
#' observation time as a datetime. | ||
#' @family preprocess | ||
#' @autoglobal | ||
#' @export | ||
add_event_vars <- function( | ||
linelist, ptime_lwr = NULL, ptime_upr = NULL, pwindow = NULL, | ||
stime_lwr = NULL, stime_upr = NULL, swindow = NULL | ||
as_epidist_linelist <- function( | ||
data, pdate_lwr = NULL, pdate_upr = NULL, sdate_lwr = NULL, sdate_upr = NULL, | ||
obs_date = NULL | ||
) { | ||
linelist <- .rename_column(linelist, "ptime_lwr", ptime_lwr) | ||
linelist <- .rename_column(linelist, "ptime_upr", ptime_upr) | ||
linelist <- .rename_column(linelist, "stime_lwr", stime_lwr) | ||
linelist <- .rename_column(linelist, "stime_upr", stime_upr) | ||
linelist <- .rename_column(linelist, "pwindow", pwindow) | ||
linelist <- .rename_column(linelist, "swindow", swindow) | ||
class(data) <- c("epidist_linelist", class(data)) | ||
|
||
if (is.numeric(pwindow)) { | ||
cli::cli_warn("Overwriting using numeric value(s) of pwindow provided!") | ||
linelist$pwindow <- pwindow | ||
} | ||
|
||
if (is.numeric(swindow)) { | ||
cli::cli_warn("Overwriting using numeric value(s) of swindow provided!") | ||
linelist$swindow <- swindow | ||
} | ||
|
||
if (is.null(stime_upr)) { | ||
linelist <- mutate(linelist, stime_upr = stime_lwr + swindow) | ||
} | ||
|
||
if (is.null(ptime_upr)) { | ||
linelist <- mutate(linelist, ptime_upr = ptime_lwr + pwindow) | ||
} | ||
data <- .rename_columns(data, | ||
new_names = c( | ||
"pdate_lwr", "pdate_upr", "sdate_lwr", "sdate_upr", "obs_date" | ||
), | ||
old_names = c(pdate_lwr, pdate_upr, sdate_lwr, sdate_upr, obs_date) | ||
) | ||
|
||
if (is.null(swindow)) { | ||
linelist <- mutate(linelist, pwindow = stime_upr - stime_lwr) | ||
} | ||
# Check for being a datetime | ||
assert_true(any(inherits(data$pdate_lwr, c("POSIXct", "POSIXlt")))) | ||
assert_true(any(inherits(data$pdate_upr, c("POSIXct", "POSIXlt")))) | ||
assert_true(any(inherits(data$sdate_lwr, c("POSIXct", "POSIXlt")))) | ||
assert_true(any(inherits(data$sdate_upr, c("POSIXct", "POSIXlt")))) | ||
assert_true(any(inherits(data$obs_date, c("POSIXct", "POSIXlt")))) | ||
|
||
if (is.null(pwindow)) { | ||
linelist <- mutate(linelist, swindow = ptime_upr - ptime_lwr) | ||
} | ||
# Convert datetime to time | ||
min_date <- min(data$pdate_lwr) | ||
|
||
assert_numeric(linelist$ptime_lwr) | ||
assert_numeric(linelist$ptime_upr) | ||
assert_numeric(linelist$pwindow, lower = 0) | ||
assert_true( | ||
all(linelist$ptime_lwr + linelist$pwindow - linelist$ptime_upr < 1e-6) | ||
data <- mutate(data, | ||
ptime_lwr = as.numeric(.data$pdate_lwr - min_date), | ||
ptime_upr = as.numeric(.data$pdate_upr - min_date), | ||
stime_lwr = as.numeric(.data$sdate_lwr - min_date), | ||
stime_upr = as.numeric(.data$sdate_upr - min_date), | ||
obs_time = as.numeric(.data$obs_date - min_date) | ||
) | ||
|
||
assert_numeric(linelist$stime_lwr) | ||
assert_numeric(linelist$stime_upr) | ||
assert_numeric(linelist$swindow, lower = 0) | ||
assert_true( | ||
all(linelist$stime_lwr + linelist$swindow - linelist$stime_upr < 1e-6) | ||
) | ||
epidist_validate_data(data) | ||
|
||
return(data) | ||
} | ||
|
||
linelist <- dplyr::relocate( | ||
linelist, ptime_lwr, ptime_upr, pwindow, stime_lwr, stime_upr, swindow | ||
#' Validation for the `epidist_linelist` class | ||
#' | ||
#' @inheritParams as_epidist_linelist | ||
#' @param ... Additional arguments | ||
#' @family preprocess | ||
#' @export | ||
epidist_validate_data.epidist_linelist <- function(data, ...) { | ||
assert_true(is_epidist_linelist(data)) | ||
assert_data_frame(data) | ||
col_names <- c( | ||
"case", "ptime_lwr", "ptime_upr", "stime_lwr", "stime_upr", "obs_time" | ||
) | ||
assert_names(names(data), must.include = col_names) | ||
assert_numeric(data$ptime_lwr, lower = 0) | ||
assert_numeric(data$ptime_upr, lower = 0) | ||
assert_true(all(data$ptime_upr - data$ptime_lwr > 0)) | ||
assert_numeric(data$stime_lwr, lower = 0) | ||
assert_numeric(data$stime_upr, lower = 0) | ||
assert_true(all(data$stime_upr - data$stime_lwr > 0)) | ||
assert_numeric(data$obs_time, lower = 0) | ||
} | ||
|
||
return(linelist) | ||
#' Check if data has the `epidist_linelist` class | ||
#' | ||
#' @inheritParams as_epidist_linelist | ||
#' @param ... Additional arguments | ||
#' @family preprocess | ||
#' @export | ||
is_epidist_linelist <- function(data, ...) { | ||
inherits(data, "epidist_linelist") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,45 @@ | ||
#' Validate a data object for use with [epidist()] | ||
#' Validate data class | ||
#' | ||
#' This function validates that the provided `data` is suitable to run a | ||
#' particular `epidist` model. This may include checking the class of `data`, | ||
#' and that it contains suitable columns. | ||
#' @inheritParams epidist | ||
#' @param ... Additional arguments | ||
#' @family validate | ||
#' @export | ||
epidist_validate_data <- function(data, ...) { | ||
UseMethod("epidist_validate_data") | ||
} | ||
|
||
#' Default method for validate data class | ||
#' | ||
#' @inheritParams epidist | ||
#' @param ... Additional arguments | ||
#' @family validate | ||
#' @export | ||
epidist_validate_data.default <- function(data, ...) { | ||
cli_abort( | ||
"No epidist_validate_data method implemented for the class ", class(data), | ||
"\n", "See methods(epidist_validate_data) for available methods" | ||
) | ||
} | ||
|
||
#' Validate model class | ||
#' | ||
#' @inheritParams epidist | ||
#' @param ... Additional arguments | ||
#' @family validate | ||
#' @export | ||
epidist_validate <- function(data, ...) { | ||
UseMethod("epidist_validate") | ||
epidist_validate_model <- function(data, ...) { | ||
UseMethod("epidist_validate_model") | ||
} | ||
|
||
#' Default method for data validation | ||
#' Default method for validate model class | ||
#' | ||
#' @inheritParams epidist | ||
#' @param ... Additional arguments | ||
#' @family validate | ||
#' @export | ||
epidist_validate.default <- function(data, ...) { | ||
epidist_validate_model.default <- function(data, ...) { | ||
cli_abort( | ||
"No epidist_validate method implemented for the class ", class(data), "\n", | ||
"See methods(epidist_validate) for available methods" | ||
"No epidist_validate_model method implemented for the class ", class(data), | ||
"\n", "See methods(epidist_validate_model) for available methods" | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,46 @@ | ||
test_that("add_event_vars produces equivalent linelists in different ways", { # nolint: line_length_linter. | ||
linelist <- tibble::tibble( | ||
"a" = runif(100), | ||
"b" = 1, | ||
"c" = a + b, | ||
"d" = runif(100, 2, 3), | ||
"e" = 1, | ||
"f" = d + e | ||
test_that("as_epidist_linelist assigns epidist_linelist class to data", { | ||
data <- data.frame( | ||
case = 1, | ||
pdate_lwr = as.POSIXct("2023-01-01 00:00:00"), | ||
pdate_upr = as.POSIXct("2023-01-02 00:00:00"), | ||
sdate_lwr = as.POSIXct("2023-01-03 00:00:00"), | ||
sdate_upr = as.POSIXct("2023-01-04 00:00:00"), | ||
obs_date = as.POSIXct("2023-01-05 00:00:00") | ||
) | ||
linelist <- as_epidist_linelist( | ||
data, "pdate_lwr", "pdate_upr", "sdate_lwr", "sdate_upr", "obs_date" | ||
) | ||
expect_s3_class(linelist, "epidist_linelist") | ||
}) | ||
|
||
ll <- linelist |> | ||
add_event_vars( | ||
ptime_lwr = "a", pwindow = "b", ptime_upr = "c", | ||
stime_lwr = "d", swindow = "e", stime_upr = "f" | ||
) | ||
|
||
ll2 <- select(linelist, a, c, d, f) |> | ||
add_event_vars( | ||
ptime_lwr = "a", pwindow = 1, ptime_upr = "c", | ||
stime_lwr = "d", swindow = 1, stime_upr = "f" | ||
) | ||
|
||
ll3 <- select(linelist, a, b, d, e) |> | ||
add_event_vars( | ||
ptime_lwr = "a", pwindow = "b", stime_lwr = "d", swindow = "e", | ||
) | ||
test_that("as_epidist_linelist correctly renames columns", { | ||
data <- data.frame( | ||
case = 1, | ||
p_lower = as.POSIXct("2023-01-01"), | ||
p_upper = as.POSIXct("2023-01-02"), | ||
s_lower = as.POSIXct("2023-01-03"), | ||
s_upper = as.POSIXct("2023-01-04"), | ||
observation = as.POSIXct("2023-01-05") | ||
) | ||
linelist <- as_epidist_linelist( | ||
data, "p_lower", "p_upper", "s_lower", "s_upper", "observation" | ||
) | ||
col_names <- c("pdate_lwr", "pdate_upr", "sdate_lwr", "sdate_upr", "obs_date") | ||
expect_true(all(col_names %in% names(linelist))) | ||
}) | ||
|
||
ll4 <- select(linelist, a, c, d, f) |> | ||
add_event_vars( | ||
ptime_lwr = "a", ptime_upr = "c", stime_lwr = "d", stime_upr = "f", | ||
test_that("as_epidist_linelist gives error if columns are not datetime", { | ||
data <- data.frame( | ||
case = 1, | ||
pdate_lwr = as.Date("2023-01-01"), | ||
pdate_upr = as.Date("2023-01-02"), | ||
sdate_lwr = as.Date("2023-01-03"), | ||
sdate_upr = as.Date("2023-01-04"), | ||
obs_date = as.Date("2023-01-05") | ||
) | ||
expect_error( | ||
as_epidist_linelist( | ||
data, "pdate_lwr", "pdate_upr", "sdate_lwr", "sdate_upr", "obs_date" | ||
) | ||
|
||
expect_equal(ll, ll2) | ||
expect_equal(ll, ll3) | ||
expect_equal(ll, ll4) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters