楼主: Limdep
1996 2

Using a matrix of predictors with lmer() [推广有奖]

  • 0关注
  • 2粉丝

已卖:117份资源

本科生

98%

还不是VIP/贵宾

-

TA的文库  其他...

Java资源全汇

Data Science NewOccidental

Database NewOccidental

威望
0
论坛币
4718 个
通用积分
4.2550
学术水平
8 点
热心指数
3 点
信用等级
3 点
经验
1089 点
帖子
133
精华
0
在线时间
20 小时
注册时间
2006-5-15
最后登录
2017-10-27

楼主
Limdep 发表于 2014-5-7 02:30:34 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

I am attempting to fit a model with a large number of predictors, such that it would be tedious to enumerate them in a model formula. This is straightforward to do with lm():


indicatorMatrix <- data.frame(matrix(rbinom(26000, 1, 1/3), ncol = 26))

colnames(indicatorMatrix) <- LETTERS

someDV <- rnorm(nrow(indicatorMatrix))


head(indicatorMatrix)


# One method, enumerating variables by name:

olsModel1 <- lm(someDV ~ A + B + C + D,  # ...etc.

               data = indicatorMatrix)


# Preferred method, including the matrix of predictors:

olsModel2 <- lm(someDV ~ as.matrix(indicatorMatrix))

summary(olsModel2)

Since I have a very large number of predictors (more than the 26 in this invented example), I don't want to list them individually as in the first example (someDV ~ A + B + C + D...), and I can avoid this by just including the predictors as.matrix.


However, I want to fit a mixed effects model, like this:


library(lme4)

meModel1 <- lmer(someDV ~ (1 | A) + (1 | B) + (1 | C),  # ...etc.

                 data = indicatorMatrix)

summary(meModel1)

Except that I want to include a large number of random effects terms. Rather than having to type (1 | A) ... (1 | ZZZ), I would like to include each predictor in a manner analogous to the matrix approach used for olsModel2 above. The following, obviously, does not work:


meModel2 <- lmer(someDV ~ (1 | as.matrix(indicatorMatrix)))

Do you have any suggestions for how I can best replicate the matrix-predictor approach for random effects with lmer()? I am very willing to consider "pragmatic" solutions (i.e. hacks), so long as they are "programmatic," and don't require me to copy & paste, etc. etc.


Thanks in advance for your time.

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Predictors predictor predict matrix Using matrix

沙发
Limdep 发表于 2014-5-7 02:31:48
I think that constructing the formula as a string and then using as.formula, something along the lines of

restring1 <- paste0("(1 | ",colnames(indicatorMatrix),")",collapse="+")
form <- as.formula(paste0("someDV ~",restring1))
meModel1 <- lmer(form, data = data.frame(someDV,indicatorMatrix))
should work (it runs without complaining on my system, anyway ...)

藤椅
Limdep 发表于 2014-5-7 02:32:22
You could also use reformulate(restring1, response = 'someDV')

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 03:17