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.
来源:https://stackoverflow.com/questions/20525633/r-analyzing-multiple-responses-i-e-dependent-variables-in-a-mixed-effects-mo