r - run multiple model after grouping and save as columns in dataframe -
i'm trying run multiple mix effect models on grouped data. here codes:
library(dplyr) library(lme4) dat.tx.a <- mvrnorm(n=250, mu=c(30, 20, 28), sigma=matrix(c(25.0, 17.5, 12.3, 17.5, 25.0, 17.5, 12.3, 17.5, 25.0), nrow=3, byrow=true)) dat.tx.b <- mvrnorm(n=250, mu=c(30, 20, 22), sigma=matrix(c(25.0, 17.5, 12.3, 17.5, 25.0, 17.5, 12.3, 17.5, 25.0), nrow=3, byrow=true)) dat <- data.frame(rbind(dat.tx.a, dat.tx.b)) names(dat) = c("measure.1", "measure.2", "measure.3") dat <- data.frame(subject.id=factor(1:500), tx=rep(c("a", "b"), each=250), dat) rm(dat.tx.a, dat.tx.b) dat <- reshape(dat, varying=c("measure.1", "measure.2", "measure.3"), idvar="subject.id", direction="long") df.model<-dat %>% group_by(tx) %>% (model1=lmer(measure~(1|subject.id), data=.)) %>% (model2=lmer(measure~time+(1|subject.id), data=.))
here error message:
error in as.data.frame.default(x[[i]], optional = true) : cannot coerce class "structure("lmermod", package = "lme4")" data.frame
the code works if remove line runs model2. seems dataframe accept 1 column list of models? lot.
just pass multiple models do()
:
df.model<-dat %>% group_by(tx) %>% (model1=lmer(measure~(1|subject.id), data=.), model2=lmer(measure~time+(1|subject.id), data=.))
also note in example using mass
package.
Comments
Post a Comment