Teradata R Package Function Reference | 17.00 - 17.00 - WeightedMovAvg - Teradata R Package

Teradata® R Package Function Reference

Teradata R Package
September 2020
Programming Reference


The WeightedMovAvg function computes the weighted moving average of points in a time series, applying weights to older values. The weights for the older values decrease arithmetically.


  td_weighted_mov_avg_mle (
      data = NULL,
      target.columns = NULL,
      include.first = FALSE,
      window.size = 10,
      data.sequence.column = NULL,
      data.partition.column = NULL,
      data.order.column = NULL



Required Argument.
Specifies the input tbl_teradata that contains the columns.


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)


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)


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)


Optional Argument.
Specifies whether to include the starting rows in the output tbl_teradata. If you specify TRUE, the output columns for the starting rows contain NA, because their weighted moving average is undefined.
Default Value: FALSE
Types: logical


Optional Argument.
Specifies the number of old values to be considered for calculating the new weighted moving average.
Default Value: 10
Types: integer


Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "data". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.
Types: character OR vector of Strings (character)


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


    # Get the current context/connection
    con <- td_get_context()$connection
    # Load example data.
    loadExampleData("weightedmovavg_example", "stock_vol")

    # Create object(s) of class "tbl_teradata".
    stock_vol <- tbl(con, "stock_vol")

    # Example: Compute the simple moving average for columns: "stockprice" and "volume".
    # The input tbl_teradata, stock_vol, contains hypothetical stock price and volume data of three
    # companies between 17 May 1961 and 21 June 1961.
    # Note: This also includes the first 9 rows with moving average NA.
    td_weighted_mov_avg_out <- td_weighted_mov_avg_mle(data = stock_vol,
                                                       data.partition.column = c("id"),
                                                       data.order.column = c("name"),
                                                       target.columns = c("stockprice","volume"),
                                                       include.first = TRUE,
                                                       window.size = 5