Teradata Package for R Function Reference | 17.00 - MovingAverage - Teradata Package for R - Look here for syntax, methods and examples for the functions included in the Teradata Package for R.

Teradata® Package for R Function Reference

Product
Teradata Package for R
Release Number
17.00
Published
July 2021
Language
English (United States)
Last Update
2023-08-08
dita:id
B700-4007
NMT
no
Product Category
Teradata Vantage
MovingAverage

Description

The MovingAverage function calculates the moving average of the target columns based on the moving average types argument ("mvgtype").
Possible moving average types:

  • 'C' - Cumulative moving average.

  • 'E' - Exponential moving average.

  • 'M' - Modified moving average.

  • 'S' - Simple moving average.

  • 'T' - Triangular moving average.

  • 'W' - Weighted moving average.

Note: This function is only available when tdplyr is connected to Vantage 1.1 or later versions.

Usage

  td_moving_average_sqle (
      data = NULL,
      target.columns = NULL,
      alpha = 0.1,
      start.rows = 2,
      window.size = 10,
      include.first = FALSE,
      mavgtype = "C",
      data.partition.column = NULL,
      data.order.column = NULL
  )

Arguments

data

Required Argument.
Specifies the name of the tbl_teradata that contains the columns.

data.partition.column

Required Argument.
Specifies Partition By columns for "data".
Values to this argument can be provided as a vector, if multiple columns are used for partition.
Types: character OR vector of Strings (character)

data.order.column

Required Argument.
Specifies Order By columns for "data".
Values to this argument can be provided as a vector, if multiple columns are used for ordering.
Types: character OR vector of Strings (character)

target.columns

Optional Argument.
Specifies the input column names for which the moving average is to be computed. If you omit this argument, then the function copies every input column to the output tbl_teradata but does not compute moving average.
Types: character OR vector of Strings (character)

alpha

Optional Argument.
Specifies the damping factor, a value in the range [0, 1], which represents a percentage in the range [0, 100]. For example, if alpha is 0.2, then the damping factor is 20%. A higher alpha discounts older observations faster. Only used if "mavgtype" is E. For other moving average types this value will be ignored.
Default Value: 0.1
Types: numeric

start.rows

Optional Argument.
Specifies the number of rows at the beginning of the time series that the function skips before it begins the calculation of the exponential moving average. The function uses the arithmetic average of these rows as the initial value of the exponential moving average. Only used if "mavgtype" is E. For other moving average types this value will be ignored. The value n must be an integer.
Default Value: 2
Types: integer

window.size

Optional Argument.
Specifies the number of previous values to include in the computation of the moving average if "mavgtype" is M, S, T, and W. For other moving average types this value will be ignored.
Default Value: 10
Types: integer

include.first

Optional Argument.
Specifies whether the first starting rows should be included in the output or not. Only used if "mavgtype" is S, M, W, E, T. For cumulative moving average types this value will be ignored.
Default Value: FALSE
Types: logical

mavgtype

Optional Argument.
Specify the moving average type that needs to be used for computing moving averages of "target.columns".
Following are the different type of averages calculated by MovingAverage td_moving_average_sqle function:

  • "S": The MovingAverage function computes the simple moving average of points in a series.

  • "W": The MovingAverage function computes the weighted moving average the average of points in a time series, applying weights to older values. The weights for the older values decrease arithmetically.

  • "E": The MovingAverage function computes the exponential moving average of the points in a time series, exponentially decreasing the weights of older values.

  • "C": The MovingAverage function computes the cumulative moving average of a value from the beginning of a series.

  • "M": The MovingAverage function computes moving average of points in series.

  • "T": The MovingAverage function computes double-smoothed average of points in series.

Default Value: "C"
Permitted Values: C, S, M, W, E, T
Types: character

Value

Function returns an object of class "td_moving_average_sqle" which is a named list containing object of class "tbl_teradata".
Named list member can be referenced directly with the "$" operator using name: result.

Examples

  
    # Get the current context/connection
    con <- td_get_context()$connection
    
    # Load example data.
    loadExampleData("exponentialmovavg_example", "ibm_stock")
    loadExampleData("weightedmovavg_example", "stock_vol")
    
    # Create object(s) of class "tbl_teradata".
    ibm_stock <- tbl(con, "ibm_stock")
    
    # Example 1: Compute the exponential moving average
    td_exponential_mov_avg_out <- td_moving_average_sqle(data = ibm_stock,
                                                         data.partition.column = c("name"),
                                                         data.order.column = c("period"),
                                                         target.columns = c("stockprice"),
                                                         start.rows = 10,
                                                         include.first = TRUE,
                                                         mavgtype = "E"
                                                         )
                                                         
    # Example 2: Compute the cumulative moving average for "stockprice".
    td_cumulative_mov_avg_out <- td_moving_average_sqle(data = ibm_stock,
                                                       data.partition.column = c("name"),
                                                       data.order.column = c("period"),
                                                       target.columns = c("stockprice"),
                                                       mavgtype = "C"
                                                       ) 
     
    # Example 3: Compute the simple moving average for "stockprice".
    td_simple_mov_avg_out <- td_moving_average_sqle(data = ibm_stock,
                                           data.partition.column = "name",
                                           data.order.column = "period",
                                           target.columns = "stockprice",
                                           include.first = TRUE,
                                           window.size = 10,
                                           mavgtype = "S"
                                           )
                                           
    # The input table, stock_vol, contains hypothetical stock price and volume data of three
    # companies between 17 May 1961 and 21 June 1961.
    stock_vol <- tbl(con, "stock_vol")
      
    # Example 4: This example computes the weighted moving average for stockprice and volume
    # for three companies.
    td_weighted_mov_avg_out <- td_moving_average_sqle(data = stock_vol,
                                                   data.partition.column = c("id"),
                                                   data.order.column = c("name"),
                                                   target.columns = c("stockprice","volume"),
                                                   include.first = TRUE,
                                                   window.size = 5,
                                                   mavgtype = "W"
                                                   )  
                                                   
    # Example 5: Triangular Moving Average
    td_triangular_mov_avg_out <- td_moving_average_sqle(data = stock_vol,
                                                   data.partition.column = "name",
                                                   data.order.column = "period",
                                                   target.columns = c("stockprice"),
                                                   include.first = TRUE,
                                                   window.size = 3,
                                                   mavgtype = "T"
                                                   )
                                                   
     # Example 6: Modified Moving Average.
     td_modified_mov_avg_out <- td_moving_average_sqle(data = stock_vol,
                                                   data.partition.column = "name",
                                                   data.order.column = "period",
                                                   target.columns = c("stockprice"),
                                                   include.first = TRUE,
                                                   window.size = 3,
                                                   mavgtype = "M"
                                                   )