Open simoncolumbus opened 1 year ago
Hi We should consider that estimating a linear model takes a little more than running a command. GAMLj does use lme4::glmer() to estimate the coefficients of the generalized mixed model, but it prepares the data to yield a sensible model. The differences you observed are due to the fact that in R the user is responsible to prepare the data, whereas in GAMLj the data are prepared accordingly to some default (https://gamlj.github.io/)
Notice, for instance, that the ANOVA-like model you ran with y ~ 1 + x1*x2 + (1|grp) will not yield the classical main effects and interaction, but would yield two simple effects and an interaction. To obtain main effects, you should code x1 and x2 as factors, and center their codes.
To obtain the same results in GAMLj and lme4::glmer() one should simply be sure that the variables are defined in the same way before each command is executed. Here is an example with your data
data$grp<-factor(data$grp)
data$x1<-factor(data$x1)
contrasts(data$x1)<- -contr.sum(2)/2
data$x2<-factor(data$x2)
contrasts(data$x2)<- -contr.sum(2)/2
gamlj::gamljGlmMixed(y ~ 1 + x1*x2 + (1|grp),
data = data,
modelSelection = "custom",
custom_family = "binomial",
custom_link = "logit")
lme4::glmer(y ~ 1 + x1*x2 + (1|grp),
data = data,
family = binomial(link = 'logit'))
Thanks a lot for this explanation, Marcello. That was very helpful!
I have fitted some equivalent models with gamlj::gamljGlmMixed() and with lme4::glmer(). I was under the assumption that gamljGlmMixed() was a wrapper for glmer() and would produce equivalent results, but this is not the case. I am trying to understand what produces these differences. Is it possible to see the exact glmer() input gamljGlmMixed() produces?
Minimum working example with some model configurations below.