R:在混合效应模型(lme4)中分析多个响应(即因变量) [英] R: analyzing multiple responses (i.e. dependent variables) in a mixed effects model (lme4)

查看:502
本文介绍了R:在混合效应模型(lme4)中分析多个响应(即因变量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我认为非常简单的问题.在与一组参与者进行的纵向实验中,每个人都在7个不同的时间对其他所有人进行了评分,例如10个变量(例如此人很可爱",此人很钝"等). 如果我想获得一个变量/响应的某种感知器和目标方差,我会使用:

lmer(scale(Var1) ~ (1|target) + (1|perceiver), data= subset(x, time_point == 1))

这里有一个数据帧"x"的因变量"Var1",其规格为第一个time_point(也是x的变量).

到目前为止,效果很好.

现在,正如我所说,我有多个回答和多个时间点.因此,我想使用a)一个"for"循环,或b)一次应用,以一次获得所有模型.

无论哪种方式,我都必须以某种方式索引"因变量,无论是指定列位置(x[,10],其中10是Var1的假定位置)还是变量本身(x$Var1)或(位于至少有点奇怪)将变量的名称粘贴或打印到公式(col.names(c[10])中.

我要说的是,这两个都不起作用.对于不同的变量长度,我总是会出现错误.但是,正如我所写,我正在使用完全相同的列!

你们中的任何人都有运行多个lmers的经验吗?

欢迎并赞赏所有想法!我希望我不太清楚,如果您需要任何进一步的信息,我将竭诚为您提供.

干杯, 铝

解决方案

我会尝试重塑您的数据,以便每个评分都有自己的记录,然后遍历这些记录:

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)

我仍然认为在模型中将time_point作为组件更有意义,在这种情况下,您可以将其从.var = c("variable", "time_point")参数中删除并将其添加到模型规范中. /p>

在R中,数据格式正确时,许多事情变得容易得多. 非常值得学习reshape2软件包背后的融化"和铸造"概念-我不知道没有它们我怎么过.

I have a, what I thought, really simple question. In a longitudinal experiment with a group of participants has everyone rated everyone else on, let's say, 10 variables (e.g. "This person is likeable.", "This person is dull." and so on) at 7 different times. If i want to get some sort of perceiver and target variance for one variable/response I'd use:

lmer(scale(Var1) ~ (1|target) + (1|perceiver), data= subset(x, time_point == 1))

Here we have a dependent variable "Var1" of a dataframe "x" with the specification of the 1st time_point (which is also a variable of x).

So far so good, this works just fine.

Now as I said, I have multiple responses and multiple time points. Therefore I wanted to use a) a "for"-loop, or b) lapply, to get all the models at once.

Either way, I have to somehow "index" the dependent variable, be it specifying the column position (x[,10] with 10 being the assumed position of Var1) or the variable itself (x$Var1) or (which is at least a little odd) paste or print the name of the Variable into the formula (col.names(c[10]).

What I am trying to say is, neither of this does work. I always get an error about differing variable lengths. But, as I wrote, I am using the exact same columns!

Does anyone of you have experience with running multiple lmers?

All ideas are welcome and appreciated! I hope I was not too unclear, if you need any further information, I'd be happy to provide, as far as I can.

Cheers, Al

解决方案

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.

这篇关于R:在混合效应模型(lme4)中分析多个响应(即因变量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆