Applies an extended ARMA filter to a time series
xarmaFilter.RdFilter time series with an extended arma filter.
If whiten is FALSE (default) the function applies
the given ARMA filter to eps (eps is often
white noise). If whiten is TRUE the function applies
the “inverse filter” to \(x\), effectively computing
residuals.
Usage
xarmaFilter(model, x = NULL, eps = NULL, from = NULL, whiten = FALSE,
xcenter = NULL, xintercept = NULL)Arguments
- x
the time series to be filtered, a vector.
- eps
residuals, a vector or NULL.
- model
the model parameters, a list with components
"ar","ma","center"and"intercept", see Details.- from
the index from which to start filtering.
- whiten
if TRUE use
xas input and apply the inverse filter to produceeps("whiten"x), if FALSE useepsas input and generatex("colour"eps).- xcenter
a vector of means of the same length as the time series, see Details.
- xintercept
a vector of intercepts having the length of the series, see Details.
Details
The model is specified by argument model, which is a list with
the following components:
arthe autoregression parameters,
mathe moving average parameters,
centercenter by this value,
interceptintercept.
model$center and model$intercept are scalars and usually
at most one of them is nonzero. They can be considered part of the
model specification. In contrast, arguments xcenter and
xintercept are vectors of the same length as x. They can
represent contributions from covariate variables. Usually at most one
of xcenter and xintercept is used.
The description below uses \(\mu_t\) and \(c_t\) for
the contributions by model$center plus xcenter and
model$intercept plus xintercept, respectively.
The time series \(\{x_t\}\) and \(\{\varepsilon_t\}\) are
represented by x and eps in the R code.
Let
$$y_t = x_t - \mu_t$$
be the centered series. where the centering term \(\mu_t\)
is essentially the sum of center and xcenter and is not
necessarilly the mean. The equation relating the centered series,
\(y_t=x_t - \mu_t\), and eps is the
following:
$$ y_t
= c_t
+ \sum_{i=1}^{p} \phi(i)y _{t-i}
+ \sum_{i=1}^{q} \theta(i)\varepsilon_{t-i}
+ \varepsilon_t
$$
where \(c_t\) is the intercept (basically the sum of
intercept with xintercept).
If whiten = FALSE, \(y_t\) is computed for
t=from,...,n using the above formula, i.e. the filter is
applied to get y from eps (and some initial values). If
eps is white noise, it can be said that y is obtained by
``colouring'' the white noise eps. This can be used, for
example, to simulate ARIMA time series. Finally, the centering term
is added back, \(x_t=y_t+\mu_t\) for t=from,...,n, and the
modified x is returned. The first from - 1 elements of
x are left unchanged.
The inverse filter is obtained by rewriting the above equation as an equation expressing \(\varepsilon_t\) in terms of the remaining quantities: $$ \varepsilon_t = - c_t - \sum_{i=1}^{q} \theta(i)\varepsilon_{t-i} - \sum_{i=1}^{p} \phi (i)y _{t-i} + y_t $$
If whiten = TRUE, xarmaFilter uses this formula for
t=from,...,n to compute eps from y (and some
initial values). If eps is white noise, then it can be said
that the time series y has been whitened.
In both cases the first few values in x and/or
eps are used as initial values.
The centering is formed from model$center and argument
xcenter. If model$center is supplied it is recycled
to the length of the series, x, and subtracted from
x. If argument xcenter is supplied, it is subtracted
from x. If both model$center and xcenter are
supplied their sum is subtracted from x.
xarmaFilter can be used to simulate ARMA series with the
default value of whiten = FALSE. In this case eps is the
input series and y the output:
Then model$center and/or xcenter are added to y
to form the output vector x.
Residuals corresponding to a series x can be obtained by
setting whiten = TRUE. In this case x is the input series.
The elements of the output vector eps are calculated by the
formula for \(\varepsilon_{t}\) given above.
There is no need in this case to restore x since eps is
returned.
In both cases any necessary initial values are assumed to be already
in the vectors and provide the first from - 1 values in the
returned vectors. Argument from should not be smaller than the
default value max(p,q)+1.
xarmaFilter calls the lower level function coreXarmaFilter
to do the computation.
Value
the result of applying the filter or its inverse, as descibed in Details:
if whiten = FALSE, the modified x;
if whiten = TRUE, the modified eps.
Examples
## define a seasonal ARIMA model
m1 <- new("SarimaModel", iorder = 1, siorder = 1, ma = -0.3, sma = -0.1, nseasons = 12)
model0 <- modelCoef(m1, "ArmaModel")
model1 <- as(model0, "list")
ap.1 <- xarmaFilter(model1, x = AirPassengers, whiten = TRUE)
ap.2 <- xarmaFilter(model1, x = AirPassengers, eps = ap.1, whiten = FALSE)
ap <- AirPassengers
ap[-(1:13)] <- 0 # check that the filter doesn't use x, except for initial values.
ap.2a <- xarmaFilter(model1, x = ap, eps = ap.1, whiten = FALSE)
ap.2a - ap.2 ## indeed = 0
#> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> 1949 0 0 0 0 0 0 0 0 0 0 0 0
#> 1950 0 0 0 0 0 0 0 0 0 0 0 0
#> 1951 0 0 0 0 0 0 0 0 0 0 0 0
#> 1952 0 0 0 0 0 0 0 0 0 0 0 0
#> 1953 0 0 0 0 0 0 0 0 0 0 0 0
#> 1954 0 0 0 0 0 0 0 0 0 0 0 0
#> 1955 0 0 0 0 0 0 0 0 0 0 0 0
#> 1956 0 0 0 0 0 0 0 0 0 0 0 0
#> 1957 0 0 0 0 0 0 0 0 0 0 0 0
#> 1958 0 0 0 0 0 0 0 0 0 0 0 0
#> 1959 0 0 0 0 0 0 0 0 0 0 0 0
#> 1960 0 0 0 0 0 0 0 0 0 0 0 0
##ap.3 <- xarmaFilter(model1, x = list(init = AirPassengers[1:13]), eps = ap.1, whiten = TRUE)
## now set some non-zero initial values for eps
eps1 <- numeric(length(AirPassengers))
eps1[1:13] <- rnorm(13)
ap.A <- xarmaFilter(model1, x = AirPassengers, eps = eps1, whiten = TRUE)
ap.Ainv <- xarmaFilter(model1, x = ap, eps = ap.A, whiten = FALSE)
AirPassengers - ap.Ainv # = 0
#> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> 1949 0 0 0 0 0 0 0 0 0 0 0 0
#> 1950 0 0 0 0 0 0 0 0 0 0 0 0
#> 1951 0 0 0 0 0 0 0 0 0 0 0 0
#> 1952 0 0 0 0 0 0 0 0 0 0 0 0
#> 1953 0 0 0 0 0 0 0 0 0 0 0 0
#> 1954 0 0 0 0 0 0 0 0 0 0 0 0
#> 1955 0 0 0 0 0 0 0 0 0 0 0 0
#> 1956 0 0 0 0 0 0 0 0 0 0 0 0
#> 1957 0 0 0 0 0 0 0 0 0 0 0 0
#> 1958 0 0 0 0 0 0 0 0 0 0 0 0
#> 1959 0 0 0 0 0 0 0 0 0 0 0 0
#> 1960 0 0 0 0 0 0 0 0 0 0 0 0
## compare with sarima.f (an old function)
## compute predictions starting at from = 14
pred1 <- sarima.f(past = AirPassengers[1:13], n = 131, ar = model1$ar, ma = model1$ma)
pred2 <- xarmaFilter(model1, x = ap, whiten = FALSE)
pred2 <- pred2[-(1:13)]
all(pred1 == pred2) ##TRUE
#> [1] TRUE