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

Teradata® R Package Function Reference

Product
Teradata R Package
Release Number
16.20
Published
February 2020
Language
English (United States)
Last Update
2020-02-28
dita:id
B700-4007
lifecycle
previous
Product Category
Teradata Vantage

Description

The function computes the moving average over a number of points in a series based on the selected moving average type.
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 object that contains the columns.

data.partition.column

Required Argument.
Specifies Partition By columns for data. Values to this argument can be provided as 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 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 object 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.
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. The value n must be an integer.
Default Value: 2
Types: numeric

window.size

Optional Argument.
Specifies the number of previous values to include in the computation of the simple moving average.
Default Value: 10
Types: numeric

include.first

Optional Argument.
Specifies whether the first starting rows should be included in the output or not.
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'.
Permitted Values: C, S, M, W, E, T

  1. "C" (default): Cumulative Moving Average

  2. "S": Simple Moving Average

  3. "M": Modified Moving Average

  4. "W": Weighted Moving Average

  5. "E": Exponential Moving Average

  6. "T": Triangular Moving Average

Types: character

Value

Function returns an object of class "td_moving_average_sqle" which is a named list containing Teradata tbl object. 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 remote tibble objects.
    ibm_stock <- tbl(con, "ibm_stock")
    
    # 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"
                                                         )
                                                         
    # 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"
                                                       ) 
     
    # 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: 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"
                                                   )  
                                                   
    # 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"
                                                   )
                                                   
     # 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"
                                                   )