forked from tidyverts/fabletools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.R
41 lines (40 loc) · 1.48 KB
/
components.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
#' Extract components from a fitted model
#'
#' Allows you to extract elements of interest from the model which can be
#' useful in understanding how they contribute towards the overall fitted values.
#'
#' A dable will be returned, which will allow you to easily plot the components
#' and see the way in which components are combined to give forecasts.
#'
#' @param object A mable.
#' @param ... Other arguments passed to methods.
#'
#' @examplesIf requireNamespace("fable", quietly = TRUE)
#' library(fable)
#' library(tsibbledata)
#'
#' # Forecasting with an ETS(M,Ad,A) model to Australian beer production
#' aus_production %>%
#' model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>%
#' components() %>%
#' autoplot()
#'
#' @rdname components
#' @export
components.mdl_df <- function(object, ...){
object <- tidyr::pivot_longer(object, all_of(mable_vars(object)),
names_to = ".model", values_to = ".fit")
kv <- key_vars(object)
object <- transmute(as_tibble(object),
!!!syms(kv), !!sym(".model"),
cmp = map(!!sym(".fit"), components))
attrs <- combine_dcmp_attr(object[["cmp"]])
object <- unnest_tsbl(object, "cmp", parent_key = kv)
as_dable(object, method = attrs[["method"]], resp = !!attrs[["response"]],
seasons = attrs[["seasons"]], aliases = attrs[["aliases"]])
}
#' @rdname components
#' @export
components.mdl_ts <- function(object, ...){
components(object$fit, ...)
}