# stlOp: Calculate operator matrices for the STL decomposition In hafen/operator: Loess and STL operator matrix computation for time series

## Description

Calculate operator matrices for the STL decomposition for a univariate equally-spaced design time series.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ```## Default S3 method: stlOp(n, n.p, s.window, s.degree = 1, t.window = NULL, t.degree = 1, fc.window = NULL, fc.degree = NULL, l.window = nextodd(n.p), l.degree = t.degree, critfreq = 0.05, inner = 2, n.ahead = 0, s.blend = 0, t.blend = 0, l.blend = t.blend, fc.blend = NULL, fc.name = NULL, arma = NULL, stats = TRUE) ## S3 method for class 'stl' stlOp(x, n.ahead=0, s.blend=0, t.blend=0, l.blend=t.blend, critfreq=0.05, arma=NULL, stats=TRUE) ## S3 method for class 'stl2' stlOp(x, n.ahead=0, arma=NULL, stats=TRUE) ```

## Arguments

 `n` number of observations in the time series. `x` object of class `"stl"` or `"stl2"`. `n.p` the periodicity of the seasonal component. `s.window` either the character string `"periodic"` or the span (in lags) of the loess window for seasonal extraction, which should be odd. This has no default. `s.degree` degree of locally-fitted polynomial in seasonal extraction. Should be 0, 1, or 2. `t.window` the span (in lags) of the loess window for trend extraction, which should be odd. If `NULL`, the default, `nextodd(ceiling((1.5*period) / (1-(1.5/s.window))))`, is taken. `t.degree` degree of locally-fitted polynomial in trend extraction. Should be 0, 1, or 2. `l.window` the span (in lags) of the loess window of the low-pass filter used for each subseries. Defaults to the smallest odd integer greater than or equal to `frequency(x)` which is recommended since it prevents competition between the trend and seasonal components. If not an odd integer its given value is increased to the next odd one. `l.degree` degree of locally-fitted polynomial for the subseries low-pass filter. Should be 0, 1, or 2. `critfreq` the critical frequency to use for automatic calculation of smoothing windows for the trend and high-pass filter. `n.ahead` number of time units into the future to compute the operator matrices for. This will be used for prediction. `fc.window` vector of lengths of windows for loess smoothings for other trend frequency components after the original STL decomposition has been obtained. The smoothing is applied to the data with the STL seasonal component removed. A frequency component is computed by a loess fit with the window length equal to the first element of fc.window, the component is removed, another component is computed with the window length equal to the second element of fc.window, and so forth. In most cases, the values of the argument should be decreasing, that is, the frequency bands of the fitted components should increase. The robustness weights from original STL are used as weights in the loess fitting if specified. `fc.degree` vector of degrees of locally-fitted polynomial in the loess smoothings for the frequency components specified in fc.window. Values of 0, 1 and 2 are allowed. If the length of fc.degree is less than that of fc.window, the former is expanded to the length of the latter using rep; thus, giving the value 1 specifies a degree of 1 for all components. `fc.name` vector of names of the post-trend smoothing operations specified by `fc.window` and `fc.degree` (optional). `inner` integer; the number of ‘inner’ (backfitting) iterations; usually very few (2) iterations suffice. `s.blend, t.blend, l.blend, fc.blend` vectors of proportion of blending to degree 0 polynomials at the endpoints of the series. `arma` object of class `"Arima"`, which specifies the arma model to use on the remainder component. `stats` whether or not to calculate auxiliary statistics for the overall STL fit operator matrix.

## Details

The STL operator matrix is the matrix that yields the fitted values (seasonal + trend) through a linear operation with the observed time series. It is obtained through a series of linear filters. If post-trend smoothing is specified through `fc.window, ...`, then the overall operator matrix will be the seasonal operator plus the operator for each post-trend smoothing. If ARMA modeling is specified, this will also be factored in to the calculation of the overall operator matrix.

## Value

a list of class `"stlop"`.

 `fit` the overall operator matrix, a list of class `"op"`, which is calculated as described in the details section. If `stats=TRUE`, then auxiliary statistics will also be computed and will be available as an element of this list. `seas` the operator matrix for just the seasonal component, an object of class `"op"`. `trend` the operator matrix for just the trend component, a list of class `"op"`. `fc` a list of operator matrices corresponding to post-trend frequency components, if specified. `at` the values at which the operator matrices were calculated, which will either be `1, ..., n` or `1, ..., n + n.ahead`. `pars` parameters used in the fitting.

## Note

This function does a lot of `n`x`n` matrix multiplication. Be aware of this when choosing `n`. While the loess operator matrices are calculated in C, the matrix multiplication happens in R. This is because of the speed of BLAS, which is especially good if R has been compiled with threaded BLAS.

Ryan Hafen

## References

R. B. Cleveland, W. S. Cleveland, J.E. McRae, and I. Terpenning (1990) STL: A Seasonal-Trend Decomposition Procedure Based on Loess. Journal of Official Statistics, 6, 3-73.

W. S. Cleveland, E. Grosse and W. M. Shyu (1992) Local regression models. Chapter 8 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.

`stl`, `plotVar`, `loessOp`, `predict.stlop`
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30``` ```sop <- stlOp(50, n.p=7, s.window="periodic") plotVar(sop) plotVar(sop\$seas) # stl blending the trend t.blend <- list(deg=0, span=11, blend.p=0.5, n.b=20) sop2 <- stlOp(50, n.p=7, s.window="periodic", t.blend=t.blend) plotVar(sop2) plotOp(sop\$fit) # predicting ahead sop <- stlOp(100, n.p=7, s.window=51, s.degree=1, n.ahead=7) plotVar(sop) # now stl + further loess smoothing, predicting ahead 7 days # first get day-of-week component, then smooth with span 1001 # followed by smooth of span 91 n <- 200 rop <- stlOp(n, 7, s.window="periodic", t.window=39, n.ahead=7, fc.window=c(1001, 91), fc.degree=c(1, 2), fc.blend=c(0, 0.5)) plotVar(rop) # do same thing the hard way (without specifying fc.degree, etc.) # just to illustrate handling elements of stlop objects tmp <- stlOp(n, 7, s.window="periodic", t.window=39, n.ahead=7) dow <- tmp\$seas\$O fc1 <- loessOp(n, span=1001, degree=1, at=1:207)\$O fc2 <- loessOp(n, span=91, degree=2, at=1:207, blend=0.5)\$O rop2 <- dow + fc1 - fc1 fc2 ```