Calculate average contrasts by taking the mean of all the unit-level contrasts computed by the predictions function.

# S3 method for comparisons
tidy(x, conf_level = 0.95, transform_avg = NULL, ...)

Arguments

x

An object produced by the comparisons function.

conf_level

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

transform_avg

(experimental) A function applied to the estimates and confidence intervals after the unit-level estimates have been averaged.

...

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

To compute standard errors around the average marginaleffects, we begin by applying the mean function to each column of the Jacobian. Then, we use this matrix 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 calculate the mean and the quantile function to the results of Step 1 to obtain the Average Marginal Effect and its associated interval.

Examples

mod <- lm(mpg ~ factor(gear), data = mtcars)
contr <- comparisons(mod, variables = list(gear = "sequential"))
tidy(contr)
#>       type term contrast  estimate std.error statistic      p.value  conf.low
#> 1 response gear    4 - 3  8.426667  1.823417  4.621361 3.812306e-06  4.852836
#> 2 response gear    5 - 4 -3.153333  2.506046 -1.258290 2.082869e-01 -8.065094
#>   conf.high
#> 1 12.000498
#> 2  1.758428