moving_average: Manipulation of moving averages

View source: R/1_moving_average.R

moving_averageR Documentation

Manipulation of moving averages

Description

Manipulation of moving averages

Usage

moving_average(
  x,
  lags = -length(x),
  trailing_zero = FALSE,
  leading_zero = FALSE
)

is.moving_average(x)

is_symmetric(x)

upper_bound(x)

lower_bound(x)

mirror(x)

## S3 method for class 'moving_average'
rev(x)

## S3 method for class 'moving_average'
length(x)

to_seasonal(x, s)

## S4 method for signature 'moving_average'
show(object)

Arguments

x

vector of coefficients

lags

integer indicating the number of lags of the moving average.

trailing_zero, leading_zero

boolean indicating wheter to remove leading/trailing zero and NA.

s

seasonal period for the to_seasonal() function.

object

moving_average object.

Examples

y <- retailsa$AllOtherGenMerchandiseStores
e1 <- moving_average(rep(1,12), lags = -6)
e1 <- e1/sum(e1)
e2 <- moving_average(rep(1/12, 12), lags = -5)
M2X12 <- (e1 + e2)/2
coef(M2X12)
M3 <- moving_average(rep(1/3, 3), lags = -1)
M3X3 <- M3 * M3
# M3X3 moving average applied to each month
M3X3
M3X3_seasonal <- to_seasonal(M3X3, 12)
# M3X3_seasonal moving average applied to the global series
M3X3_seasonal

def.par <- par(no.readonly = TRUE)
par(mai = c(0.5, 0.8, 0.3, 0))
layout(matrix(c(1,2), nrow = 1))
plot_gain(M3X3, main = "M3X3 applied to each month")
plot_gain(M3X3_seasonal, main = "M3X3 applied to the global series")
par(def.par)

# To apply the moving average
t <- y * M2X12
# Or use the filter() function:
t <- filter(y, M2X12)
si <- y - t
s <- si * M3X3_seasonal
# or equivalently:
s_mm <- M3X3_seasonal * (1 - M2X12)
s <- y * s_mm
plot(s)

palatej/rjdfilters documentation built on May 8, 2023, 6:28 a.m.