\HeaderA{stl}{Seasonal Decomposition of Time Series by Loess}{stl}
\keyword{ts}{stl}
\begin{Description}\relax
Decompose a time series into seasonal, trend and irregular components
using \code{loess}, acronym STL.
\end{Description}
\begin{Usage}
\begin{verbatim}
stl(x, s.window, s.degree = 0,
    t.window = NULL, t.degree = 1,
    l.window = nextodd(period), l.degree = t.degree, 
    s.jump = ceiling(s.window/10),
    t.jump = ceiling(t.window/10),
    l.jump = ceiling(l.window/10),
    robust = FALSE,
    inner = if(robust)  1 else 2,
    outer = if(robust) 15 else 0,
    na.action = na.fail)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] univariate time series to be decomposed.
This should be an object of class \code{"ts"} with a frequency
greater than one.
\item[\code{s.window}] either the character string \code{"periodic"} or the span (in
lags) of the loess window for seasonal extraction, which should
be odd.  This has no default.
\item[\code{s.degree}] degree of locally-fitted polynomial in seasonal
extraction.  Should be zero or one.
\item[\code{t.window}] the span (in lags) of the loess window for trend
extraction, which should be odd.  If \code{NULL}, the default,
\code{nextodd(ceiling((1.5*period) / (1-(1.5/s.window))))}, is taken.
\item[\code{t.degree}] degree of locally-fitted polynomial in trend
extraction.  Should be zero or one.
\item[\code{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 \code{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.
\item[\code{l.degree}] degree of locally-fitted polynomial for the subseries
low-pass filter.  Must be 0 or 1.
\item[\code{s.jump, t.jump, l.jump}] integers at least one to increase speed of
the respective smoother.  Linear interpolation happens between every
\code{*.jump}th value.
\item[\code{robust}] logical indicating if robust fitting be used in the
\code{loess} procedure.
\item[\code{inner}] integer; the number of \sQuote{inner} (backfitting)
iterations; usually very few (2) iterations suffice.
\item[\code{outer}] integer; the number of \sQuote{outer} robustness
iterations.
\item[\code{na.action}] action on missing values.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The seasonal component is found by \emph{loess} smoothing the
seasonal sub-series (the series of all January values, \ldots); if
\code{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 \code{remainder} component is the
residuals from the seasonal plus trend fit.

Several methods for the resulting class \code{"stl"} objects, see,
\code{\LinkA{plot.stl}{plot.stl}}.
\end{Details}
\begin{Value}
\code{stl} returns an object of class \code{"stl"} with components
\begin{ldescription}
\item[\code{time.series}] a multiple time series with columns
\code{seasonal}, \code{trend} and \code{remainder}.
\item[\code{weights}] the final robust weights (all one if fitting is not
done robustly).
\item[\code{call}] the matched call.
\item[\code{win}] integer (length 3 vector) with the spans used for the \code{"s"},
\code{"t"}, and \code{"l"} smoothers.
\item[\code{deg}] integer (length 3) vector with the polynomial degrees for
these smoothers.
\item[\code{jump}] integer (length 3) vector with the \dQuote{jumps} (skips)
used for these smoothers.
\item[\code{ni}] number of \bold{i}nner iterations
\item[\code{no}] number of \bold{o}uter robustness iterations
\end{ldescription}
\end{Value}
\begin{Note}\relax
This is similar to but not identical to the \code{stl} function in
S-PLUS. The \code{remainder} component given by S-PLUS is the sum of
the \code{trend} and \code{remainder} series from this function.
\end{Note}
\begin{Author}\relax
B.D. Ripley; Fortran code by Cleveland \emph{et al.} (1990) from
\file{netlib}.
\end{Author}
\begin{References}\relax
R. B. Cleveland, W. S. Cleveland, J.E.  McRae, and I. Terpenning (1990)
STL:  A  Seasonal-Trend  Decomposition  Procedure Based on Loess.
\emph{Journal of Official Statistics}, \bold{6}, 3--73.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{plot.stl}{plot.stl}} for \code{stl} methods;
\code{\LinkA{loess}{loess}} in package \pkg{stats} (which is not actually
used in \code{stl}).
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
plot(stl(nottem, "per"))
plot(stl(nottem, s.win = 4, t.win = 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.w = "per") # un-robust
summary(stmR <- stl(mdeaths, s.w = "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  < 1e-8)) # 10 were considered outliers
sts <- stmR$time.series
points(time(sts)[iO], .8* sts[,"remainder"][iO], pch = 4, col = "red")
par(op)# reset    
\end{ExampleCode}
\end{Examples}

