Description Usage Arguments Details Value Note Author(s) References See Also Examples
Decompose a time series into seasonal, trend and irregular components using loess
, acronym STL. A new implementation of STL. Allows for NA values, local quadratic smoothing, post-trend smoothing, and endpoint blending. The usage is very similar to that of R's built-in stl()
.
1 2 3 4 5 6 7 8 9 10 11 | stl2(x, t = NULL, n.p, s.window, s.degree = 1,
t.window = NULL, t.degree = 1,
fc.window = NULL, fc.degree = NULL, fc.name = NULL,
l.window = NULL, l.degree = t.degree,
s.jump=ceiling(s.window/10), t.jump=ceiling(t.window/10),
l.jump=ceiling(l.window/10), fc.jump = NULL,
critfreq = 0.05, s.blend = 0, t.blend = 0,
l.blend = t.blend, fc.blend=NULL,
inner = 2, outer = 1,
sub.labels = NULL, sub.start=1, details = FALSE,
...)
|
x |
vector of time series values, in order of time. If |
t |
times at which the time series values were observed. Not required. |
n.p |
periodicity of the seasonal component. In R's |
s.window |
either the character string |
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 |
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 |
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. |
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 |
inner |
integer; the number of ‘inner’ (backfitting) iterations; usually very few (2) iterations suffice. |
outer |
integer; the number of ‘outer’ robustness iterations. Default is 0, but Recommended if outliers are present. |
sub.labels |
optional vector of length n.p that contains the labels of the subseries in their natural order (such as month name, day of week, etc.), used for strip labels when plotting. All entries must be unique. |
sub.start |
which element of sub.labels does the series begin with. See details. |
details |
if |
s.jump, t.jump, l.jump, fc.jump |
integers at least one to increase speed of the respective smoother. Linear interpolation happens between every |
s.blend, t.blend, l.blend, fc.blend |
vectors of proportion of blending to degree 0 polynomials at the endpoints of the series. |
... |
The seasonal component is found by loess smoothing the
seasonal sub-series (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.
Cycle-subseries labels are useful for plotting and can be specified through the sub.labels argument. Here is an example for how the sub.labels and sub.start parameters might be set for one situation. Suppose we have a daily series with n.p=7 (fitting a day-of-week component). Here, sub.labels could be set to c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"). Now, if the series starts with a Wednesday value, then one would specify sub.labels=4, since Wednesday is the fourth element of sub.labels. This ensures that the labels in the plots to start the plotting with Sunday cycle-subseries instead of Wednesday.
returns an object of class "stl2"
, containing
data |
data frame containing all of the components: |
pars |
list of parameters used in the procedure. |
fc.number |
number of post-trend frequency components fitted. |
fc |
data frame of the post-trend frequency components. |
time |
vector of time values corresponding to the raw values, if specified. |
n |
the number of observations. |
sub.labels |
the cycle-subseries labels. |
This is a complete re-implementation of the STL algorithm, with the loess part in C and the rest in R. Moving a lot of the code to R makes it easier to experiment with the method at a very minimal speed cost. Recoding in C instead of using R's built-in loess results in better performance, especially for larger series.
Ryan Hafen
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.
plot.stl2
for plotting the components.
getraw
, seasonal
, trend
, remainder
for accessing the components.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | co2.stl <- stl2(co2, t=as.vector(time(co2)), n.p=12,
l.window=13, t.window=19, s.window=35, s.degree=1,
sub.labels=substr(month.name, 1, 3))
plot(co2.stl, ylab="CO2 Concentration (ppm)", xlab="Time (years)")
plot.seasonal(co2.stl)
plot.trend(co2.stl)
plot.cycle(co2.stl)
plot.rembycycle(co2.stl)
# post-trend smoothing
co2.stl_pt <- stl2(co2, t=as.vector(time(co2)), n.p=12,
l.window=13, t.window=19, s.window=35, s.degree=1,
sub.labels=substr(month.name, 1, 3),
fc.degree=c(1, 2), fc.window=c(201, 35),
fc.name=c("long-term", "so. osc."))
plot(co2.stl_pt, scales=list(y=list(relation="free")),
ylab="CO2 Concentration (ppm)", xlab="Time (years)",
aspect=0.25, type=c("l", "g"))
# with NAs
y <- co2
y[201:224] <- NA
y.stl <- stl2(y, l.window=13, t.window=19, s.window=35,
s.degree=1, sub.labels=substr(month.name, 1, 3))
plot(y.stl, ylab="CO2 Concentration (ppm)", xlab="Time (years)")
plot.seasonal(y.stl)
plot.trend(y.stl)
plot.cycle(y.stl)
plot.rembycycle(y.stl)
# if you don't want to use a time series object:
y.stl <- stl2(y, t=as.vector(time(y)), n.p=12,
l.window=13, t.window=19, s.window=35, s.degree=1,
sub.labels=substr(month.name, 1, 3))
# with an outlier
y2 <- co2
y2[200] <- 300
y2.stl <- stl2(y2, t=as.vector(time(y2)), n.p=12,
l.window=13, t.window=19, s.window=35, s.degree=1,
sub.labels=substr(month.name, 1, 3), outer=10)
plot(y2.stl, ylab="CO2 Concentration (ppm)", xlab="Time (years)")
plot.seasonal(y2.stl)
plot.trend(y2.stl)
plot.cycle(y2.stl)
plot.rembycycle(y2.stl)
# compare to R's stl
x1 <- stl2(co2, t=as.vector(time(co2)), n.p=12,
l.window=13, t.window=19, s.window=11, s.degree=1,
sub.labels=substr(month.name, 1, 3))
x2 <- stl(co2, l.window=13, t.window=19, s.window=11, s.degree=1)
# will be different due to interpolation differences
plot(seasonal(x1) - seasonal(x2))
# but not if all jump parameters are 1
x1 <- stl2(co2, t=as.vector(time(co2)), n.p=12,
l.window=13, t.window=19, s.window=11, s.degree=1,
sub.labels=substr(month.name, 1, 3),
s.jump=1, t.jump=1, l.jump=1)
x2 <- stl(co2, l.window=13, t.window=19, s.window=11, s.degree=1,
s.jump=1, t.jump=1, l.jump=1)
plot(seasonal(x1) - seasonal(x2))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.