Debian Codegraph @

R/stats-decompose.R

raw
1 #' @templateVar class decomposed.ts
2 #' @template title_desc_augment
3 #'
4 #' @param x A `decomposed.ts` object returned from [stats::decompose()].
5 #' @template param_unused_dots
6 #'
7 #' @return A [tibble::tibble] with one row for each observation in the
8 #' original times series:
9 #'
10 #' \item{`.seasonal`}{The seasonal component of the decomposition.}
11 #' \item{`.trend`}{The trend component of the decomposition.}
12 #' \item{`.remainder`}{The remainder, or "random" component of the
13 #' decomposition.}
14 #' \item{`.weight`}{The final robust weights (`stl` only).}
15 #' \item{`.seasadj`}{The seasonally adjusted (or "deseasonalised")
16 #' series.}
17 #'
18 #' @examplesIf rlang::is_installed("ggplot2")
19 #'
20 #' # time series of temperatures in Nottingham, 1920-1939:
21 #' nottem
22 #'
23 #' # perform seasonal decomposition on the data with both decompose
24 #' # and stl:
25 #' d1 <- decompose(nottem)
26 #' d2 <- stl(nottem, s.window = "periodic", robust = TRUE)
27 #'
28 #' # compare the original series to its decompositions.
29 #'
30 #' cbind(
31 #' tidy(nottem), augment(d1),
32 #' augment(d2)
33 #' )
34 #'
35 #' # visually compare seasonal decompositions in tidy data frames.
36 #'
37 #' library(tibble)
38 #' library(dplyr)
39 #' library(tidyr)
40 #' library(ggplot2)
41 #'
42 #' decomps <- tibble(
43 #' # turn the ts objects into data frames.
44 #' series = list(as.data.frame(nottem), as.data.frame(nottem)),
45 #' # add the models in, one for each row.
46 #' decomp = c("decompose", "stl"),
47 #' model = list(d1, d2)
48 #' ) |>
49 #' rowwise() |>
50 #' # pull out the fitted data using broom::augment.
51 #' mutate(augment = list(broom::augment(model))) |>
52 #' ungroup() |>
53 #' # unnest the data frames into a tidy arrangement of
54 #' # the series next to its seasonal decomposition, grouped
55 #' # by the method (stl or decompose).
56 #' group_by(decomp) |>
57 #' unnest(c(series, augment)) |>
58 #' mutate(index = 1:n()) |>
59 #' ungroup() |>
60 #' select(decomp, index, x, adjusted = .seasadj)
61 #'
62 #' ggplot(decomps) +
63 #' geom_line(aes(x = index, y = x), colour = "black") +
64 #' geom_line(aes(
65 #' x = index, y = adjusted, colour = decomp,
66 #' group = decomp
67 #' ))
68 #'
69 #' @aliases decompose_tidiers
70 #' @export
71 #' @family decompose tidiers
72 #' @seealso [augment()], [stats::decompose()]
73 augment.decomposed.ts <- function(x, ...) {
74 ret <- tibble(
75 seasonal = as.numeric(x$seasonal),
76 trend = as.numeric(x$trend),
77 remainder = as.numeric(x$random)
78 )
79 # Inspired by forecast::seasadj, this is the "deseasonalised" data:
80 ret$seasadj <- if (x$type == "additive") {
81 as.numeric(x$x) - ret$seasonal
82 } else {
83 as.numeric(x$x) / ret$seasonal
84 }
85 colnames(ret) <- paste0(".", colnames(ret))
86 as_tibble(ret)
87 }
88
89 #' @templateVar class stl
90 #' @template title_desc_augment
91 #'
92 #' @param x An `stl` object returned from [stats::stl()].
93 #' @param data Ignored, included for consistency with the augment generic signature only.
94 #' @param weights Logical indicating whether or not to include the robust
95 #' weights in the output.
96 #' @template param_unused_dots
97 #'
98 #' @return A [tibble::tibble] with one row for each observation in the
99 #' original times series:
100 #'
101 #' \item{`.seasonal`}{The seasonal component of the decomposition.}
102 #' \item{`.trend`}{The trend component of the decomposition.}
103 #' \item{`.remainder`}{The remainder, or "random" component of the
104 #' decomposition.}
105 #' \item{`.weight`}{The final robust weights, if requested.}
106 #' \item{`.seasadj`}{The seasonally adjusted (or "deseasonalised")
107 #' series.}
108 #'
109 #' @export
110 #' @family decompose tidiers
111 #' @seealso [augment()], [stats::stl()]
112 augment.stl <- function(x, data = NULL, weights = TRUE, ...) {
113 check_ellipses("newdata", "augment", "stl", ...)
114
115 ret <- as_tibble(x$time.series)
116 ret$weight <- x$weights
117 # Inspired by forecast::seasadj, this is the "deseasonalised" data:
118 ret$seasadj <- ret$trend + ret$remainder
119 colnames(ret) <- paste0(".", colnames(ret))
120 ret
121 }