Debian Codegraph @

R/stats-arima.R

raw
1 #' @templateVar class Arima
2 #' @template title_desc_tidy
3 #'
4 #' @param x An object of class `Arima` created by [stats::arima()].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(
9 #' "term",
10 #' "estimate",
11 #' "std.error",
12 #' "conf.low",
13 #' "conf.high"
14 #' )
15 #'
16 #' @examples
17 #'
18 #' # fit model
19 #' fit <- arima(lh, order = c(1, 0, 0))
20 #'
21 #' # summarize model fit with tidiers
22 #' tidy(fit)
23 #' glance(fit)
24 #'
25 #' @aliases Arima_tidiers
26 #' @seealso [stats::arima()]
27 #' @export
28 #' @family Arima tidiers
29 tidy.Arima <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
30 check_ellipses("exponentiate", "tidy", "Arima", ...)
31
32 coefs <- stats::coef(x)
33 # standard errors are computed as in stats:::print.Arima
34 ses <- rep.int(0, length(coefs))
35 ses[x$mask] <- sqrt(diag(x$var.coef))
36
37 ret <- unrowname(data.frame(
38 term = names(coefs),
39 estimate = coefs,
40 std.error = ses
41 ))
42
43 if (conf.int) {
44 ci <- broom_confint_terms(x, level = conf.level)
45 ret <- dplyr::left_join(ret, ci, by = "term")
46 }
47
48 as_tibble(ret)
49 }
50
51
52 #' @templateVar class Arima
53 #' @template title_desc_glance
54 #'
55 #' @inherit tidy.Arima params examples
56 #'
57 #' @evalRd return_glance("sigma", "logLik", "AIC", "BIC", "nobs")
58 #'
59 #' @seealso [stats::arima()]
60 #' @export
61 #' @family Arima tidiers
62 glance.Arima <- function(x, ...) {
63 ret <- tibble(sigma = sqrt(x$sigma2))
64 ret$logLik <- tryCatch(as.numeric(stats::logLik(x)), error = function(e) NULL)
65 # special case for class Arima when method = "CSS"
66 if (!is.na(ret$logLik)) {
67 ret$AIC <- tryCatch(stats::AIC(x), error = function(e) NULL)
68 ret$BIC <- tryCatch(stats::BIC(x), error = function(e) NULL)
69 }
70 ret$nobs <- stats::nobs(x)
71 as_tibble(ret)
72 }