/
stats-time-series-tidiers.R
88 lines (85 loc) · 2.22 KB
/
stats-time-series-tidiers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#' @templateVar class ts
#' @template title_desc_tidy
#'
#' @param x A univariate or multivariate `ts` times series object.
#' @template param_unused_dots
#'
#' @evalRd return_tidy("index", "series", "value")
#'
#' @details `series` column is only present for multivariate `ts` objects.
#'
#' @examples
#'
#' set.seed(678)
#'
#' tidy(ts(1:10, frequency = 4, start = c(1959, 2)))
#'
#' z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12)
#' colnames(z) <- c("Aa", "Bb", "Cc")
#'
#' tidy(z)
#' @export
#' @seealso [tidy()], [stats::ts()]
#' @family time series tidiers
#'
tidy.ts <- function(x, ...) {
# This generates the "index" column using the same approach as time(x), but
# without converting to a ts object.
xtsp <- stats::tsp(x)
index <- seq(xtsp[1], xtsp[2], by = 1 / xtsp[3])
# Turn multi-column time series into tidy data frames.
if (is.matrix(x)) {
res <- as_tibble(as.data.frame(x))
res <- tibble::add_column(res, index = index, .before = 1)
res %>%
pivot_longer(
cols = c(dplyr::everything(), -index),
names_to = "series",
values_to = "value"
)
} else {
tibble(index = index, value = as.vector(x))
}
}
#' @templateVar class acf
#' @template title_desc_tidy
#'
#' @param x An `acf` object created by [stats::acf()], [stats::pacf()] or
#' [stats::ccf()].
#' @template param_unused_dots
#'
#' @evalRd return_tidy("lag", "acf")
#'
#' @examples
#'
#' tidy(acf(lh, plot = FALSE))
#' tidy(ccf(mdeaths, fdeaths, plot = FALSE))
#' tidy(pacf(lh, plot = FALSE))
#' @export
#' @seealso [tidy()], [stats::acf()], [stats::pacf()], [stats::ccf()]
#' @family time series tidiers
tidy.acf <- function(x, ...) {
tibble(lag = as.numeric(x$lag), acf = as.numeric(x$acf))
}
#' @templateVar class spec
#' @template title_desc_tidy
#'
#' @param x A `spec` object created by [stats::spectrum()].
#' @template param_unused_dots
#'
#' @evalRd return_tidy("freq", "spec")
#'
#' @examplesIf rlang::is_installed("ggplot2")
#'
#' spc <- spectrum(lh)
#' tidy(spc)
#'
#' library(ggplot2)
#' ggplot(tidy(spc), aes(freq, spec)) +
#' geom_line()
#' @export
#' @seealso [tidy()], [stats::spectrum()]
#' @family time series tidiers
tidy.spec <- function(x, ...) {
as_tibble(x[c("freq", "spec")])
}