Debian Codegraph @

R/systemfit.R

raw
1 #' @templateVar class systemfit
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `systemfit` object produced by a call to [systemfit::systemfit()].
5 #' @template param_confint
6 #' @template param_unused_dots
7 #'
8 #' @evalRd return_tidy(
9 #' "term",
10 #' "estimate",
11 #' "std.error",
12 #' "p.value",
13 #' "conf.low",
14 #' "conf.high"
15 #' )
16 #'
17 #' @details This tidy method works with any model objects of class `systemfit`.
18 #' Default returns a tibble of six columns.
19 #'
20 #' @examplesIf rlang::is_installed("systemfit")
21 #'
22 #' set.seed(27)
23 #'
24 #' # load libraries for models and data
25 #' library(systemfit)
26 #'
27 #' # generate data
28 #' df <- data.frame(
29 #' X = rnorm(100),
30 #' Y = rnorm(100),
31 #' Z = rnorm(100),
32 #' W = rnorm(100)
33 #' )
34 #'
35 #' # fit model
36 #' fit <- systemfit(formula = list(Y ~ Z, W ~ X), data = df, method = "SUR")
37 #'
38 #' # summarize model fit with tidiers
39 #' tidy(fit)
40 #' tidy(fit, conf.int = TRUE)
41 #'
42 #' @export
43 #' @seealso [tidy()], [systemfit::systemfit()]
44 #'
45 #' @family systemfit tidiers
46 #' @aliases systemfit_tidiers
47 #'
48 tidy.systemfit <- function(x, conf.int = TRUE, conf.level = 0.95, ...) {
49 check_ellipses("exponentiate", "tidy", "systemfit", ...)
50
51 ret <- as_tibble(summary(x)$coefficients, rownames = "term")
52 colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value")
53
54 if (conf.int) {
55 # can't use broom_confint_terms since the resulting confidence
56 # intervals are of type `confint.systemfit` not `matrix`
57
58 ci <- confint(x, level = conf.level)
59 colnames(ci) <- c("conf.low", "conf.high")
60 ci <- as_tibble(unclass(ci), rownames = "term")
61 ret <- left_join(ret, ci, by = "term")
62 }
63
64 ret
65 }