Search code examples
ranovamixed-modelsposthocemmeans

R: post-hoc comparisons using estimated marginal means but not assuming equal variances


I am trying to use R to run post-hoc comparisons following a significant interaction for a mixed-method Anova. I would like to do the post-hoc similar to SPSS [EMMEANS=TABLES(Group*time) COMPARE(Group) ADJ(BONFERRONI)], using estimated marginal means but not assuming equality of variance.

Dependent variable = 'depvar'. I have 3 groups ('group') and 3 time points ('timept'), which are repeated measures over subjects ('id');

aov_car(depvar ~ group*timept + Error(id/(timept)), data=myData)

If I use pairwise.t.test I can compare groups separately for each time point, but the R uses observed means and I do not know how to force using the estimated marginal means of my model:

for (itimept in unique(myData$timept)){
      idx=myData$timept==itimept
      pairwise.t.test(myData$depvar[idx],myData$group[idx],p.adj="bonferroni")
    }

If I use emmeans or lsmeans then R uses estimated marginal means, but assumes variances are the same (the SE in the results are all the same).

myfit=lme(depvar ~ group*timept, random = ~1|id/timept, data=myData)
emmeans(myfit, pairwise~group|timept, adjust="bonferroni")

How do I run post-hoc comparisons between groups for each time point, using estimated marginal means but not assuming equal variances, similar to SPSS?

Thanks! Cristina


Solution

  • It isn’t emmeans that assumes equal variances. It is the model that you fitted and subsequently handed to emmeans for further analysis. Fit a different model using, I think, the weights argument, that specifies unequal variances.

    I believe that this model will do the trick:

    myfit = lme(depvar ~ group*timept, 
                random = ~1|id/timept, 
                weights = varFunc(~ group*timept),
                data = myData)