R中混合模型(lme4)错误中的单级变量
set.seed(1234)
mydata <- data.frame (
individual = factor(1:10),
M1a = factor (sample (c(1,2),10, replace = T)),
M1b = factor (sample (c(1,2),10, replace = T)),
pop = factor (c(rep(1, 5), rep (2, 5))),
yld = rnorm(10, 10, 2))
这里M1a、M1b是固定的,但个体是随机的。
require(lme4)
model1 <- lmer(yld ~ M1a + M1b + pop + (1|individual), data = mydata)
model1
Error in function (fr, FL, start, REML, verbose) :
Number of levels of a grouping factor for the random effects
must be less than the number of observations
我们可以在 lme4 中执行此操作吗?这些被称为动物模型,asrmel 可以做一些这样的事情(l墨水)。
编辑:我忘记提及需要关系矩阵。以下是这样做的谱系结构。为了使示例适合大小,我将样本大小减少到 10。
peddf <- data.frame (individual = factor(1:10),
mother = c(NA, NA, NA, 1, 1, 1, 1,3, 3,3),
father = c(NA, NA, NA, 2, 2, 2, 2, 2, 2, 2))
individual mother father
1 1 NA NA
2 2 NA NA
3 3 NA NA
4 4 1 2
5 5 1 2
6 6 1 2
7 7 1 2
8 8 3 2
9 9 3 2
10 10 3 2
就矩阵而言,如下所示(仅显示下半三角形加对角线):
1 NA NA NA NA NA NA NA NA NA
0 1 NA NA NA NA NA NA NA NA
0 0 1 NA NA NA NA NA NA NA
0.25 0.25 0 1 NA NA NA NA NA NA
0.25 0.25 0 0.25 1 NA NA NA NA NA
0.25 0.25 0 0.25 0.25 1 NA NA NA NA
0.25 0.25 0 0.25 0.25 0.25 1 NA NA NA
0 0.25 0.25 0.125 0.125 0.125 0.125 1 NA NA
0 0.25 0.25 0.125 0.125 0.125 0.125 0.25 1 NA
0 0.25 0.25 0.125 0.125 0.125 0.125 0.25 0.25 1
以图片形式:
set.seed(1234)
mydata <- data.frame (
individual = factor(1:10),
M1a = factor (sample (c(1,2),10, replace = T)),
M1b = factor (sample (c(1,2),10, replace = T)),
pop = factor (c(rep(1, 5), rep (2, 5))),
yld = rnorm(10, 10, 2))
Here M1a, M1b are fixed however individual is random.
require(lme4)
model1 <- lmer(yld ~ M1a + M1b + pop + (1|individual), data = mydata)
model1
Error in function (fr, FL, start, REML, verbose) :
Number of levels of a grouping factor for the random effects
must be less than the number of observations
Can we do this in lme4. These are known as animal model and asrmel can do some of such things (link).
EDITS: I forget to mention the relationship matrix is rquired. The following is pedigree structure to do so. To fit the example to size, I reduce the sample size to 10.
peddf <- data.frame (individual = factor(1:10),
mother = c(NA, NA, NA, 1, 1, 1, 1,3, 3,3),
father = c(NA, NA, NA, 2, 2, 2, 2, 2, 2, 2))
individual mother father
1 1 NA NA
2 2 NA NA
3 3 NA NA
4 4 1 2
5 5 1 2
6 6 1 2
7 7 1 2
8 8 3 2
9 9 3 2
10 10 3 2
In term of matrix is following (only lowerhalf triangle plus diagnonal is shown):
1 NA NA NA NA NA NA NA NA NA
0 1 NA NA NA NA NA NA NA NA
0 0 1 NA NA NA NA NA NA NA
0.25 0.25 0 1 NA NA NA NA NA NA
0.25 0.25 0 0.25 1 NA NA NA NA NA
0.25 0.25 0 0.25 0.25 1 NA NA NA NA
0.25 0.25 0 0.25 0.25 0.25 1 NA NA NA
0 0.25 0.25 0.125 0.125 0.125 0.125 1 NA NA
0 0.25 0.25 0.125 0.125 0.125 0.125 0.25 1 NA
0 0.25 0.25 0.125 0.125 0.125 0.125 0.25 0.25 1
In picture form:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在扩展亚伦所说的内容,因此所有功劳都应归功于亚伦的回答。
# 无关联结构
# 有 A 矩阵
I am expanding what Aaron said, so all credit should go to the Aaron's answer.
# without relatedness structure
# with A matrix
尝试基于
nlme
的kinship
包。请参阅 r-sig- 上的此帖子混合模型的详细信息。对于非正常响应,您需要修改
lme4
和pedigreemm
包;有关详细信息,请参阅此问题。Try the
kinship
package, which is based onnlme
. See this thread on r-sig-mixed-models for details.For non-normal responses, you'd need to modify
lme4
and thepedigreemm
package; see this question for details.