Skip to content

Commit e84778c

Browse files
committed
ready to fill out the predict method, finally
1 parent 5ccb58a commit e84778c

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ S3method(prep,step_epi_lag)
1313
S3method(print,step_epi_ahead)
1414
S3method(print,step_epi_lag)
1515
export("%>%")
16+
export(add_epi_recipe)
1617
export(arx_args_list)
1718
export(arx_forecaster)
1819
export(create_lags_and_leads)
20+
export(default_epi_recipe_blueprint)
1921
export(df_mat_mul)
2022
export(epi_keys)
2123
export(epi_recipe)
24+
export(epi_workflow)
2225
export(get_precision)
2326
export(grab_names)
27+
export(is_epi_recipe)
2428
export(knn_iteraive_ar_args_list)
2529
export(knn_iteraive_ar_forecaster)
2630
export(knnarx_args_list)

R/epi_recipe.R

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,78 @@ epi_form2args <- function(formula, data, ...) {
210210
list(x = data, vars = vars, roles = roles)
211211
}
212212

213+
214+
215+
#' Test for `epi_df` format
216+
#'
217+
#' @param x An object.
218+
#' @return `TRUE` if the object inherits from `epi_recipe`.
219+
#'
220+
#' @export
213221
is_epi_recipe <- function(x) {
214222
inherits(x, "epi_recipe")
215223
}
216224

225+
226+
227+
#' Add an epi_recipe to a workflow
228+
#'
229+
#' @seealso [workflows::add_recipe()]
230+
#' - `add_recipe()` specifies the terms of the model and any preprocessing that
231+
#' is required through the usage of a recipe.
232+
#'
233+
#' - `remove_recipe()` removes the recipe as well as any downstream objects
234+
#'
235+
#' @details
236+
#' Has the same behaviour as [workflows::add_recipe()] but sets a different
237+
#' default blueprint to automatically handle [epiprocess::epi_df] data.
238+
#'
239+
#' @param x A workflow or epi_workflow
240+
#'
241+
#' @param recipe A recipe created using [recipes::recipe()]
242+
#'
243+
#' @param ... Not used.
244+
#'
245+
#' @param blueprint A hardhat blueprint used for fine tuning the preprocessing.
246+
#'
247+
#' [default_epi_recipe_blueprint()] is used.
248+
#'
249+
#' Note that preprocessing done here is separate from preprocessing that
250+
#' might be done automatically by the underlying model.
251+
#'
252+
#' @return
253+
#' `x`, updated with a new recipe preprocessor.
254+
#'
255+
#' @export
256+
#' @examples
257+
#' library(recipes)
258+
#' library(magrittr)
259+
#'
260+
#' recipe <- epi_recipe(mpg ~ cyl, mtcars) %>%
261+
#' step_log(cyl)
262+
#'
263+
#' workflow <- workflow() %>%
264+
#' add_epi_recipe(recipe)
265+
#'
266+
#' workflow
267+
#'
217268
add_epi_recipe <- function(
218269
x, recipe, ..., blueprint = default_epi_recipe_blueprint()) {
219270
add_recipe(x, recipe, ..., blueprint = blueprint)
220271
}
221272

273+
274+
275+
#' Recipe blueprint that accounts for `epi_df` panel data
276+
#'
277+
#' Used for simplicity. See [hardhat::default_recipe_blueprint()] for more
278+
#' details.
279+
#'
280+
#' @inheritParams hardhat::default_recipe_blueprint
281+
#'
282+
#' @details The `bake_dependent_roles` are automatically set to `epi_df` defaults.
283+
#' @return A recipe blueprint.
284+
#' @export
222285
default_epi_recipe_blueprint <-
223286
function(intercept = FALSE, allow_novel_levels = FALSE, fresh = TRUE,
224287
bake_dependent_roles = c("time_value", "geo_value", "key", "raw"),

R/epi_workflow.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,38 @@ predict.epi_workflow <-
1616
is_epi_workflow <- function(x) {
1717
inherits(x, "epi_workflow")
1818
}
19+
20+
workflow <- function(preprocessor = NULL, spec = NULL) {
21+
out <- new_workflow()
22+
23+
if (!is_null(preprocessor)) {
24+
out <- add_preprocessor(out, preprocessor)
25+
}
26+
27+
if (!is_null(spec)) {
28+
out <- add_model(out, spec)
29+
}
30+
31+
out
32+
}
33+
34+
add_preprocessor <- function(x, preprocessor, ..., call = caller_env()) {
35+
check_dots_empty()
36+
37+
if (is_formula(preprocessor)) {
38+
return(add_formula(x, preprocessor))
39+
}
40+
41+
if (is_recipe(preprocessor)) {
42+
return(add_recipe(x, preprocessor))
43+
}
44+
45+
if (is_workflow_variables(preprocessor)) {
46+
return(add_variables(x, variables = preprocessor))
47+
}
48+
49+
abort(
50+
"`preprocessor` must be a formula, recipe, or a set of workflow variables.",
51+
call = call
52+
)
53+
}

man/add_epi_recipe.Rd

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/default_epi_recipe_blueprint.Rd

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/is_epi_recipe.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)