step_diff: Create a differenced predictor

Description Usage Arguments Details Value See Also Examples

View source: R/recipes-step_diff.R

Description

step_diff creates a specification of a recipe step that will add new columns of differenced data. Differenced data will include NA values where a difference was induced. These can be removed with step_naomit().

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
step_diff(
  recipe,
  ...,
  role = "predictor",
  trained = FALSE,
  lag = 1,
  difference = 1,
  log = FALSE,
  prefix = "diff_",
  columns = NULL,
  skip = FALSE,
  id = rand_id("diff")
)

## S3 method for class 'step_diff'
tidy(x, ...)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

One or more selector functions to choose which variables are affected by the step. See selections() for more details.

role

Defaults to "predictor"

trained

A logical to indicate if the quantities for preprocessing have been estimated.

lag

A vector of positive integers identifying which lags (how far back) to be included in the differencing calculation.

difference

The number of differences to perform.

log

Calculates log differences instead of differences.

prefix

A prefix for generated column names, default to "diff_".

columns

A character string of variable names that will be populated (eventually) by the terms argument.

skip

A logical. Should the step be skipped when the recipe is baked by bake.recipe()? While all operations are baked when prep.recipe() is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using skip = TRUE as it may affect the computations for subsequent operations

id

A character string that is unique to this step to identify it.

x

A step_diff object.

Details

The step assumes that the data are already in the proper sequential order for lagging.

Value

An updated version of recipe with the new step added to the sequence of existing steps (if any).

See Also

Time Series Analysis:

Remove NA Values:

Main Recipe Functions:

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
library(tidyverse)
library(tidyquant)
library(recipes)
library(timetk)


FANG_wide <- FANG %>%
    select(symbol, date, adjusted) %>%
    pivot_wider(names_from = symbol, values_from = adjusted)


# Make and apply recipe ----

recipe_diff <- recipe(~ ., data = FANG_wide) %>%
  step_diff(FB, AMZN, NFLX, GOOG, lag = 1:3, difference = 1) %>%
  prep()

recipe_diff %>% bake(FANG_wide)


# Get information with tidy ----

recipe_diff %>% tidy()

recipe_diff %>% tidy(1)

timetk documentation built on Jan. 19, 2021, 1:06 a.m.