Tidy summarizes information about the components of a model. A model component might be a single term in a regression, a single hypothesis, a cluster, or a class. Exactly what tidy considers to be a model component varies across models but is usually self-evident. If a model has several distinct types of components, you will need to specify which components to return.
# S3 method for margins tidy(x, conf.int = FALSE, conf.level = 0.95, ...)
x | A |
---|---|
conf.int | Logical indicating whether or not to include a confidence
interval in the tidied output. Defaults to |
conf.level | The confidence level to use for the confidence interval
if |
... | Additional arguments. Not used. Needed to match generic
signature only. Cautionary note: Misspelled arguments will be
absorbed in |
The margins
package provides a way to obtain coefficient marginal
effects for a variety of (non-linear) models, such as logit or models with
multiway interaction terms. Note that the glance.margins()
method
requires rerunning the underlying model again, which can take some time.
Similarly, an augment.margins()
method is not currently supported, but
users can simply run the underlying model to obtain the same information.
A tibble::tibble()
with columns:
Upper bound on the confidence interval for the estimate.
Lower bound on the confidence interval for the estimate.
The estimated value of the regression term.
The two-sided p-value associated with the observed statistic.
The value of a T-statistic to use in a hypothesis that the regression term is non-zero.
The standard error of the regression term.
The name of the regression term.
library(margins) ## Example 1: Logit model ## mod_log <- glm(am ~ cyl + hp + wt, data = mtcars, family = binomial) # Get tidied "naive" model coefficients tidy(mod_log)#> # A tibble: 4 x 5 #> term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 (Intercept) 19.7 8.12 2.43 0.0152 #> 2 cyl 0.488 1.07 0.455 0.649 #> 3 hp 0.0326 0.0189 1.73 0.0840 #> 4 wt -9.15 4.15 -2.20 0.0276# Convert to marginal effects with margins::margins() marg_log <- margins(mod_log) # Get tidied marginal effects tidy(marg_log)#> # A tibble: 3 x 5 #> term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 cyl 0.0215 0.0470 0.457 0.648 #> 2 hp 0.00143 0.000618 2.32 0.0204 #> 3 wt -0.403 0.115 -3.49 0.000487#> # A tibble: 3 x 7 #> term estimate std.error statistic p.value conf.low conf.high #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 cyl 0.0215 0.0470 0.457 0.648 -0.0706 0.114 #> 2 hp 0.00143 0.000618 2.32 0.0204 0.000222 0.00265 #> 3 wt -0.403 0.115 -3.49 0.000487 -0.629 -0.176#> # A tibble: 1 x 8 #> null.deviance df.null logLik AIC BIC deviance df.residual nobs #> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <int> <int> #> 1 43.2 31 -4.92 17.8 23.7 9.84 28 32if (FALSE) augment(marg_log) ## Not supported. augment(mod_log) ## But can get the same info by running on the underlying model.#> # A tibble: 32 x 11 #> .rownames am cyl hp wt .fitted .resid .std.resid .hat .sigma #> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 Mazda RX4 1 6 110 2.62 2.24 0.449 0.529 0.278 0.595 #> 2 Mazda RX… 1 6 110 2.88 -0.0912 1.22 1.51 0.352 0.529 #> 3 Datsun 7… 1 4 93 2.32 3.46 0.249 0.262 0.0960 0.602 #> 4 Hornet 4… 0 6 110 3.22 -3.20 -0.282 -0.297 0.0945 0.601 #> 5 Hornet S… 0 8 175 3.44 -2.17 -0.466 -0.527 0.220 0.595 #> 6 Valiant 0 6 105 3.46 -5.61 -0.0856 -0.0866 0.0221 0.604 #> 7 Duster 3… 0 8 245 3.57 -1.07 -0.766 -0.941 0.337 0.576 #> 8 Merc 240D 0 4 62 3.19 -5.51 -0.0897 -0.0915 0.0376 0.603 #> 9 Merc 230 0 4 95 3.15 -4.07 -0.184 -0.196 0.122 0.603 #> 10 Merc 280 0 6 123 3.44 -4.84 -0.126 -0.128 0.0375 0.603 #> # … with 22 more rows, and 1 more variable: .cooksd <dbl>## Example 2: Threeway interaction terms ## mod_ie <- lm(mpg ~ wt * cyl * disp, data = mtcars) # Get tidied "naive" model coefficients tidy(mod_ie)#> # A tibble: 8 x 5 #> term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 (Intercept) 108. 23.3 4.62 0.000109 #> 2 wt -24.8 8.47 -2.92 0.00744 #> 3 cyl -10.8 4.34 -2.49 0.0201 #> 4 disp -0.593 0.213 -2.79 0.0102 #> 5 wt:cyl 2.91 1.42 2.05 0.0514 #> 6 wt:disp 0.184 0.0685 2.69 0.0127 #> 7 cyl:disp 0.0752 0.0268 2.81 0.00979 #> 8 wt:cyl:disp -0.0233 0.00861 -2.71 0.0123# Convert to marginal effects with margins::margins() marg_ie0 <- margins(mod_ie) # Get tidied marginal effects tidy(marg_ie0)#> # A tibble: 3 x 5 #> term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 cyl -3.85 1.46 -2.65 0.00812 #> 2 disp -0.0295 0.0174 -1.70 0.0900 #> 3 wt -2.01 1.17 -1.72 0.0860#> # A tibble: 1 x 12 #> r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 0.896 0.865 2.21 29.4 2.75e-10 7 -66.2 150. 164. #> # … with 3 more variables: deviance <dbl>, df.residual <int>, nobs <int># Marginal effects evaluated at specific values of a variable (here: cyl) marg_ie1 <- margins(mod_ie, at = list(cyl = c(4,6,8))) tidy(marg_ie1)#> # A tibble: 9 x 7 #> term at.variable at.value estimate std.error statistic p.value #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 cyl cyl 4 -3.85 1.46 -2.65 0.00814 #> 2 cyl cyl 6 -3.85 1.46 -2.65 0.00812 #> 3 cyl cyl 8 -3.85 1.46 -2.65 0.00810 #> 4 disp cyl 4 0.000978 0.0314 0.0312 0.975 #> 5 disp cyl 6 0.00134 0.0182 0.0737 0.941 #> 6 disp cyl 8 0.00170 0.0120 0.141 0.888 #> 7 wt cyl 4 7.91 5.06 1.56 0.118 #> 8 wt cyl 6 2.96 2.52 1.18 0.239 #> 9 wt cyl 8 -1.98 2.40 -0.825 0.409# Marginal effects of one interaction variable (here: wt), modulated at # specific values of the two other interaction variables (here: cyl and drat) marg_ie2 <- margins(mod_ie, variables = "wt", ## Main var at = list(cyl = c(4,6,8), drat = c(3, 3.5, 4))) ## Modulating vars tidy(marg_ie2)#> # A tibble: 18 x 7 #> term at.variable at.value estimate std.error statistic p.value #> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 wt cyl 4 7.91 5.06 1.56 0.118 #> 2 wt drat 3 7.91 5.06 1.56 0.118 #> 3 wt cyl 4 7.91 5.06 1.56 0.118 #> 4 wt drat 3.5 7.91 5.06 1.56 0.118 #> 5 wt cyl 4 7.91 5.06 1.56 0.118 #> 6 wt drat 4 7.91 5.06 1.56 0.118 #> 7 wt cyl 6 2.96 2.52 1.18 0.239 #> 8 wt drat 3 2.96 2.52 1.18 0.239 #> 9 wt cyl 6 2.96 2.52 1.18 0.239 #> 10 wt drat 3.5 2.96 2.52 1.18 0.239 #> 11 wt cyl 6 2.96 2.52 1.18 0.239 #> 12 wt drat 4 2.96 2.52 1.18 0.239 #> 13 wt cyl 8 -1.98 2.40 -0.825 0.409 #> 14 wt drat 3 -1.98 2.40 -0.825 0.409 #> 15 wt cyl 8 -1.98 2.40 -0.825 0.409 #> 16 wt drat 3.5 -1.98 2.40 -0.825 0.409 #> 17 wt cyl 8 -1.98 2.40 -0.825 0.409 #> 18 wt drat 4 -1.98 2.40 -0.825 0.409