Debian Codegraph @

R/tseries.R

raw
1 #' @templateVar class garch
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `garch` object returned by [tseries::garch()].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(
9 #' "term",
10 #' "estimate",
11 #' "std.error",
12 #' "statistic",
13 #' "p.value",
14 #' "conf.low",
15 #' "conf.high"
16 #' )
17 #'
18 #' @examplesIf rlang::is_installed("tseries")
19 #'
20 #' # load libraries for models and data
21 #' library(tseries)
22 #'
23 #' # load data
24 #' data(EuStockMarkets)
25 #'
26 #' # fit model
27 #' dax <- diff(log(EuStockMarkets))[, "DAX"]
28 #' dax.garch <- garch(dax)
29 #' dax.garch
30 #'
31 #' # summarize model fit with tidiers
32 #' tidy(dax.garch)
33 #' glance(dax.garch)
34 #'
35 #' @aliases garch_tidiers
36 #' @export
37 #' @family garch tidiers
38 #' @seealso [tidy()], [tseries::garch()]
39 tidy.garch <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
40 check_ellipses("exponentiate", "tidy", "garch", ...)
41
42 s <- summary(x)
43 co <- s$coef
44 nn <- c("estimate", "std.error", "statistic", "p.value")
45 ret <- as_tidy_tibble(co, new_names = nn[1:ncol(co)])
46
47 if (conf.int) {
48 ci <- broom_confint_terms(x, level = conf.level)
49 ret <- dplyr::left_join(ret, ci, by = "term")
50 }
51
52 ret
53 }
54
55 #' @templateVar class garch
56 #' @template title_desc_tidy
57 #'
58 #' @param x A `garch` object returned by [tseries::garch()].
59 #' @param test Character specification of which hypothesis test to use. The
60 #' `garch` function reports 2 hypothesis tests: Jarque-Bera to residuals
61 #' and Box-Ljung to squared residuals.
62 #' @template param_unused_dots
63 #'
64 #' @evalRd return_glance(
65 #' "statistic",
66 #' "p.value",
67 #' "method",
68 #' "logLik",
69 #' "AIC",
70 #' "BIC",
71 #' "nobs",
72 #' parameter = "Parameter field in the htest, typically degrees of
73 #' freedom."
74 #' )
75 #'
76 #' @export
77 #' @family garch tidiers
78 #' @seealso [glance()], [tseries::garch()], []
79 glance.garch <- function(
80 x,
81 test = c("box-ljung-test", "jarque-bera-test"),
82 ...
83 ) {
84 test <- rlang::arg_match(test)
85 s <- summary(x)
86 ret <- garch_glance_helper(s, test, ...)
87 ret$logLik <- as.numeric(stats::logLik(x))
88 ret$AIC <- stats::AIC(x)
89 ret$BIC <- stats::BIC(x)
90 ret$nobs <- stats::nobs(x)
91 ret
92 }
93
94 garch_glance_helper <- function(x, test, ...) {
95 ret <- if (test == "box-ljung-test") {
96 glance.htest(x$l.b.test)
97 } else {
98 glance.htest(x$j.b.test)
99 }
100 as_tibble(ret)
101 }