Debian Codegraph @

R/aer.R

raw
1 #' @templateVar class ivreg
2 #' @template title_desc_tidy
3 #'
4 #' @param x An `ivreg` object created by a call to [AER::ivreg()].
5 #' @template param_confint
6 #' @param instruments Logical indicating whether to return
7 #' coefficients from the second-stage or diagnostics tests for
8 #' each endogenous regressor (F-statistics). Defaults to `FALSE`.
9 #' @template param_unused_dots
10 #'
11 #' @details This tidier currently only supports `ivreg`-classed objects
12 #' outputted by the `AER` package. The `ivreg` package also outputs
13 #' objects of class `ivreg`, and will be supported in a later release.
14 #'
15 #' @evalRd return_tidy(
16 #' "statistic.Sargan",
17 #' "p.value.Sargan",
18 #' "statistic.Wu.Hausman",
19 #' "p.value.Wu.Hausman",
20 #' "statistic.weakinst",
21 #' "p.value.weakinst",
22 #' regression = TRUE
23 #' )
24 #'
25 #' @examplesIf rlang::is_installed("AER")
26 #'
27 #' # load libraries for models and data
28 #' library(AER)
29 #'
30 #' # load data
31 #' data("CigarettesSW", package = "AER")
32 #'
33 #' # fit model
34 #' ivr <- ivreg(
35 #' log(packs) ~ income | population,
36 #' data = CigarettesSW,
37 #' subset = year == "1995"
38 #' )
39 #'
40 #' # summarize model fit with tidiers
41 #' tidy(ivr)
42 #' tidy(ivr, conf.int = TRUE)
43 #' tidy(ivr, conf.int = TRUE, instruments = TRUE)
44 #'
45 #' augment(ivr)
46 #' augment(ivr, data = CigarettesSW)
47 #' augment(ivr, newdata = CigarettesSW)
48 #'
49 #' glance(ivr)
50 #'
51 #' @export
52 #' @seealso [tidy()], [AER::ivreg()]
53 #' @family ivreg tidiers
54 #' @aliases ivreg_tidiers aer_tidiers
55 tidy.ivreg <- function(
56 x,
57 conf.int = FALSE,
58 conf.level = 0.95,
59 instruments = FALSE,
60 ...
61 ) {
62 check_ellipses("exponentiate", "tidy", "ivreg", ...)
63
64 # TODO: documentation on when you get what needs to be updated !!!
65
66 # case 1: user does not ask for instruments
67
68 if (!instruments) {
69 ret <- as_tibble(summary(x)$coefficients, rownames = "term")
70 colnames(ret) <- c("term", "estimate", "std.error", "statistic", "p.value")
71
72 if (conf.int) {
73 ci <- broom_confint_terms(x, level = conf.level)
74 ret <- dplyr::left_join(ret, ci, by = "term")
75 }
76
77 return(ret)
78 }
79
80 # case 2: user asks for instruments
81
82 end_vars <- names(coef(x))[-1] # subtract off the intercept
83 d <- summary(x, diagnostics = TRUE)$diagnostics
84
85 # drop last two rows, the Wu-Hausman and Sargan diagnostics
86 last_two_rows <- c(nrow(d) - 1, nrow(d))
87 d <- as_tibble(d)[-last_two_rows, ]
88 tibble::add_column(d, term = end_vars, .before = TRUE) |>
89 rename2("p.value" = "p-value", "num.df" = "df1", "den.df" = "df2")
90 }
91
92 #' @templateVar class ivreg
93 #' @template title_desc_augment
94 #'
95 #' @inherit tidy.ivreg params examples
96 #' @template param_data
97 #' @template param_newdata
98 #' @template param_unused_dots
99 #'
100 #' @details This tidier currently only supports `ivreg`-classed objects
101 #' outputted by the `AER` package. The `ivreg` package also outputs
102 #' objects of class `ivreg`, and will be supported in a later release.
103 #'
104 #' @evalRd return_augment()
105 #'
106 #' @export
107 #' @seealso [augment()], [AER::ivreg()]
108 #' @family ivreg tidiers
109 augment.ivreg <- function(x, data = model.frame(x), newdata = NULL, ...) {
110 augment_columns(x, data, newdata)
111 }
112
113 #' @templateVar class ivreg
114 #' @template title_desc_glance
115 #'
116 #' @inherit tidy.ivreg params examples
117 #' @param diagnostics Logical indicating whether or not to return the
118 #' Wu-Hausman and Sargan diagnostic information.
119 #'
120 #' @note Beginning 0.7.0, `glance.ivreg` returns statistics for the
121 #' Wu-Hausman test for endogeneity and the Sargan test of
122 #' overidentifying restrictions. Sargan test values are returned as `NA`
123 #' if the number of instruments is not greater than the number of
124 #' endogenous regressors.
125 #'
126 #' @details This tidier currently only supports `ivreg`-classed objects
127 #' outputted by the `AER` package. The `ivreg` package also outputs
128 #' objects of class `ivreg`, and will be supported in a later release.
129 #'
130 #' @evalRd return_glance(
131 #' "r.squared",
132 #' "adj.r.squared",
133 #' "sigma",
134 #' "df",
135 #' "df.residual",
136 #' "nobs",
137 #' statistic = "Wald test statistic.",
138 #' p.value = "P-value for the Wald test."
139 #' )
140 #'
141 #' @export
142 #' @seealso [glance()], [AER::ivreg()]
143 #' @family ivreg tidiers
144 glance.ivreg <- function(x, diagnostics = FALSE, ...) {
145 s <- summary(x, diagnostics = FALSE)
146
147 ret <- as_glance_tibble(
148 r.squared = s$r.squared,
149 adj.r.squared = s$adj.r.squared,
150 sigma = s$sigma,
151 statistic = s$waldtest[1],
152 p.value = s$waldtest[2],
153 df = s$df[1],
154 df.residual = df.residual(x),
155 nobs = stats::nobs(x),
156 na_types = "rrrrriii"
157 )
158
159 if (diagnostics) {
160 s_ <- summary(x, diagnostics = TRUE)
161
162 diags <- as_glance_tibble(
163 statistic.Sargan = s_$diagnostics["Sargan", "statistic"],
164 p.value.Sargan = s_$diagnostics["Sargan", "p-value"],
165 statistic.Wu.Hausman = s_$diagnostics["Wu-Hausman", "statistic"],
166 p.value.Wu.Hausman = s_$diagnostics["Wu-Hausman", "p-value"],
167 na_types = "rrrr"
168 )
169
170 return(bind_cols(ret, diags))
171 }
172
173 ret
174 }
175
176 #' @include null-and-default.R
177 #' @export
178 tidy.tobit <- tidy.default