Learn R Programming

tsforecast (version 1.3.0)

tsmovav: Generate Moving Averages of a Time Series

Description

The function `tsmovav` calculates the moving averages of a time series.

Usage

tsmovav(
  x,
  order = 3,
  type = c("backward", "center"),
  n.ahead = 0,
  x.name = NULL,
  show.plot = TRUE
)

# S3 method for tsmovav print(x, digits = max(3L, getOption("digits") - 3L), ...)

# S3 method for tsmovav plot(x, title = NULL, ...)

Value

x

original series data

x.time

list of time in which the series values were observed.

x.timegap

time gap between the series and forecasted values.

x.name

name of the time series for which forecasts was requested.

pred

predicted past values and forecasted future values.

pred.time

list of time in which the predictions/forecasts were estimated.

pred.name

name of the series containing the predicted/forecasted values.

se

standard errors of the forecasted values.

cil, ciu

lower and upper limits of the prediction interval.

n.ahead

number of forecasting periods.

forecast.incl

indication of the series part that should be predicted or forecasted.

log

logical. Indicates whether series values are log-transformed for model fitting or not.

alpha

significance level.

order

moving average order.

type

type of moving average.

Arguments

x

a univariate time series object or a numeric vector or matrix, or a `tsmovav` object.

order

moving average order. Default is 3.

type

type of moving average to be calculated. Available options are "backward" and "center". While backward assigns the moving averages to the next period after the averaging window, which is more useful for forecasting purpose, center assigns the moving averages to the middle period of the averaging window, which is more suitable for time series smoothing. Default is `backward`.

n.ahead

number of forecasting periods. Only useful if "type = backward". Default is 0.

x.name

a new name for x. If the parameter is omitted, the current name of the time series will be returned to the user.

show.plot

logical. If TRUE, the smoothing/forecasting plot will be displayed directly. Default is TRUE.

digits

the number of significant digits.

...

other printing or plotting parameters.

title

title of the moving average plot. Default is NULL.

Author

Ka Yui Karl Wu

Details

Centred moving averages are better suited for smoothing a time series than for forecasting. By definition, each moving average is aligned with the midpoint of its averaging window. When the number of periods in the averaging window (i.e., the moving average order) is even, the averages calculated for the two central positions must be combined. Specifically, the mean of these two middle moving averages is assigned to the central period that lies closest to the true midpoint of the series, forming its final centred moving average.

Mathematically, for odd number order \(r\):

$$\tilde{y}_t = \dfrac{y_{t-\frac{r-1}{2}}+\ldots+y_{t+\frac{r+1}{2}}}{r}$$

For even number order \(r\):

$$\tilde{y}_t = \dfrac{0.5y_{t-\frac{r}{2}}+y_{t-\frac{r}{2}+1}+\ldots+y_{t+\frac{r}{2}-1}+0.5y_{t+\frac{r}{2}}}{2r}$$

Backward moving average is a forecasting method that assigns each computed average to the period immediately following the observation window. This approach works the same regardless of whether the moving average order is odd or even.

$$\tilde{y}_t = \dfrac{y_{t-1}+\ldots+x_{y-r}}{r}$$

References

Hyndman, R. J., & Athanasopoulos, G. (2021). Forecasting: Principles and practice (3rd ed.). OTexts.
https://otexts.com/fpp3/

Examples

Run this code
## Backward Moving Average
tsmovav(airport$Travellers, order = 12, type = "backward", n.ahead = 6, show.plot = TRUE)

## Centered Moving Average
tsmovav(airport$Travellers, order = 12, type = "center")

Run the code above in your browser using DataLab