Extract Fixed Effect and Random Effect in Dataframe

和自甴很熟 提交于 2019-12-11 10:50:38

问题


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.

E.g.

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)

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:

Error in View : cannot coerce class ""ranef.mer"" to a data.frame

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.

Thanks and Regards,


回答1:


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)

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.



来源:https://stackoverflow.com/questions/36136645/extract-fixed-effect-and-random-effect-in-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!