emmeansr-marginaleffects

Use marginaleffects to recover all the pairwise comparison that emmeans reports


I can't get marginaleffects to return all the pairwise comparisons of a three level variable.

Consider this model

library(tidyverse)
library(palmerpenguins)
library(marginaleffects)
library(emmeans)

lm1 <- lm(
 bill_length_mm ~ species,
 data = penguins
 ) 

If you want the pairwise contrasts of species from emmeans you write

lm1 %>% 
  emmeans(
    pairwise ~ species
  )

which returns

$emmeans
 species   emmean    SE  df lower.CL upper.CL
 Adelie      38.8 0.241 339     38.3     39.3
 Chinstrap   48.8 0.359 339     48.1     49.5
 Gentoo      47.5 0.267 339     47.0     48.0

Confidence level used: 0.95 

$contrasts
 contrast           estimate    SE  df t.ratio p.value
 Adelie - Chinstrap   -10.04 0.432 339 -23.232  <.0001
 Adelie - Gentoo       -8.71 0.360 339 -24.237  <.0001
 Chinstrap - Gentoo     1.33 0.447 339   2.971  0.0089

P value adjustment: tukey method for comparing a family of 3 estimates 

However, this, from marginaleffects::avg_comparisons()

avg_comparisons(
  lm1, 
  variables = "species" 
  ) 

only reports the first two contrasts:

           Contrast Estimate Std. Error    z Pr(>|z|)     S 2.5 % 97.5 %
 Chinstrap - Adelie    10.04      0.432 23.2   <0.001 394.2  9.20  10.89
 Gentoo - Adelie        8.71      0.360 24.2   <0.001 428.7  8.01   9.42

Term: species
Type:  response 

How do I get marginaleffects to report all three contrasts, for all pairwise comparisons of the three levels of species?


Solution

  • If you type ?avg_comparisons or visit the marginaleffects.com website, you will see that this option is clearly documented under the variables argument:

    library(palmerpenguins)
    library(marginaleffects)
    
    lm1 <- lm(
      bill_length_mm ~ species,
      data = penguins
    )
    
    avg_comparisons(
      lm1,
      variables = list("species" = "pairwise")
    )
    #> 
    #>            Contrast Estimate Std. Error     z Pr(>|z|)     S 2.5 % 97.5 %
    #>  Chinstrap - Adelie    10.04      0.432 23.23  < 0.001 394.2  9.20 10.890
    #>  Gentoo - Adelie        8.71      0.360 24.24  < 0.001 428.7  8.01  9.418
    #>  Gentoo - Chinstrap    -1.33      0.447 -2.97  0.00297   8.4 -2.21 -0.452
    #> 
    #> Term: species
    #> Type:  response