runehaubo / lmerTestR

Repository for the R-package lmerTest
50 stars 9 forks source link

as_lmerModLmerTest fails when lmerMod has been fit in different session #55

Open leatamberg opened 10 months ago

leatamberg commented 10 months ago

Hello,

when I fit a model using the lme4 version of lmer, save it and try to convert it to a lmerModlmerTest object after clearing the environment, I get the following error message:

Error in as_lmerModLmerTest(model_lme4_reloaded) : Unable to extract deviance function from model fit

Here is a minimal reproducible example:

library(lme4)
sleepstudy_copy <- sleepstudy
model_lme4 <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy_copy)
saveRDS(model_lme4, "model_lme4.rds")
rm(list=ls())
model_lme4_reloaded <- readRDS("model_lme4.rds")
library(lmerTest)
model_lmerTest <- as_lmerModLmerTest(model_lme4_reloaded)

I think the reason is that as_lmerModLmerTest tries to retrieve information from the environment no longer available. My workaround is to refit the model directly with lmerTest being loaded, which is however time consuming. If there is no simple solution, it would probably be good to have at least a more meaningful error message.

Thanks a lot in advance :) Cheers, Lea