Decompose a time series into seasonal, trend and irregular components
using loess
, acronym STL.
1 2 3 4 5 6 7 8 9 10 
x 
univariate time series to be decomposed.
This should be an object of class 
s.window 
either the character string 
s.degree 
degree of locallyfitted polynomial in seasonal extraction. Should be zero or one. 
t.window 
the span (in lags) of the loess window for trend
extraction, which should be odd. If 
t.degree 
degree of locallyfitted polynomial in trend extraction. Should be zero or one. 
l.window 
the span (in lags) of the loess window of the lowpass
filter used for each subseries. Defaults to the smallest odd
integer greater than or equal to 
l.degree 
degree of locallyfitted polynomial for the subseries lowpass filter. Must be 0 or 1. 
s.jump, t.jump, l.jump 
integers at least one to increase speed of
the respective smoother. Linear interpolation happens between every

robust 
logical indicating if robust fitting be used in the

inner 
integer; the number of ‘inner’ (backfitting) iterations; usually very few (2) iterations suffice. 
outer 
integer; the number of ‘outer’ robustness iterations. 
na.action 
action on missing values. 
The seasonal component is found by loess smoothing the
seasonal subseries (the series of all January values, ...); if
s.window = "periodic"
smoothing is effectively replaced by
taking the mean. The seasonal values are removed, and the remainder
smoothed to find the trend. The overall level is removed from the
seasonal component and added to the trend component. This process is
iterated a few times. The remainder
component is the
residuals from the seasonal plus trend fit.
Several methods for the resulting class "stl"
objects, see,
plot.stl
.
stl
returns an object of class "stl"
with components
time.series 
a multiple time series with columns

weights 
the final robust weights (all one if fitting is not done robustly). 
call 
the matched call. 
win 
integer (length 3 vector) with the spans used for the 
deg 
integer (length 3) vector with the polynomial degrees for these smoothers. 
jump 
integer (length 3) vector with the ‘jumps’ (skips) used for these smoothers. 
ni 
number of inner iterations 
no 
number of outer robustness iterations 
This is similar to but not identical to the stl
function in
SPLUS. The remainder
component given by SPLUS is the sum of
the trend
and remainder
series from this function.
B.D. Ripley; Fortran code by Cleveland et al (1990) from ‘netlib’.
R. B. Cleveland, W. S. Cleveland, J.E. McRae, and I. Terpenning (1990) STL: A SeasonalTrend Decomposition Procedure Based on Loess. Journal of Official Statistics, 6, 3–73.
plot.stl
for stl
methods;
loess
in package stats (which is not actually
used in stl
).
StructTS
for different kind of decomposition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22  require(graphics)
plot(stl(nottem, "per"))
plot(stl(nottem, s.window = 7, t.window = 50, t.jump = 1))
plot(stllc < stl(log(co2), s.window = 21))
summary(stllc)
## linear trend, strict period.
plot(stl(log(co2), s.window = "per", t.window = 1000))
## Two STL plotted side by side :
stmd < stl(mdeaths, s.window = "per") # nonrobust
summary(stmR < stl(mdeaths, s.window = "per", robust = TRUE))
op < par(mar = c(0, 4, 0, 3), oma = c(5, 0, 4, 0), mfcol = c(4, 2))
plot(stmd, set.pars = NULL, labels = NULL,
main = "stl(mdeaths, s.w = \"per\", robust = FALSE / TRUE )")
plot(stmR, set.pars = NULL)
# mark the 'outliers' :
(iO < which(stmR $ weights < 1e8)) # 10 were considered outliers
sts < stmR$time.series
points(time(sts)[iO], 0.8* sts[,"remainder"][iO], pch = 4, col = "red")
par(op) # reset

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.