# time series of temperatures in Nottingham, 1920-1939:
nottem
# perform seasonal decomposition on the data with both decompose
# and stl:
d1 <- decompose(nottem)
d2 <- stl(nottem, s.window = "periodic", robust = TRUE)
# compare the original series to its decompositions.
cbind(
tidy(nottem), augment(d1),
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(as.data.frame(nottem), as.data.frame(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(c(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
))
Run the code above in your browser using DataLab