rvlenth / emmeans

Estimated marginal means
https://rvlenth.github.io/emmeans/
341 stars 30 forks source link

Problematic output using the contrast() and pairs() functions #417

Closed camillesouama closed 1 year ago

camillesouama commented 1 year ago

Hi,

When I use the contrast() and pairs() functions on emmGrid objects, the following output and warning appear:

[1] x

<0 rows> (or 0-length row.names) Warning message: In class(rtn) <- c("summary_emm", "data.frame") : Setting class(x) to multiple strings ("summary_emm", "data.frame", ...); result will no longer be an S4 object Do you maybe know why this issue occurs and how I can fix it?
rvlenth commented 1 year ago

It is impossible to tell anything from what little context you give. I can only guess, and chances are I will guess wrong. Show the code you used to fit the model, the results of summary(model), and your code for emmeans() and contrast().

camillesouama commented 1 year ago

Thanks for looking into it, here is the info required:

> model <- lm(bmi ~ sex + age ,  data = Sample_main)
> summary(model)

Call:
lm(formula = bmi ~ sex + age, data = Sample_main)
Residuals:
     Min       1Q   Median       3Q      Max 
-10.5718  -3.2990  -0.9122   2.1633  30.1579 
Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) 21.911586   0.332511  65.897  < 2e-16 ***
sex1        -0.662307   0.188288  -3.518 0.000442 ***
age          0.098307   0.006807  14.442  < 2e-16 ***
--
Residual standard error: 4.828 on 2956 degrees of freedom
Multiple R-squared:  0.07238,   Adjusted R-squared:  0.07175 
F-statistic: 115.3 on 2 and 2956 DF,  p-value: < 2.2e-16
> emmeans(model, pairwise ~ sex)
$emmeans
 sex emmean    SE   df lower.CL upper.CL
 0     26.0 0.153 2956     25.7     26.3
 1     25.4 0.109 2956     25.2     25.6
Confidence level used: 0.95 
$contrasts
 contrast    estimate    SE   df t.ratio p.value
 sex0 - sex1    0.662 0.188 2956   3.518  0.0004
> pairs(emmeans(model, pairwise ~ sex))
[1] x
<0 rows> (or 0-length row.names)
Warning message:
In class(rtn) <- c("summary_emm", "data.frame") :
  Setting class(x) to multiple strings ("summary_emm", "data.frame", ...); result will no longer be an S4 object
> contrast(emmeans(model, pairwise ~ sex))
[1] x
<0 rows> (or 0-length row.names)
Warning message:
In class(rtn) <- c("summary_emm", "data.frame") :
  Setting class(x) to multiple strings ("summary_emm", "data.frame", ...); result will no longer be an S4 object

Does that clarify the issue?

rvlenth commented 1 year ago

Note that you already have the results:

emmeans(model, pairwise ~ sex)
$emmeans
sex emmean SE df lower.CL upper.CL
0 26.0 0.153 2956 25.7 26.3
1 25.4 0.109 2956 25.2 25.6
Confidence level used: 0.95

$contrasts
contrast estimate SE df t.ratio p.value
sex0 - sex1 0.662 0.188 2956 3.518 0.0004

When you do contrast(emmeans(model, pairwise ~ sex)), you are asking it to do contrasts of an emm_list object (not an emmGrid object). I don't know why you are doing that, given that you already have the results in a previous step. But if you want to do follow-up contratsts on a emm_list object, you should specify which one you want; e.g.,

lst <- emmeans(model, pairwise ~ sex)
contrast(lst$emmeans)
contrast(lst$contrasts)
camillesouama commented 1 year ago

Great, I was trying to use the contrast() or pairs() function to be able to reverse contrasts in an automatic way. With your help, I am now able to do so. Thanks for your time!

rvlenth commented 1 year ago

You can use revpairwise ~ sex to get the comparison to go the other direction. Better yet, IMHO, is to not conflate means and contrasts

EMM = emmeans(model, ~sex) EMM. # view means contrast(EMM, "revpairwise"). #or whatever

rvlenth commented 1 year ago

I made some changes so that we get results, and don't get the error messages you experienced. Example:

> MOats.lm = lm(formula = yield ~ Block + Variety, data = MOats)
> lst = emmeans(MOats.lm, pairwise ~ Variety)
> lst
$emmeans
 Variety     emmean   SE df lower.CL upper.CL
 Golden Rain  104.5 5.01 10     93.3      116
 Marvellous   109.8 5.01 10     98.6      121
 Victory       97.6 5.01 10     86.5      109

Results are averaged over the levels of: Block, rep.meas 
Confidence level used: 0.95 

$contrasts
 contrast                 estimate   SE df t.ratio p.value
 Golden Rain - Marvellous    -5.29 7.08 10  -0.748  0.7419
 Golden Rain - Victory        6.88 7.08 10   0.971  0.6104
 Marvellous - Victory        12.17 7.08 10   1.719  0.2458

Results are averaged over the levels of: Block, rep.meas 
P value adjustment: tukey method for comparing a family of 3 estimates 

> contrast(lst, "eff")
 contrast           estimate   SE df t.ratio p.value
 Golden Rain effect    0.528 4.09 10   0.129  0.8998
 Marvellous effect     5.819 4.09 10   1.424  0.2774
 Victory effect       -6.347 4.09 10  -1.553  0.2774

Results are averaged over the levels of: Block, rep.meas 
P value adjustment: fdr method for 3 tests