You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否将"mixed"对象转换为"merModLmerTest"对象以计算GLMM的$R^2$?

解决AFEX 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对象
    The mixed() function returns a list-like object where the full fitted model is stored in the full_model slot. You can access it like this:

    # 假设你的mixed对象名为my_mixed_model
    lmer_test_model <- my_mixed_model$full_model
    

    Now you can run fixef(lmer_test_model) and getME(lmer_test_model, c("theta", "beta")) just like you would with a standard merModLmerTest object.

  • 更简便的方法:直接用performance包计算$R^2$
    You don’t even need to extract the model manually. The performance package’s r2_nakagawa() function natively supports afex mixed objects, 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() and getME().

  • 如果确实需要强制转换(一般没必要)
    In rare cases where the slot access doesn’t work (unlikely with standard afex usage), you can try coercing the object with as():

    lmer_test_model <- as(my_mixed_model, "merModLmerTest")
    

    But again, the $full_model approach is the intended and more reliable way to access the underlying model.

内容的提问来源于stack exchange,提问作者KrisAnathema

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:06:16