forked from tidyverts/fabletools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvctrs-fable.R
120 lines (105 loc) · 3.25 KB
/
vctrs-fable.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#' Internal vctrs methods
#'
#' These methods are the extensions that allow fable objects to
#' work with vctrs.
#'
#' @keywords internal
#' @name fable-vctrs
NULL
#' @rdname fable-vctrs
#' @method vec_ptype2 fbl_ts
#' @export
vec_ptype2.fbl_ts <- function(x, y, ...) {
UseMethod("vec_ptype2.fbl_ts", y)
}
#' @export
vec_ptype2.fbl_ts.fbl_ts <- function(x, y, ...) {
fable_ptype2(x, y, ...)
}
#' @export
vec_ptype2.data.frame.fbl_ts <- function(x, y, ...) {
fable_ptype2(y, x, ...)
}
#' @export
vec_ptype2.fbl_ts.data.frame <- vec_ptype2.fbl_ts.fbl_ts
#' @export
vec_ptype2.tbl_df.fbl_ts <- vec_ptype2.data.frame.fbl_ts
#' @export
vec_ptype2.fbl_ts.tbl_df <- vec_ptype2.fbl_ts.fbl_ts
fable_ptype2 <- function(x, y, ...) {
idx_x <- index_var(x)
key_x <- key_vars(x)
dist_x <- distribution_var(x)
resp_x <- response_vars(x)
if (is_fable(y)) {
if (idx_x != index_var(y)) {
abort("No common index variable for `x` and `y`.")
}
if (dist_x != distribution_var(y)) {
abort("No common distribution variable for `x` and `y`.")
}
if (!identical(resp_x, response_vars(y))) {
abort("Objects with different response variables cannot be combined.")
}
key_x <- union(key_x, key_vars(y))
}
out <- df_ptype2(x, y, ...)
tsbl <- build_tsibble_meta(
out, key_data = tibble(!!!x[key_x], !!".rows" := list_of(.ptype = integer())),
index = idx_x, index2 = idx_x, ordered = TRUE,
interval = new_interval())
build_fable(tsbl, response = resp_x, distribution = dist_x)
}
#' @rdname fable-vctrs
#' @method vec_cast fbl_ts
#' @export
vec_cast.fbl_ts <- function(x, to, ...) {
UseMethod("vec_cast.fbl_ts")
}
#' @export
vec_cast.fbl_ts.fbl_ts <- function(x, to, ...) {
is_identical <- identical(x, to)
tbl <- tib_cast(x, to, ...)
tsbl <- build_tsibble(
tbl, key = key_vars(to),
key_data = if (is_identical) key_data(x) else NULL,
index = index_var(to), index2 = index2_var(to),
ordered = is_ordered(to),
validate = FALSE, .drop = key_drop_default(to))
build_fable(tsbl, response = response_vars(to), distribution = distribution_var(to))
}
#' @export
vec_cast.fbl_ts.tbl_df <- function(x, to, ...) {
tbl <- tib_cast(x, to, ...)
tsbl <- build_tsibble(
tbl, key = key_vars(to), index = index_var(to), index2 = index2_var(to),
ordered = TRUE, validate = TRUE, .drop = key_drop_default(to))
build_fable(tsbl, response = response_vars(to), distribution = distribution_var(to))
}
#' @export
vec_cast.tbl_ts.fbl_ts <- function(x, to, ...) {
tbl <- tib_cast(x, to, ...)
build_tsibble(
tbl, key = key_vars(to), index = index_var(to), index2 = index2_var(to),
ordered = TRUE, validate = TRUE, .drop = key_drop_default(to)
)
}
#' @export
vec_cast.fbl_ts.data.frame <- vec_cast.fbl_ts.tbl_df
#' @export
vec_cast.tbl_df.fbl_ts <- function(x, to, ...) {
tib_cast(x, to, ...)
}
#' @export
vec_cast.data.frame.fbl_ts <- function(x, to, ...) {
df_cast(x, to, ...)
}
#' @export
vec_restore.fbl_ts <- function(x, to, ..., n = NULL) {
if(!is_tsibble(x)){
x <- build_tsibble(
x, key = key_vars(to), index = index_var(to), index2 = index2_var(to),
ordered = TRUE, validate = TRUE, .drop = key_drop_default(to))
}
build_fable(x, response = response_vars(to), distribution = distribution_var(to))
}