Learn R Programming

broom (version 0.4.5)

decompose_tidiers: Tidying methods for seasonal decompositions

Description

These tidiers provide an augment method for the results of a seasonal decomposition with decompose or stl.

Usage

# S3 method for decomposed.ts
augment(x, ...)

# S3 method for stl augment(x, weights = TRUE, ...)

Arguments

x

An object of class "stl" or "decomposed.ts", resulting from a call to decompose or stl.

...

Extra arguments. Unused.

weights

Whether to include the robust weights in the output.

Value

The augment method returns a tidy data frame with the following columns:

.seasonal

The seasonal component of the decomposition.

.trend

The trend component of the decomposition.

.remainder

The remainder, or "random" component of the decomposition.

.weight

The final robust weights (stl only).

.seasadj

The seasonally adjusted (or "deseasonalised") series.

Details

The augment method returns the computed seasonal and trend components, as well as the "remainder" term and the seasonally adjusted (or "deseasonalised") series.

See Also

decompose, stl

Examples

Run this code
# NOT RUN {
# 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))

# }

Run the code above in your browser using DataLab