rvlenth / emmeans

Estimated marginal means
https://rvlenth.github.io/emmeans/
358 stars 31 forks source link

Issue with `emmeans` when passing a `vcov` matrix for `lme` fit #381

Closed iago-pssjd closed 1 year ago

iago-pssjd commented 1 year ago

Let's see

library(nlme)
library(emmeans)
library(clubSandwich)
Oats.lme <- lme(yield ~ Variety + factor(nitro),  random = ~ 1 | Block / Variety, data = Oats)
vcCR <- vcovCR(Oats.lme, type = "CR2")
emmV <- emmeans(Oats.lme, ~ Variety, vcov. = vcCR)
Error in validObject(.Object) : 
  invalid class “emmGrid” object: invalid object for slot "V" in class "emmGrid": got class "vcovCR", should be or extend class "matrix"

However next works

library(lme4)
Oats.lmer <- lmer(yield ~ Variety + factor(nitro) +(1 | Block / Variety), data = Oats)
vcCRr <- vcovCR(Oats.lmer, type = "CR2")
emmeans(Oats.lmer, ~ Variety, vcov. = vcCRr, lmer.df = "satterthwaite")
 Variety     emmean   SE   df lower.CL upper.CL
 Golden Rain  104.5 7.11 8.87     88.4      121
 Marvellous   109.8 6.90 8.87     94.2      125
 Victory       97.6 9.18 8.87     76.8      118

Results are averaged over the levels of: nitro 
Degrees-of-freedom method: satterthwaite 
Confidence level used: 0.95 
rvlenth commented 1 year ago

Why do you think this is a bug? And please note that the argument name is vcov., with a dot at the end.

iago-pssjd commented 1 year ago

Thanks for pointing that the argument name is vcov., the behaviour is the same. It is a bug because the documentation says covariance matrices are allowed satisfying that they are

square and of the same dimension and parameter order of the fixed effects. If a function, must return a suitable matrix when it is called with argument

and such is the argument. The problem is that this object (by clubSandwich::vcovCR) has an invalid class. However, in the code for lmer models, the class is modified by means of as.matrix