diff --git a/NAMESPACE b/NAMESPACE index f5c1cb52a..71e08fbf9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -70,6 +70,7 @@ export(smooth_arx_args_list) export(smooth_arx_forecaster) export(step_epi_ahead) export(step_epi_lag) +export(step_epi_naomit) export(validate_layer) import(recipes) importFrom(generics,augment) diff --git a/R/step_epi_naomit.R b/R/step_epi_naomit.R new file mode 100644 index 000000000..f266fe636 --- /dev/null +++ b/R/step_epi_naomit.R @@ -0,0 +1,20 @@ +#' Unified NA omission wrapper function for recipes +#' +#' @param recipe Recipe to be used for omission steps +#' +#' @return Omits NA's from both predictors and outcomes at training time +#' to fit the model. Also only omits associated predictors and not +#' outcomes at prediction time due to lack of response and avoidance +#' of data loss. +#' @export +#' @examples +#' case_death_rate_subset %>% +#' epi_recipe() %>% +#' step_epi_naomit() + +step_epi_naomit <- function(recipe) { + stopifnot(inherits(recipe, "recipe")) + recipe %>% + recipes::step_naomit(all_predictors()) %>% + recipes::step_naomit(all_outcomes(), skip = TRUE) +} diff --git a/man/create_layer.Rd b/man/create_layer.Rd index 7917e8854..81f5e33b0 100644 --- a/man/create_layer.Rd +++ b/man/create_layer.Rd @@ -9,9 +9,9 @@ create_layer(name = NULL, open = rlang::is_interactive()) \arguments{ \item{name}{Either a name without extension, or \code{NULL} to create the paired file based on currently open file in the script editor. If -the R file is open, \code{use_test()} will create/open the corresponding +the \verb{R/} file is open, \code{use_test()} will create/open the corresponding test file; if the test file is open, \code{use_r()} will create/open the -corresponding R file.} +corresponding \verb{R/} file.} \item{open}{Whether to open the file for interactive editing.} } diff --git a/man/step_epi_naomit.Rd b/man/step_epi_naomit.Rd new file mode 100644 index 000000000..eccc87c68 --- /dev/null +++ b/man/step_epi_naomit.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/step_epi_naomit.R +\name{step_epi_naomit} +\alias{step_epi_naomit} +\title{Unified NA omission wrapper function for recipes} +\usage{ +step_epi_naomit(recipe) +} +\arguments{ +\item{recipe}{Recipe to be used for omission steps} +} +\value{ +Omits NA's from both predictors and outcomes at training time +to fit the model. Also only omits associated predictors and not +outcomes at prediction time due to lack of response and avoidance +of data loss. +} +\description{ +Unified NA omission wrapper function for recipes +} +\examples{ +case_death_rate_subset \%>\% + epi_recipe() \%>\% + step_epi_naomit() +} diff --git a/tests/testthat/test-step_epi_naomit.R b/tests/testthat/test-step_epi_naomit.R new file mode 100644 index 000000000..65c9d90c9 --- /dev/null +++ b/tests/testthat/test-step_epi_naomit.R @@ -0,0 +1,37 @@ +library(dplyr) +library(parsnip) +library(workflows) + +# Random generated dataset +x <- tibble(geo_value = rep("nowhere",200), + time_value = as.Date("2021-01-01") + 0:199, + case_rate = 1:200, + death_rate = 1:200) %>% + epiprocess::as_epi_df() + +# Preparing the datasets to be used for comparison +r <- epi_recipe(x) %>% + step_epi_ahead(death_rate, ahead = 7) %>% + step_epi_lag(death_rate, lag = c(0,7,14)) + +test_that("Argument must be a recipe", { + expect_error(step_epi_naomit(x)) +}) + +z1 <- step_epi_naomit(r) +z2 <- r %>% + step_naomit(all_predictors()) %>% + step_naomit(all_outcomes(), skip = TRUE) + +# Checks the behaviour of a step function, omitting the quosure and id that +# differ from one another, even with identical behaviour +behav <- function(recipe,step_num) recipe$steps[[step_num]][-1][-5] +# Checks the class type of an object +step_class <- function(recipe,step_num) class(recipe$steps[step_num]) + +test_that("Check that both functions behave the same way", { + expect_identical(behav(z1,3),behav(z2,3)) + expect_identical(behav(z1,4),behav(z2,4)) + expect_identical(step_class(z1,3),step_class(z2,3)) + expect_identical(step_class(z1,4),step_class(z2,4)) +})