| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/stats-decompose.R |
| 3 |
\name{augment.decomposed.ts} |
| 4 |
\alias{augment.decomposed.ts} |
| 5 |
\alias{decompose_tidiers} |
| 6 |
\title{Augment data with information from a(n) decomposed.ts object} |
| 7 |
\usage{ |
| 8 |
\method{augment}{decomposed.ts}(x, ...) |
| 9 |
} |
| 10 |
\arguments{ |
| 11 |
\item{x}{A \code{decomposed.ts} object returned from \code{\link[stats:decompose]{stats::decompose()}}.} |
| 12 |
|
| 13 |
\item{...}{Additional arguments. Not used. Needed to match generic |
| 14 |
signature only. \strong{Cautionary note:} Misspelled arguments will be |
| 15 |
absorbed in \code{...}, where they will be ignored. If the misspelled |
| 16 |
argument has a default value, the default value will be used. |
| 17 |
For example, if you pass \code{conf.lvel = 0.9}, all computation will |
| 18 |
proceed using \code{conf.level = 0.95}. Two exceptions here are: |
| 19 |
\itemize{ |
| 20 |
\item \code{tidy()} methods will warn when supplied an \code{exponentiate} argument if |
| 21 |
it will be ignored. |
| 22 |
\item \code{augment()} methods will warn when supplied a \code{newdata} argument if it |
| 23 |
will be ignored. |
| 24 |
}} |
| 25 |
} |
| 26 |
\value{ |
| 27 |
A \link[tibble:tibble]{tibble::tibble} with one row for each observation in the |
| 28 |
original times series: |
| 29 |
|
| 30 |
\item{\code{.seasonal}}{The seasonal component of the decomposition.} |
| 31 |
\item{\code{.trend}}{The trend component of the decomposition.} |
| 32 |
\item{\code{.remainder}}{The remainder, or "random" component of the |
| 33 |
decomposition.} |
| 34 |
\item{\code{.weight}}{The final robust weights (\code{stl} only).} |
| 35 |
\item{\code{.seasadj}}{The seasonally adjusted (or "deseasonalised") |
| 36 |
series.} |
| 37 |
} |
| 38 |
\description{ |
| 39 |
Augment accepts a model object and a dataset and adds |
| 40 |
information about each observation in the dataset. Most commonly, this |
| 41 |
includes predicted values in the \code{.fitted} column, residuals in the |
| 42 |
\code{.resid} column, and standard errors for the fitted values in a \code{.se.fit} |
| 43 |
column. New columns always begin with a \code{.} prefix to avoid overwriting |
| 44 |
columns in the original dataset. |
| 45 |
|
| 46 |
Users may pass data to augment via either the \code{data} argument or the |
| 47 |
\code{newdata} argument. If the user passes data to the \code{data} argument, |
| 48 |
it \strong{must} be exactly the data that was used to fit the model |
| 49 |
object. Pass datasets to \code{newdata} to augment data that was not used |
| 50 |
during model fitting. This still requires that at least all predictor |
| 51 |
variable columns used to fit the model are present. If the original outcome |
| 52 |
variable used to fit the model is not included in \code{newdata}, then no |
| 53 |
\code{.resid} column will be included in the output. |
| 54 |
|
| 55 |
Augment will often behave differently depending on whether \code{data} or |
| 56 |
\code{newdata} is given. This is because there is often information |
| 57 |
associated with training observations (such as influences or related) |
| 58 |
measures that is not meaningfully defined for new observations. |
| 59 |
|
| 60 |
For convenience, many augment methods provide default \code{data} arguments, |
| 61 |
so that \code{augment(fit)} will return the augmented training data. In these |
| 62 |
cases, augment tries to reconstruct the original data based on the model |
| 63 |
object with varying degrees of success. |
| 64 |
|
| 65 |
The augmented dataset is always returned as a \link[tibble:tibble]{tibble::tibble} with the |
| 66 |
\strong{same number of rows} as the passed dataset. This means that the passed |
| 67 |
data must be coercible to a tibble. If a predictor enters the model as part |
| 68 |
of a matrix of covariates, such as when the model formula uses |
| 69 |
\code{\link[splines:ns]{splines::ns()}}, \code{\link[stats:poly]{stats::poly()}}, or \code{\link[survival:Surv]{survival::Surv()}}, it is represented |
| 70 |
as a matrix column. |
| 71 |
|
| 72 |
We are in the process of defining behaviors for models fit with various |
| 73 |
\code{na.action} arguments, but make no guarantees about behavior when data is |
| 74 |
missing at this time. |
| 75 |
} |
| 76 |
\examples{ |
| 77 |
\dontshow{if (rlang::is_installed("ggplot2")) withAutoprint(\{ # examplesIf} |
| 78 |
|
| 79 |
# time series of temperatures in Nottingham, 1920-1939: |
| 80 |
nottem |
| 81 |
|
| 82 |
# perform seasonal decomposition on the data with both decompose |
| 83 |
# and stl: |
| 84 |
d1 <- decompose(nottem) |
| 85 |
d2 <- stl(nottem, s.window = "periodic", robust = TRUE) |
| 86 |
|
| 87 |
# compare the original series to its decompositions. |
| 88 |
|
| 89 |
cbind( |
| 90 |
tidy(nottem), augment(d1), |
| 91 |
augment(d2) |
| 92 |
) |
| 93 |
|
| 94 |
# visually compare seasonal decompositions in tidy data frames. |
| 95 |
|
| 96 |
library(tibble) |
| 97 |
library(dplyr) |
| 98 |
library(tidyr) |
| 99 |
library(ggplot2) |
| 100 |
|
| 101 |
decomps <- tibble( |
| 102 |
# turn the ts objects into data frames. |
| 103 |
series = list(as.data.frame(nottem), as.data.frame(nottem)), |
| 104 |
# add the models in, one for each row. |
| 105 |
decomp = c("decompose", "stl"), |
| 106 |
model = list(d1, d2) |
| 107 |
) |> |
| 108 |
rowwise() |> |
| 109 |
# pull out the fitted data using broom::augment. |
| 110 |
mutate(augment = list(broom::augment(model))) |> |
| 111 |
ungroup() |> |
| 112 |
# unnest the data frames into a tidy arrangement of |
| 113 |
# the series next to its seasonal decomposition, grouped |
| 114 |
# by the method (stl or decompose). |
| 115 |
group_by(decomp) |> |
| 116 |
unnest(c(series, augment)) |> |
| 117 |
mutate(index = 1:n()) |> |
| 118 |
ungroup() |> |
| 119 |
select(decomp, index, x, adjusted = .seasadj) |
| 120 |
|
| 121 |
ggplot(decomps) + |
| 122 |
geom_line(aes(x = index, y = x), colour = "black") + |
| 123 |
geom_line(aes( |
| 124 |
x = index, y = adjusted, colour = decomp, |
| 125 |
group = decomp |
| 126 |
)) |
| 127 |
\dontshow{\}) # examplesIf} |
| 128 |
} |
| 129 |
\seealso{ |
| 130 |
\code{\link[=augment]{augment()}}, \code{\link[stats:decompose]{stats::decompose()}} |
| 131 |
|
| 132 |
Other decompose tidiers: |
| 133 |
\code{\link{augment.stl}()} |
| 134 |
} |
| 135 |
\concept{decompose tidiers} |