modelbased icon indicating copy to clipboard operation
modelbased copied to clipboard

Specifying specific contrasts

Open bwiernik opened this issue 4 years ago • 6 comments

If I wanted to compute effect (sum) contrasts or custom contrasts (e.g., all treatments vs control), is that something I can currently do with modelbased? If not, is that something we want to add, or do we just point folks toward emmeans for that?

@DominiqueMakowski @mattansb

bwiernik avatar Sep 28 '21 16:09 bwiernik

I second that this would be helpful. Currently, it only uses pairwise.emmc but it would be great to be able to specify eff.emmc (for sum contrasts) too.

library(emmeans)
fit <- lm(Sepal.Length ~ Species, data = iris)
e <- emmeans(fit, specs = "Species")
contrast(e, method = "pairwise")
#>  contrast               estimate    SE  df t.ratio p.value
#>  setosa - versicolor      -0.930 0.103 147  -9.033  <.0001
#>  setosa - virginica       -1.582 0.103 147 -15.366  <.0001
#>  versicolor - virginica   -0.652 0.103 147  -6.333  <.0001
#> 
#> P value adjustment: tukey method for comparing a family of 3 estimates
contrast(e, method = "eff")
#>  contrast          estimate     SE  df t.ratio p.value
#>  setosa effect      -0.8373 0.0594 147 -14.086  <.0001
#>  versicolor effect   0.0927 0.0594 147   1.559  0.1212
#>  virginica effect    0.7447 0.0594 147  12.527  <.0001
#> 
#> P value adjustment: fdr method for 3 tests
library(modelbased)
estimate_contrasts(fit, contrast = "Species")
#> Marginal Contrasts Analysis
#> 
#> Level1     |     Level2 | Difference |         95% CI |   SE | t(147) |      p
#> ------------------------------------------------------------------------------
#> setosa     | versicolor |      -0.93 | [-1.18, -0.68] | 0.10 |  -9.03 | < .001
#> setosa     |  virginica |      -1.58 | [-1.83, -1.33] | 0.10 | -15.37 | < .001
#> versicolor |  virginica |      -0.65 | [-0.90, -0.40] | 0.10 |  -6.33 | < .001
#> 
#> Marginal contrasts estimated at Species
#> p-value adjustment method: Holm (1979)

Created on 2021-10-19 by the reprex package (v2.0.1)

jmgirard avatar Oct 19 '21 18:10 jmgirard

Maybe that's also something for the marginaleffects package, see https://vincentarelbundock.github.io/marginaleffects/articles/contrasts.html. Tagging @vincentarelbundock

strengejacke avatar Oct 19 '21 19:10 strengejacke

In terms of my teaching--I would really like to be able to teach one set of APIs, so I would like to have something that matches the modelbased API, even if it's just a wrapper around marginaleffects

bwiernik avatar Oct 19 '21 19:10 bwiernik

Right now, marginaleffects can only do pairwise contrasts. I've been more focused on slopes, but plan to turn my attention to contrasts as soon as possible. I am optimistic about the prospects of implementing more complex contrasts but, realistically, feature parity with emmeans seems unlikely in the medium run (it's sooo huge).

I'd be very happy to hear about what kind of wrapper might be of interest for modelbased. I was careful to keep the number of dependencies low to allow packages to onboard marginaleffects at reasonably low cost. But given that the features you want are not there yet, maybe it makes more sense to improve the emmeans wrapper instead, at least in the short run.

In any case, I'd be really curious to know what you think a "good" or "modelbased-consistent" API for contrasts would be. Knowing that would probably influence my design choices going forward.

(FYI, I'm travelling all this week, so I might not be able to react or engage as much.)

vincentarelbundock avatar Oct 19 '21 20:10 vincentarelbundock

I'll also note that, for planned contrasts, I really like the {hypr} package: https://github.com/mmrabe/hypr

jmgirard avatar Oct 29 '21 19:10 jmgirard