step_ma: Extract moving average features

Description Usage Arguments Details Value Examples

View source: R/step_ma.R

Description

step_ma creates a specification of a recipe step that will extract moving average features from an asset price historical data.

Usage

1
2
3
4
5
6
7
step_ma(recipe, ..., ma_fun = TTR::SMA, n = 10, weights = NULL,
  ma_options = list(), state = FALSE, ratio = TRUE, prefix = "ma",
  prices = NULL, role = "predictor", trained = FALSE, skip = FALSE,
  id = rand_id("ma"))

## S3 method for class 'step_ma'
tidy(x, info = "terms", ...)

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 (from recipes package) for more details.

ma_fun

A function to extract moving average, or a character vector of length one which specify a moving average function. Defaults to TTR::SMA.

n

A numeric vector of length one which specify the moving average window.

weights

A character vector of length one that specify a column name, or a numeric vector for ma_fun that has wts or volume argument. See details for more information.

ma_options

A list of additional argument(s) that would be passed to ma_fun function.

state

An option to specify whether to return the current states of the calculated moving averages. See details for more informations. Defaults to FALSE.

ratio

Whether to return the moving average spread as ratio or absolute difference. See details for more informations. Defaults to TRUE.

prefix

A character vector of length one that would be used as a prefix to the created columns.

prices

A container for selected prices columns. Leave to NULL as it will be populated by prep() function.

role

For model terms created by this step, what analysis role should they be assigned? By default, the function assumes that the created columns will be used as "predictors" in a model.

trained

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

skip

A logical. Should the step be skipped when the recipe is baked by bake()? While all operations are baked when prep() 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_ma object.

info

Options for tidy() method; whether to return tidied information for used "terms" or "params"

Details

The output from this step are several new columns which contains the extracted moving average features.

For basic output, this step will produces:

If state argument is TRUE, it will also produces:

Note that if ratio argument is TRUE, the spread value would be returned as a ratio to prices variable instead of an absolute difference.

Value

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

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# import libs
library(quantrecipes)

# basic usage
rec <- recipe(. ~ ., data = btcusdt) %>%
  step_ma(close) %>%
  step_naomit(all_predictors()) %>%
  prep()

# get preprocessed data
juice(rec)

# using state argument
rec <- recipe(. ~ ., data = btcusdt) %>%
  step_ma(close, state = TRUE) %>%
  step_naomit(all_predictors()) %>%
  prep()

# get preprocessed data
juice(rec)

# using pass-through options
rec <- recipe(. ~ ., data = btcusdt) %>%
  step_ma(close,
    ma_fun = TTR::EMA,
    n = 10,
    ma_options = list(wilder = TRUE),
    state = TRUE
  ) %>%
  step_naomit(all_predictors()) %>%
  prep()

# get preprocessed data
juice(rec)

# using custom weights for weighted moving average
rec <- recipe(. ~ ., data = btcusdt) %>%
  step_ma(close,
    ma_fun = TTR::WMA,
    n = 10,
    weights = c(rep(1, 9), 10),
    state = TRUE
  ) %>%
  step_naomit(all_predictors()) %>%
  prep()

# get preprocessed data
juice(rec)

# using volume-based moving average
rec <- recipe(. ~ ., data = btcusdt) %>%
  step_ma(close,
    ma_fun = TTR::VWMA,
    n = 10,
    weights = "volume",
    state = TRUE
  ) %>%
  step_naomit(all_predictors()) %>%
  prep()

# get preprocessed data
juice(rec)

bagasbgy/quantrecipes documentation built on Dec. 25, 2019, 7:54 a.m.