| decompose_tidiers {broom} | R Documentation |
These tidiers provide an augment method for the results of a seasonal
decomposition with decompose or
stl.
## S3 method for class 'decomposed.ts' augment(x, ...) ## S3 method for class 'stl' augment(x, weights = TRUE, ...)
x |
An object of class |
... |
Extra arguments. Unused. |
weights |
Whether to include the robust weights in the output. |
The augment method returns the computed seasonal and trend components,
as well as the "remainder" term and the seasonally adjusted (or
"deseasonalised") series.
The augment method returns a tidy data frame with the following
columns:
.seasonalThe seasonal component of the decomposition.
.trendThe trend component of the decomposition.
.remainderThe remainder, or "random" component of the decomposition.
.weightThe final robust weights (stl only).
.seasadjThe seasonally adjusted (or "deseasonalised") series.
Aaron Jacobs
# Time series of temperatures in Nottingham, 1920-1939:
nottem
# Perform seasonal decomposition on the data with both decompose
# and stl:
d1 <- stats::decompose(nottem)
d2 <- stats::stl(nottem, s.window = "periodic", robust = TRUE)
# Compare the original series to its decompositions.
cbind(broom::tidy(nottem), broom::augment(d1),
broom::augment(d2))
# Visually compare seasonal decompositions in tidy data frames.
library(tibble)
library(dplyr)
library(tidyr)
library(ggplot2)
decomps <- tibble(
# Turn the ts objects into data frames.
series = list(broom::tidy(nottem), broom::tidy(nottem)),
# Add the models in, one for each row.
decomp = c("decompose", "stl"),
model = list(d1, d2)
) %>%
rowwise() %>%
# Pull out the fitted data using broom::augment.
mutate(augment = list(broom::augment(model))) %>%
ungroup() %>%
# Unnest the data frames into a tidy arrangement of
# the series next to its seasonal decomposition, grouped
# by the method (stl or decompose).
group_by(decomp) %>%
unnest(series, augment) %>%
mutate(index = 1:n()) %>%
ungroup() %>%
select(decomp, index, x, adjusted = .seasadj)
ggplot(decomps) +
geom_line(aes(x = index, y = x), colour = "black") +
geom_line(aes(x = index, y = adjusted, colour = decomp,
group = decomp))