predict_rsample_nested: Predict assessment data from nested split using recipe and...

Description Usage Arguments Examples

View source: R/rsample_utils.R

Description

This function facilitates extracting, baking, and predicting assessment data from a nested split object created with rolling_origin_nested. Baking requires a recipe object and predicting requires a fitted model object.

Usage

1
2
3
4
5
6
7
8
9
predict_rsample_nested(
  split,
  recipe,
  fit,
  id_vars = "all",
  predict_options = NULL,
  new_steps = NULL,
  strings_as_factors = FALSE
)

Arguments

split

An rsplit object created with rolling_origin_nested.

recipe

An untrained recipe object.

fit

A fitted model object.

id_vars

A character vector of variables names to be returned along with the predictions. Default is to keep all variables.

predict_options

A named list of arguments passed to predict. For example, if the fitted model is of class merMod list(allow.new.levels = TRUE) may be appropriate.

new_steps

A sequence of steps created using expr or exprs from rlang. This argument adds steps to end of a recipe before bake. This is useful if you want to add steps based on the prediction date. For example, perhaps some predictors are observed at a specific time; so depending on the prediction date, these data cannot be used for prediction. This argument can be used to impute observations that use data after the predicted date. Reference the predicted date with pred_date (see examples).

strings_as_factors

A logical: should character columns be converted to factors? This affects the preprocessed training set (when retain = TRUE) as well as the results of bake.recipe. Unlike prep(), the default is FALSE.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
## Not run: 
data(airquality2)

roll <- rolling_origin_nested(
  data = airquality2,
  time_var = "date",
  unit = "week",
  round_fun = lubridate::round_date
)
rec <-
  recipe(data = airquality2 %>% slice(0), ozone ~ temp + ozone_sample + ozone_sample_date) %>%
  update_role(ozone_sample_date, new_role = "id")

roll2 <- roll %>% mutate(recipe = list(rec))

roll2$fits <-
  map2(roll2$splits, roll2$recipe, fit_rsample_nested, model_func = lm)

roll2$predictions <-
 pmap(
   lst(
    split = roll2$splits,
    recipe = roll2$recipe,
    fit = roll2$fits
  ),
  predict_rsample_nested,
  new_steps = exprs(
    step_mutate_at(
      ozone_sample,
      fn = ~ if_else(ozone_sample_date < pred_date, ozone_sample, as.numeric(NA))),
    step_meanimpute(ozone_sample))
)

## End(Not run)

gacolitti/tidyroll documentation built on Sept. 13, 2020, 8 p.m.