diff --git a/NAMESPACE b/NAMESPACE index 66618a041..08ecab055 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -18,6 +18,7 @@ S3method(epidist_stancode,default) S3method(epidist_stancode,epidist_latent_individual) S3method(epidist_validate,default) S3method(epidist_validate,epidist_latent_individual) +export(add_event_vars) export(add_mean_sd) export(as_latent_individual) export(epidist) diff --git a/R/preprocess.R b/R/preprocess.R new file mode 100644 index 000000000..ccf2b1a67 --- /dev/null +++ b/R/preprocess.R @@ -0,0 +1,69 @@ +#' Add columns for interval censoring of primary and secondary events +#' +#' @param linelist ... +#' @param ptime_lwr ... +#' @param ptime_upr ... +#' @param pwindow ... +#' @param stime_lwr ... +#' @param stime_upr ... +#' @param swindow ... +#' @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 +) { + 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) + + 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) + } + + if (is.null(swindow)) { + linelist <- mutate(linelist, pwindow = stime_upr - stime_lwr) + } + + if (is.null(pwindow)) { + linelist <- mutate(linelist, swindow = ptime_upr - ptime_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) + ) + + 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) + ) + + linelist <- dplyr::relocate( + linelist, ptime_lwr, ptime_upr, pwindow, stime_lwr, stime_upr, swindow + ) + + return(linelist) +} diff --git a/R/utils.R b/R/utils.R index 05dd76ce8..aa4ce62ee 100644 --- a/R/utils.R +++ b/R/utils.R @@ -122,3 +122,11 @@ } return(formula) } + +.rename_column <- function(df, new, old) { + are_char <- is.character(new) & is.character(old) + if (are_char) { + df <- dplyr::rename(df, !!new := !!old) + } + return(df) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index c6643917b..cf80bfada 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -37,6 +37,10 @@ reference: desc: Functions for observing data contents: - has_concept("observe") +- title: Preprocess + desc: Functions for preprocessing data + contents: + - has_concept("preprocess") - title: S3 generics desc: S3 generics for delay modelling contents: diff --git a/man/add_event_vars.Rd b/man/add_event_vars.Rd new file mode 100644 index 000000000..499ae50c3 --- /dev/null +++ b/man/add_event_vars.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/preprocess.R +\name{add_event_vars} +\alias{add_event_vars} +\title{Add columns for interval censoring of primary and secondary events} +\usage{ +add_event_vars( + linelist, + ptime_lwr = NULL, + ptime_upr = NULL, + pwindow = NULL, + stime_lwr = NULL, + stime_upr = NULL, + swindow = NULL +) +} +\arguments{ +\item{linelist}{...} + +\item{ptime_lwr}{...} + +\item{ptime_upr}{...} + +\item{pwindow}{...} + +\item{stime_lwr}{...} + +\item{stime_upr}{...} + +\item{swindow}{...} +} +\description{ +Add columns for interval censoring of primary and secondary events +} +\concept{preprocess} diff --git a/tests/testthat/test-preprocess.R b/tests/testthat/test-preprocess.R new file mode 100644 index 000000000..8f5f6e1da --- /dev/null +++ b/tests/testthat/test-preprocess.R @@ -0,0 +1,36 @@ +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 + ) + + 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", + ) + + ll4 <- select(linelist, a, c, d, f) |> + add_event_vars( + ptime_lwr = "a", ptime_upr = "c", stime_lwr = "d", stime_upr = "f", + ) + + expect_equal(ll, ll2) + expect_equal(ll, ll3) + expect_equal(ll, ll4) +})