问题描述
我正在使用 lme4 包来运行混合模型.我想在单独的数据集中提取固定效应结果和随机效应结果,以便我们可以将其用于进一步分析.但不幸的是我不能.
I'm using lme4 package to run mixed model. I want to extract fixed effect result and random effect result in seperate dataset, so that we can use it for further analysis. But unfortunately I could not.
例如
mixed_result<- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
我尝试使用以下方法提取固定效应和随机效应:
I tried to extract fixed effect and random effect using the following method:
fixEffect<-fixef(mixed_result)
randEffect<-ranef(mixed_result)
View(fixEffect)
我分别针对固定效应和随机效应尝试了 fixef 和 ranef,并尝试使用它的结果创建数据集.但它给了我以下错误:
I tried fixef and ranef for fixed effect and random effect respectively and try to create the dataset using the result of it. But it was giving me the following error:
视图中的错误:无法将ranef.mer"类强制转换为 data.frame
我实际上想要我们在 SAS、solutionF 和 solutionR 中获得的输出.但如果不可能得到这样的输出,固定和随机的系数就可以了.
I actually want output as we get in SAS , solutionF and solutionR. But in case if it's not possible to get output like that, the coeffs of fixed and random will do.
如果有人能帮助我,我将不胜感激.
I'll be grateful if someone can help me.
谢谢和问候,
推荐答案
使用 str
查看对象的结构.
Use str
to see the structure of an object.
str(fixEffect)
# named vector, can probably be coerced to data.frame
View(as.data.frame(fixEffect))
# works just fine
str(randEffect)
# list of data frames (well, list of one data frame in this case)
View(randEffect$Subject)
如果你有,比如说,斜率也随 Subject
而变化,它们将进入与 Subject
级别相同的 Subject
数据框拦截.但是,如果拦截也因某些其他变量group
而变化,与Subject
的级别数不同,则它们显然不能进入同一数据帧.这就是使用数据框列表的原因,以便相同的结构可以推广到更复杂的模型.
If you had, say, slopes that also varied by Subject
, they would go in the same Subject
data frame as the Subject
level intercepts. However, if intercepts also varied by some other variable group
, with a different number of level than Subject
, they obviously couldn't go in the same data frame. This is why a list of data frames is used, so that the same structure can generalize up for more complex models.
这篇关于提取数据帧中的固定效应和随机效应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!