R: analyzing multiple responses (i.e. dependent variables) in a mixed effects model (lme4)

会有一股神秘感。 提交于 2019-12-04 11:54:24

I would try reshaping your data so that each rating has its own record, and then iterate over those:

library(reshape2)


# This will create a data.frame with one row for each  rating, 
# which are uniquely specified by the characteristic being rated,
# the time point, the perceiver, and the target
# (I think)
x.melt <- melt(x,
               id.var = c("time_point", "perceiver", "target"),
               measure.var = c("Var1", "Var2", "Var3", "Var4",
                               "Var5", "Var6", "Var7")
)


# I'd use plyr to iterate, personally
library(plyr)

# This will return a list containing one model for each combination of variable
# (which are your various outcomes) and time_point
x.models <- dlply(x.melt, .var = c("variable", "time_point"), .fun = function(x) {

    lmer(scale(value) ~ (1|target) + (1|perceiver), data= x))

})


# Which then makes it easy to do things like print summaries for every model
lapply(x.models, summary)

I still think it makes more sense to have time_point as a component in your models, in which case you could just remove it from the .var = c("variable", "time_point") argument and add it to the model specification.

In R, many things get a lot easier when the data is in the right shape. It's extremely worthwhile to learn about the "melting" and "casting" concepts behind the reshape2 package - I don't know how I ever got by without them.

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