singmann / afex

Analysis of Factorial EXperiments (R package)
119 stars 32 forks source link

emmeans gives wrong results for afex_aovs if the at = argument is reversed #123

Closed Joao-O-Santos closed 1 year ago

Joao-O-Santos commented 1 year ago

I submitted this bug in the emmeans package repo, but it seems some times the authors of packages implement emmeans functionality in their own packages.

I don't think that is the case with afex but I opening this report just in case there's something you can do on your end.

Cross-posted from the emmeans project repo:

Describe the bug

When I run emmeans(model, ~ Var, at = list(Var = c("levelx", "levely"))) the results get switched for afex_aovs if I place levelx and levely in a different order, but this doesn't happen for models of class afex_mixed, and it probably won't happen for other models.

To reproduce

library(emmeans)

df <- data.frame(a = rnorm(300), b = rnorm(300, 3), pp = 1:300)
df <- tidyr::pivot_longer(df, -"pp")
afex_aov <- afex::aov_4(value ~ name + (name | pp), df)
afex_mixed <- afex::mixed(value ~ name + (1 | pp), df)

ordered <- list(name = c("a", "b"))
reversed <- list(name = c("b", "a")

Expected behavior

If we list the factors in the order they appear we always get correct results.

emmeans(afex_aov, ~ name, at = ordered)
emmeans(afex_mixed, ~ name, at = ordered)

If we reverse the names of the factors we get wrong values in for the model of class afex_aov.

emmeans(afex_aov, ~ name, at = reversed)
emmeans(afex_mixed, ~ name, at = reversed)

How can I help?

Please let me know if there's anything I can do about this matter.

Sincerely, J

Joao-O-Santos commented 1 year ago

The emmeans author has already replied and is working on this. I'm going to close it as it is not an afex bug.