能否将"mixed"对象转换为"merModLmerTest"对象以计算GLMM的$R^2$?
mixed 对象计算Nakagawa & Schielzeth (2013) $R^2$ 的问题 Great question! The mixed objects from the afex package (the most common source of this object type) don’t require forced conversion—they actually wrap a merModLmerTest object under the hood that you can access directly. Here’s how to handle this:
提取底层的
merModLmerTest对象
Themixed()function returns a list-like object where the full fitted model is stored in thefull_modelslot. You can access it like this:# 假设你的mixed对象名为my_mixed_model lmer_test_model <- my_mixed_model$full_modelNow you can run
fixef(lmer_test_model)andgetME(lmer_test_model, c("theta", "beta"))just like you would with a standardmerModLmerTestobject.更简便的方法:直接用
performance包计算$R^2$
You don’t even need to extract the model manually. Theperformancepackage’sr2_nakagawa()function natively supportsafexmixedobjects, so you can compute the marginal and conditional $R^2$ in one line:library(performance) r2_nakagawa(my_mixed_model)This function implements the Nakagawa & Schielzeth (2013) method directly, so it saves you from writing custom code with
fixef()andgetME().如果确实需要强制转换(一般没必要)
In rare cases where the slot access doesn’t work (unlikely with standardafexusage), you can try coercing the object withas():lmer_test_model <- as(my_mixed_model, "merModLmerTest")But again, the
$full_modelapproach is the intended and more reliable way to access the underlying model.
内容的提问来源于stack exchange,提问作者KrisAnathema

