Debian Codegraph @

R/mfx.R

raw
1 #' @templateVar class mfx
2 #' @template title_desc_tidy
3 #' @description The particular functions below provide generic tidy methods for
4 #' objects returned by the `mfx` package, preserving the calculated marginal
5 #' effects instead of the naive model coefficients. The returned tidy tibble
6 #' will also include an additional "atmean" column indicating how the marginal
7 #' effects were originally calculated (see Details below).
8 #' @param x A `logitmfx`, `negbinmfx`, `poissonmfx`, or `probitmfx` object.
9 #' (Note that `betamfx` objects receive their own set of tidiers.)
10 #' @template param_confint
11 #' @template param_unused_dots
12 #' @evalRd return_tidy(
13 #' "term",
14 #' atmean = "TRUE if the marginal effects were originally calculated as
15 #' the partial effects for the average observation. If FALSE, then these
16 #' were instead calculated as average partial effects.",
17 #' "estimate",
18 #' "std.error",
19 #' "statistic",
20 #' "p.value",
21 #' "conf.low",
22 #' "conf.high"
23 #' )
24 #' @details The `mfx` package provides methods for calculating marginal effects
25 #' for various generalized linear models (GLMs). Unlike standard linear
26 #' models, estimated model coefficients in a GLM cannot be directly
27 #' interpreted as marginal effects (i.e., the change in the response variable
28 #' predicted after a one unit change in one of the regressors). This is
29 #' because the estimated coefficients are multiplicative, dependent on both
30 #' the link function that was used for the estimation and any other variables
31 #' that were included in the model. When calculating marginal effects, users
32 #' must typically choose whether they want to use i) the average observation
33 #' in the data, or ii) the average of the sample marginal effects. See
34 #' `vignette("mfxarticle")` from the `mfx` package for more details.
35 #'
36 #' @examplesIf rlang::is_installed("mfx")
37 #'
38 #' # load libraries for models and data
39 #' library(mfx)
40 #'
41 #' # get the marginal effects from a logit regression
42 #' mod_logmfx <- logitmfx(am ~ cyl + hp + wt, atmean = TRUE, data = mtcars)
43 #'
44 #' tidy(mod_logmfx, conf.int = TRUE)
45 #'
46 #' # compare with the naive model coefficients of the same logit call
47 #' tidy(
48 #' glm(am ~ cyl + hp + wt, family = binomial, data = mtcars),
49 #' conf.int = TRUE
50 #' )
51 #'
52 #' augment(mod_logmfx)
53 #' glance(mod_logmfx)
54 #'
55 #' # another example, this time using probit regression
56 #' mod_probmfx <- probitmfx(am ~ cyl + hp + wt, atmean = TRUE, data = mtcars)
57 #'
58 #' tidy(mod_probmfx, conf.int = TRUE)
59 #' augment(mod_probmfx)
60 #' glance(mod_probmfx)
61 #'
62 #' @family mfx tidiers
63 #' @seealso [tidy()], [mfx::logitmfx()], [mfx::negbinmfx()], [mfx::poissonmfx()], [mfx::probitmfx()]
64 #' @export
65 tidy.mfx <-
66 function(x, conf.int = FALSE, conf.level = 0.95, ...) {
67 check_ellipses("exponentiate", "tidy", "mfx", ...)
68
69 x_tidy <- as_tibble(x$mfxest, rownames = "term")
70 colnames(x_tidy) <- c(
71 "term",
72 "estimate",
73 "std.error",
74 "statistic",
75 "p.value"
76 )
77
78 # Optional: Add "atmean" column
79 # If no "atmean" argument is specified in the model call, then will default to TRUE
80 if (!grepl("atmean", format(c(x$call)))) {
81 x_tidy$atmean <- TRUE
82 # Else, extract the user-specified "atmean" argument from the model call
83 } else {
84 x_tidy$atmean <- as.logical(gsub(
85 ".*atmean = |,.*|).*",
86 "",
87 format(c(x$call))
88 ))
89 }
90
91 if (conf.int) {
92 x_tidy <-
93 x_tidy |>
94 dplyr::mutate(
95 conf.low = estimate -
96 qt(1 - (1 - conf.level) / 2, df = x$fit$df.residual) * std.error,
97 conf.high = estimate +
98 qt(1 - (1 - conf.level) / 2, df = x$fit$df.residual) * std.error
99 )
100 }
101
102 dplyr::select(x_tidy, term, contains("atmean"), everything())
103 }
104
105 #' @rdname tidy.mfx
106 #' @method tidy logitmfx
107 #' @export
108 tidy.logitmfx <- tidy.mfx
109
110 #' @rdname tidy.mfx
111 #' @method tidy negbinmfx
112 #' @export
113 tidy.negbinmfx <- tidy.mfx
114
115 #' @rdname tidy.mfx
116 #' @method tidy poissonmfx
117 #' @export
118 tidy.poissonmfx <- tidy.mfx
119
120 #' @rdname tidy.mfx
121 #' @method tidy probitmfx
122 #' @export
123 tidy.probitmfx <- tidy.mfx
124
125 #' @templateVar class mfx
126 #' @template title_desc_augment
127 #' @inherit tidy.mfx examples
128 #' @param x A `logitmfx`, `negbinmfx`, `poissonmfx`, or `probitmfx` object.
129 #' (Note that `betamfx` objects receive their own set of tidiers.)
130 #' @template param_data
131 #' @template param_newdata
132 #' @param type.predict Passed to [stats::predict.glm()] `type` argument.
133 #' Defaults to `"link"`.
134 #' @param type.residuals Passed to [stats::residuals.glm()] and
135 #' to [stats::rstandard.glm()] `type` arguments. Defaults to `"deviance"`.
136 #' @template param_se_fit
137 #' @template param_unused_dots
138 #' @evalRd return_augment(
139 #' ".se.fit",
140 #' ".hat",
141 #' ".sigma",
142 #' ".std.resid",
143 #' ".cooksd"
144 #' )
145 #' @details This generic augment method wraps [augment.glm()] for applicable
146 #' objects from the `mfx` package.
147 #' @family mfx tidiers
148 #' @seealso [augment.glm()], [mfx::logitmfx()], [mfx::negbinmfx()],
149 #' [mfx::poissonmfx()], [mfx::probitmfx()]
150 #' @export
151 augment.mfx <- function(
152 x,
153 data = model.frame(x$fit),
154 newdata = NULL,
155 type.predict = c("link", "response", "terms"),
156 type.residuals = c("deviance", "pearson"),
157 se_fit = FALSE,
158 ...
159 ) {
160 # Use augment.glm() method on internal fit object
161 augment.glm(
162 x$fit,
163 data = data,
164 newdata = newdata,
165 type.predict = type.predict,
166 type.residuals = type.residuals,
167 se_fit = se_fit,
168 ...
169 )
170 }
171 #' @rdname augment.mfx
172 #' @method augment logitmfx
173 #' @export
174 augment.logitmfx <- augment.mfx
175
176 #' @rdname augment.mfx
177 #' @method augment negbinmfx
178 #' @export
179 augment.negbinmfx <- augment.mfx
180
181 #' @rdname augment.mfx
182 #' @method augment poissonmfx
183 #' @export
184 augment.poissonmfx <- augment.mfx
185
186 #' @rdname augment.mfx
187 #' @method augment probitmfx
188 #' @export
189 augment.probitmfx <- augment.mfx
190
191
192 #' @templateVar class mfx
193 #' @template title_desc_glance
194 #' @inherit tidy.mfx examples
195 #' @param x A `logitmfx`, `negbinmfx`, `poissonmfx`, or `probitmfx` object. (Note that `betamfx` objects receive their own set of tidiers.)
196 #' @template param_unused_dots
197 #' @evalRd return_glance(
198 #' "null.deviance",
199 #' "df.null",
200 #' "logLik",
201 #' "AIC",
202 #' "BIC",
203 #' "deviance",
204 #' "df.residual",
205 #' "nobs"
206 #' )
207 #' @details This generic glance method wraps [glance.glm()] for applicable
208 #' objects from the `mfx` package.
209 #' @family mfx tidiers
210 #' @seealso [glance.glm()], [mfx::logitmfx()], [mfx::negbinmfx()],
211 #' [mfx::poissonmfx()], [mfx::probitmfx()]
212 #' @export
213 glance.mfx <- function(x, ...) {
214 # Use glance.glm() method on internal fit object
215 glance.glm(x$fit)
216 }
217 #' @rdname glance.mfx
218 #' @method glance logitmfx
219 #' @export
220 glance.logitmfx <- glance.mfx
221
222 #' @rdname glance.mfx
223 #' @method glance negbinmfx
224 #' @export
225 glance.negbinmfx <- glance.mfx
226
227 #' @rdname glance.mfx
228 #' @method glance poissonmfx
229 #' @export
230 glance.poissonmfx <- glance.mfx
231
232 #' @rdname glance.mfx
233 #' @method glance probitmfx
234 #' @export
235 glance.probitmfx <- glance.mfx
236
237 #' @templateVar class betamfx
238 #' @template title_desc_tidy
239 #' @inherit tidy.mfx description details
240 #' @param x A `betamfx` object.
241 #' @template param_confint
242 #' @template param_unused_dots
243 #' @evalRd return_tidy(
244 #' "term",
245 #' atmean = "TRUE if the marginal effects were originally calculated as the
246 #' partial effects for the average observation. If FALSE, then these were
247 #' instead calculated as average partial effects.",
248 #' "estimate",
249 #' "std.error",
250 #' "statistic",
251 #' "p.value",
252 #' "conf.low",
253 #' "conf.high"
254 #' )
255 #'
256 #' @examplesIf rlang::is_installed("mfx")
257 #'
258 #' library(mfx)
259 #'
260 #' # Simulate some data
261 #' set.seed(12345)
262 #' n <- 1000
263 #' x <- rnorm(n)
264 #'
265 #' # Beta outcome
266 #' y <- rbeta(n, shape1 = plogis(1 + 0.5 * x), shape2 = (abs(0.2 * x)))
267 #' # Use Smithson and Verkuilen correction
268 #' y <- (y * (n - 1) + 0.5) / n
269 #'
270 #' d <- data.frame(y, x)
271 #' mod_betamfx <- betamfx(y ~ x | x, data = d)
272 #'
273 #' tidy(mod_betamfx, conf.int = TRUE)
274 #'
275 #' # Compare with the naive model coefficients of the equivalent betareg call (not run)
276 #' # tidy(betamfx(y ~ x | x, data = d), conf.int = TRUE)
277 #'
278 #' augment(mod_betamfx)
279 #' glance(mod_betamfx)
280 #'
281 #' @family mfx tidiers
282 #' @seealso [tidy.betareg()], [mfx::betamfx()]
283 #' @export
284 tidy.betamfx <- tidy.mfx
285
286
287 #' @templateVar class betamfx
288 #' @template title_desc_augment
289 #' @inherit tidy.betamfx examples
290 #' @param x A `betamfx` object.
291 #' @template param_data
292 #' @template param_newdata
293 #' @param type.predict Character indicating type of prediction to use. Passed to
294 #' the `type` argument of [betareg::predict.betareg()]. Defaults to
295 #' `"response"`.
296 #' @param type.residuals Character indicating type of residuals to use. Passed
297 #' to the `type` argument of [betareg::residuals.betareg()]. Defaults to
298 #' `"sweighted2`.
299 #' @template param_unused_dots
300 #' @evalRd return_augment(".cooksd")
301 #' @details This augment method wraps [augment.betareg()] for
302 #' [mfx::betamfx()] objects.
303 #' @family mfx tidiers
304 #' @seealso [augment.betareg()], [mfx::betamfx()]
305 #' @export
306 augment.betamfx <- function(
307 x,
308 data = model.frame(x$fit),
309 newdata = NULL,
310 type.predict = c("response", "link", "precision", "variance", "quantile"),
311 type.residuals = c(
312 "sweighted2",
313 "deviance",
314 "pearson",
315 "response",
316 "weighted",
317 "sweighted"
318 ),
319 ...
320 ) {
321 # Use augment.betareg() method on internal fit object
322 augment.betareg(
323 x$fit,
324 data = data,
325 newdata = newdata,
326 type.predict = type.predict,
327 type.residuals = type.residuals,
328 ...
329 )
330 }
331
332
333 #' @templateVar class betamfx
334 #' @template title_desc_glance
335 #' @inherit tidy.betamfx examples
336 #' @param x A `betamfx` object.
337 #' @template param_unused_dots
338 #' @evalRd return_glance(
339 #' "pseudo.r.squared",
340 #' "df.null",
341 #' "logLik",
342 #' "AIC",
343 #' "BIC",
344 #' "df.residual",
345 #' "nobs"
346 #' )
347 #' @details This glance method wraps [glance.betareg()] for [mfx::betamfx()] objects.
348 #' @family mfx tidiers
349 #' @seealso [glance.betareg()], [mfx::betamfx()]
350 #' @export
351 glance.betamfx <- function(x, ...) {
352 # Use glance.betareg() method on internal fit object
353 glance.betareg(x$fit)
354 }