vincentarelbundock / marginaleffects

R package to compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 100 classes of statistical and ML models. Conduct linear and non-linear hypothesis tests, or equivalence tests. Calculate uncertainty estimates using the delta method, bootstrapping, or simulation-based inference
https://marginaleffects.com
Other
392 stars 43 forks source link

Issue #1120: automatically shift to `ratioavg` when calling `avg_comparisons()` #1121

Closed vincentarelbundock closed 1 month ago

vincentarelbundock commented 1 month ago

@ngreifer, do you have a view of what the default ratio function should be when we take an average?

library(marginaleffects)
mod <- glm(vs ~ am + wt, data = mtcars, family = binomial)
d0 <- transform(mtcars, am = 0)
d1 <- transform(mtcars, am = 1)
p0 <- predict(mod, newdata = d0, type = "response")
p1 <- predict(mod, newdata = d1, type = "response")

# marginaleffects 0.20.0
mean(p1 / p0) 

    [1] 0.2375687

# this PR
mean(p1) / mean(p0) 

    [1] 0.395687

avg_comparisons(mod, variables = "am", comparison = "ratio")

     Term          Contrast Estimate Std. Error    z Pr(>|z|)    S 2.5 % 97.5 %
       am mean(1) / mean(0)    0.396     0.0779 5.08   <0.001 21.3 0.243  0.548
ngreifer commented 1 month ago

It should absolutely be the latter, but I thought users could already request this using "ratioavg". The former is not useful because risk ratios are not collapsible (except in a very obscure sense). I think it would be a good change to make all the avg_{.}() functions automatically apply the "{.}avg" suffix for any comparison.

vincentarelbundock commented 1 month ago

Thanks, that's what I thought.

Yes, ratioavg was available, but I discovered an inconsistency in the code where we would automatically switch to the avg version if by was the name of a variable as character, but not when calling avg_comparisons() or using by=TRUE. All three use cases should now switch automatically to the appropriate function.

Merged.

Thanks for confirming!