| 1 |
#' @templateVar class margins |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `margins` object returned from [margins::margins()]. |
| 5 |
#' @template param_confint |
| 6 |
#' @template param_unused_dots |
| 7 |
#' |
| 8 |
#' @evalRd return_tidy(regression = TRUE) |
| 9 |
#' |
| 10 |
#' @details The `margins` package provides a way to obtain coefficient marginal |
| 11 |
#' effects for a variety of (non-linear) models, such as logit or models with |
| 12 |
#' multiway interaction terms. Note that the `glance.margins()` method |
| 13 |
#' requires rerunning the underlying model again, which can take some time. |
| 14 |
#' Similarly, an `augment.margins()` method is not currently supported, but |
| 15 |
#' users can simply run the underlying model to obtain the same information. |
| 16 |
#' |
| 17 |
#' @examplesIf rlang::is_installed("margins") |
| 18 |
#' |
| 19 |
#' # load libraries for models and data |
| 20 |
#' library(margins) |
| 21 |
#' |
| 22 |
#' # example 1: logit model |
| 23 |
#' mod_log <- glm(am ~ cyl + hp + wt, data = mtcars, family = binomial) |
| 24 |
#' |
| 25 |
#' # get tidied "naive" model coefficients |
| 26 |
#' tidy(mod_log) |
| 27 |
#' |
| 28 |
#' # convert to marginal effects with margins() |
| 29 |
#' marg_log <- margins(mod_log) |
| 30 |
#' |
| 31 |
#' # get tidied marginal effects |
| 32 |
#' tidy(marg_log) |
| 33 |
#' tidy(marg_log, conf.int = TRUE) |
| 34 |
#' |
| 35 |
#' # requires running the underlying model again. quick for this example |
| 36 |
#' glance(marg_log) |
| 37 |
#' |
| 38 |
#' # augmenting `margins` outputs isn't supported, but |
| 39 |
#' # you can get the same info by running on the underlying model |
| 40 |
#' augment(mod_log) |
| 41 |
#' |
| 42 |
#' # example 2: threeway interaction terms |
| 43 |
#' mod_ie <- lm(mpg ~ wt * cyl * disp, data = mtcars) |
| 44 |
#' |
| 45 |
#' # get tidied "naive" model coefficients |
| 46 |
#' tidy(mod_ie) |
| 47 |
#' |
| 48 |
#' # convert to marginal effects with margins() |
| 49 |
#' marg_ie0 <- margins(mod_ie) |
| 50 |
#' # get tidied marginal effects |
| 51 |
#' tidy(marg_ie0) |
| 52 |
#' glance(marg_ie0) |
| 53 |
#' |
| 54 |
#' # marginal effects evaluated at specific values of a variable (here: cyl) |
| 55 |
#' marg_ie1 <- margins(mod_ie, at = list(cyl = c(4,6,8))) |
| 56 |
#' |
| 57 |
#' # summarize model fit with tidiers |
| 58 |
#' tidy(marg_ie1) |
| 59 |
#' |
| 60 |
#' # marginal effects of one interaction variable (here: wt), modulated at |
| 61 |
#' # specific values of the two other interaction variables (here: cyl and drat) |
| 62 |
#' marg_ie2 <- margins(mod_ie, |
| 63 |
#' variables = "wt", |
| 64 |
#' at = list(cyl = c(4,6,8), drat = c(3, 3.5, 4))) |
| 65 |
#' |
| 66 |
#' # summarize model fit with tidiers |
| 67 |
#' tidy(marg_ie2) |
| 68 |
#' |
| 69 |
#' @export |
| 70 |
#' @aliases margins_tidiers |
| 71 |
#' @family margins tidiers |
| 72 |
#' @seealso [tidy()], [margins::margins()] |
| 73 |
tidy.margins <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { |
| 74 |
check_ellipses("exponentiate", "tidy", "margins", ...) |
| 75 |
|
| 76 |
ret <- as_tibble(summary(x, level = conf.level)) |
| 77 |
|
| 78 |
# IF statement for tidying any "at" variables. |
| 79 |
if ("at" %in% names(attributes(x))) { |
| 80 |
at_vars <- setdiff(names(attributes(x)$at), "index") |
| 81 |
std_cols <- c("factor", "AME", "SE", "z", "p", "lower", "upper") |
| 82 |
ret <- |
| 83 |
{ |
| 84 |
tryCatch( |
| 85 |
tidyr::pivot_longer( |
| 86 |
ret, |
| 87 |
dplyr::all_of(at_vars), |
| 88 |
names_to = "at.variable", |
| 89 |
values_to = "at.value" |
| 90 |
), |
| 91 |
error = function(e) { |
| 92 |
mutate( |
| 93 |
., |
| 94 |
dplyr::across(dplyr::all_of(at_vars), as.character), |
| 95 |
cli::cli_warn( |
| 96 |
"The {.field at.value} column was coerced to character." |
| 97 |
) |
| 98 |
) |> |
| 99 |
tidyr::pivot_longer( |
| 100 |
dplyr::all_of(at_vars), |
| 101 |
names_to = "at.variable", |
| 102 |
values_to = "at.value" |
| 103 |
) |
| 104 |
} |
| 105 |
) |
| 106 |
} |
| 107 |
} |
| 108 |
# End of IF statement for tidying any "at" variables. |
| 109 |
|
| 110 |
# Rename and reorder variables |
| 111 |
ret <- |
| 112 |
ret |> |
| 113 |
dplyr::select( |
| 114 |
term = factor, |
| 115 |
dplyr::contains("at."), |
| 116 |
estimate = AME, |
| 117 |
std.error = SE, |
| 118 |
statistic = z, |
| 119 |
p.value = p, |
| 120 |
conf.low = lower, |
| 121 |
conf.high = upper |
| 122 |
) |
| 123 |
|
| 124 |
# Remove confidence interval if not specified |
| 125 |
if (!conf.int) { |
| 126 |
ret <- dplyr::select(ret, -c(conf.low, conf.high)) |
| 127 |
} |
| 128 |
|
| 129 |
# Return tidied tibble object |
| 130 |
return(ret) |
| 131 |
} |
| 132 |
|
| 133 |
#' @templateVar class margins |
| 134 |
#' @template title_desc_glance |
| 135 |
#' |
| 136 |
#' @inherit tidy.margins params examples |
| 137 |
#' |
| 138 |
#' @evalRd return_glance( |
| 139 |
#' "r.squared", |
| 140 |
#' "adj.r.squared", |
| 141 |
#' "sigma", |
| 142 |
#' "statistic", |
| 143 |
#' "p.value", |
| 144 |
#' "df", |
| 145 |
#' "df.residual", |
| 146 |
#' "nobs" |
| 147 |
#' ) |
| 148 |
#' |
| 149 |
#' @export |
| 150 |
glance.margins <- function(x, ...) { |
| 151 |
orig_mod_call <- attributes(x)$call |
| 152 |
ret <- broom::glance(eval(orig_mod_call), ...) |
| 153 |
return(ret) |
| 154 |
} |
| 155 |
|
| 156 |
#' @include null-and-default.R |
| 157 |
#' @export |
| 158 |
augment.margins <- augment.default |