Tidy a marginaleffects object

# S3 method for marginaleffects
tidy(x, conf_level = 0.95, ...)

## Arguments

x

An object produced by the marginaleffects function.

conf_level

numeric value between 0 and 1. Confidence level to use to build a confidence interval.

...

Additional arguments are passed to the predict() method supplied by the modeling package.These arguments are particularly useful for mixed-effects or bayesian models (see the online vignettes on the marginaleffects website). Available arguments can vary from model to model, depending on the range of supported arguments by each modeling package. See the "Model-Specific Arguments" section of the ?marginaleffects documentation for a non-exhaustive list of available arguments.

## Value

A "tidy" data.frame of summary statistics which conforms to the broom package specification.

## Details

The tidy function calculates average marginal effects by taking the mean of all the unit-level marginal effects computed by the marginaleffects function.

The standard error of the average marginal effects is obtained by taking the mean of each column of the Jacobian. . Then, we use this "Jacobian at the mean" in the Delta method to obtained standard errors.

In Bayesian models (e.g., brms), we compute Average Marginal Effects by applying the mean function twice. First, we apply it to all marginal effects for each posterior draw, thereby estimating one Average (or Median) Marginal Effect per iteration of the MCMC chain. Second, we take the mean and quantile function to the results of Step 1 to obtain the Average (or Median) Marginal Effect and its associated interval.

## Examples

mod <- lm(mpg ~ hp * wt + factor(gear), data = mtcars)
mfx <- marginaleffects(mod)

# average marginal effects
tidy(mfx)
#>       type term contrast    estimate  std.error  statistic      p.value
#> 1 response   hp    dY/dX -0.03533001 0.01044819 -3.3814471 7.210511e-04
#> 2 response   wt    dY/dX -3.52592595 0.73201586 -4.8167344 1.459267e-06
#> 3 response gear    4 - 3  0.82900152 1.11804097  0.7414769 4.584043e-01
#> 4 response gear    5 - 3  1.81649741 1.54760505  1.1737474 2.404962e-01
#>     conf.low   conf.high
#> 1 -0.0558081 -0.01485193
#> 2 -4.9606507 -2.09120123
#> 3 -1.3623185  3.02032155
#> 4 -1.2167528  4.84974757