Debian Codegraph @

R/gmm.R

raw
1 #' @templateVar class gmm
2 #' @template title_desc_tidy
3 #'
4 #' @param x A `gmm` object returned from [gmm::gmm()].
5 #' @template param_confint
6 #' @template param_exponentiate
7 #' @template param_unused_dots
8 #'
9 #' @evalRd return_tidy(regression = TRUE)
10 #'
11 #' @examplesIf rlang::is_installed(c("gmm", "ggplot2"))
12 #'
13 #' # load libraries for models and data
14 #' library(gmm)
15 #'
16 #' # examples come from the "gmm" package
17 #' # CAPM test with GMM
18 #' data(Finance)
19 #' r <- Finance[1:300, 1:10]
20 #' rm <- Finance[1:300, "rm"]
21 #' rf <- Finance[1:300, "rf"]
22 #'
23 #' z <- as.matrix(r - rf)
24 #' t <- nrow(z)
25 #' zm <- rm - rf
26 #' h <- matrix(zm, t, 1)
27 #' res <- gmm(z ~ zm, x = h)
28 #'
29 #' # tidy result
30 #' tidy(res)
31 #' tidy(res, conf.int = TRUE)
32 #' tidy(res, conf.int = TRUE, conf.level = .99)
33 #'
34 #' # coefficient plot
35 #' library(ggplot2)
36 #' library(dplyr)
37 #'
38 #' tidy(res, conf.int = TRUE) |>
39 #' mutate(variable = reorder(term, estimate)) |>
40 #' ggplot(aes(estimate, variable)) +
41 #' geom_point() +
42 #' geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
43 #' geom_vline(xintercept = 0, color = "red", lty = 2)
44 #'
45 #' # from a function instead of a matrix
46 #' g <- function(theta, x) {
47 #' e <- x[, 2:11] - theta[1] - (x[, 1] - theta[1]) %*% matrix(theta[2:11], 1, 10)
48 #' gmat <- cbind(e, e * c(x[, 1]))
49 #' return(gmat)
50 #' }
51 #'
52 #' x <- as.matrix(cbind(rm, r))
53 #' res_black <- gmm(g, x = x, t0 = rep(0, 11))
54 #'
55 #' tidy(res_black)
56 #' tidy(res_black, conf.int = TRUE)
57 #'
58 #' # APT test with Fama-French factors and GMM
59 #'
60 #' f1 <- zm
61 #' f2 <- Finance[1:300, "hml"] - rf
62 #' f3 <- Finance[1:300, "smb"] - rf
63 #' h <- cbind(f1, f2, f3)
64 #' res2 <- gmm(z ~ f1 + f2 + f3, x = h)
65 #'
66 #' td2 <- tidy(res2, conf.int = TRUE)
67 #' td2
68 #'
69 #' # coefficient plot
70 #' td2 |>
71 #' mutate(variable = reorder(term, estimate)) |>
72 #' ggplot(aes(estimate, variable)) +
73 #' geom_point() +
74 #' geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
75 #' geom_vline(xintercept = 0, color = "red", lty = 2)
76 #'
77 #' @export
78 #' @aliases gmm_tidiers
79 #' @family gmm tidiers
80 #' @seealso [tidy()], [gmm::gmm()]
81 tidy.gmm <- function(
82 x,
83 conf.int = FALSE,
84 conf.level = .95,
85 exponentiate = FALSE,
86 ...
87 ) {
88 coef <- summary(x)$coefficients
89 ret <- as_tibble(coef, rownames = "term")
90 colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value")
91
92 if (conf.int) {
93 # non-standard confint return object, so can't use
94 # broom_confint_terms() like we'd hope
95
96 ci <- confint(x, level = conf.level)$test
97 ci <- as_tibble(ci, rownames = "term")
98 colnames(ci) <- c("term", "conf.low", "conf.high")
99 ret <- dplyr::left_join(ret, ci, by = "term")
100 }
101
102 if (exponentiate) {
103 ret <- exponentiate(ret)
104 }
105
106 ret
107 }
108
109 #' @templateVar class gmm
110 #' @template title_desc_glance
111 #'
112 #' @inherit tidy.gmm params examples
113 #'
114 #' @evalRd return_glance("df",
115 #' "statistic",
116 #' "p.value",
117 #' "df.residual",
118 #' "nobs")
119 #'
120 #' @export
121 #' @family gmm tidiers
122 #' @seealso [glance()], [gmm::gmm()]
123 glance.gmm <- function(x, ...) {
124 s <- summary(x)
125
126 # TODO: why do we suppress warnings here?
127 st <- suppressWarnings(as.numeric(s$stest$test))
128
129 as_glance_tibble(
130 df = x$df,
131 statistic = st[1],
132 p.value = st[2],
133 df.residual = stats::df.residual(x),
134 nobs = stats::nobs(x),
135 na_types = "irrii"
136 )
137 }