Debian Codegraph @

R/vars.R

raw
1 #' @templateVar class varest
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `varest` object produced by a call to [vars::VAR()].
5 #' @template param_confint
6 #' @param ... For `glance()`, additional arguments passed to [summary()].
7 #' Otherwise ignored.
8 #'
9 #' @evalRd return_tidy(regression = TRUE,
10 #' component = "Whether a particular term was used to model the mean or the
11 #' precision in the regression. See details."
12 #' )
13 #'
14 #' @details The tibble has one row for each term in the regression. The
15 #' `component` column indicates whether a particular
16 #' term was used to model either the `"mean"` or `"precision"`. Here the
17 #' precision is the inverse of the variance, often referred to as `phi`.
18 #' At least one term will have been used to model the precision `phi`.
19 #'
20 #' The `vars` package does not include a `confint` method and does not report
21 #' confidence intervals for `varest` objects. Setting the `tidy` argument
22 #' `conf.int = TRUE` will return a warning.
23 #'
24 #' @examplesIf rlang::is_installed("vars")
25 #'
26 #' # load libraries for models and data
27 #' library(vars)
28 #'
29 #' # load data
30 #' data("Canada", package = "vars")
31 #'
32 #' # fit models
33 #' mod <- VAR(Canada, p = 1, type = "both")
34 #'
35 #' # summarize model fit with tidiers
36 #' tidy(mod)
37 #' glance(mod)
38 #'
39 #' @export
40 #' @seealso [tidy()], [vars::VAR()]
41 #' @family vars tidiers
42 #' @aliases vars_tidiers
43 tidy.varest <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
44 check_ellipses("exponentiate", "tidy", "varest", ...)
45
46 # `vars` does not define a `confint` method and does not calculate CIs
47 if (isTRUE(conf.int)) {
48 cli::cli_warn(c(
49 "Confidence intervals are not supported for {.cls varest} objects.",
50 "i" = "The {.arg conf.level} argument will be ignored."
51 ))
52 }
53
54 s <- summary(x)
55
56 ret <- list()
57
58 for (v in names(s$varresult)) {
59 ret[[v]] <- as_tidy_tibble(
60 s$varresult[[v]]$coefficients,
61 new_names = c("estimate", "std.error", "statistic", "p.value")
62 )
63 ret[[v]]$group <- v
64 }
65
66 ret <- dplyr::bind_rows(ret) |>
67 dplyr::relocate(group, 1)
68
69 ret
70 }
71
72
73 #' @templateVar class varest
74 #' @template title_desc_glance
75 #'
76 #' @inherit tidy.varest params examples
77 #' @template param_unused_dots
78 #'
79 #' @evalRd return_glance(
80 #' "lag.order",
81 #' "logLik",
82 #' "n",
83 #' "nobs"
84 #' )
85 #'
86 #' @seealso [glance()], [vars::VAR()]
87 #' @export
88 glance.varest <- function(x, ...) {
89 s <- summary(x, ...)
90 as_glance_tibble(
91 lag.order = x$p,
92 logLik = s$logLik,
93 nobs = x$obs,
94 n = x$totobs,
95 na_types = "riii"
96 )
97 }