lme4 中的多元线性混合模型

发布于 2024-12-11 08:01:20 字数 938 浏览 0 评论 0原文

我想知道如何使用 lme4 拟合多元线性混合模型。我使用以下代码拟合单变量线性混合模型:

library(lme4)
lmer.m1 <- lmer(Y1~A*B+(1|Block)+(1|Block:A), data=Data)
summary(lmer.m1)
anova(lmer.m1)

lmer.m2 <- lmer(Y2~A*B+(1|Block)+(1|Block:A), data=Data)
summary(lmer.m2)
anova(lmer.m2)

我想知道如何使用 lme4 拟合多元线性混合模型。数据如下:

Block A B    Y1    Y2
 1    1 1 135.8 121.6
 1    1 2 149.4 142.5
 1    1 3 155.4 145.0
 1    2 1 105.9 106.6
 1    2 2 112.9 119.2
 1    2 3 121.6 126.7
 2    1 1 121.9 133.5
 2    1 2 136.5 146.1
 2    1 3 145.8 154.0
 2    2 1 102.1 116.0
 2    2 2 112.0 121.3
 2    2 3 114.6 137.3
 3    1 1 133.4 132.4
 3    1 2 139.1 141.8
 3    1 3 157.3 156.1
 3    2 1 101.2  89.0
 3    2 2 109.8 104.6
 3    2 3 111.0 107.7
 4    1 1 124.9 133.4
 4    1 2 140.3 147.7
 4    1 3 147.1 157.7
 4    2 1 110.5  99.1
 4    2 2 117.7 100.9
 4    2 3 129.5 116.2

预先感谢您的时间与合作。

I wonder how to fit multivariate linear mixed model with lme4. I fitted univariate linear mixed models with the following code:

library(lme4)
lmer.m1 <- lmer(Y1~A*B+(1|Block)+(1|Block:A), data=Data)
summary(lmer.m1)
anova(lmer.m1)

lmer.m2 <- lmer(Y2~A*B+(1|Block)+(1|Block:A), data=Data)
summary(lmer.m2)
anova(lmer.m2)

I'd like to know how to fit multivariate linear mixed model with lme4. The data is below:

Block A B    Y1    Y2
 1    1 1 135.8 121.6
 1    1 2 149.4 142.5
 1    1 3 155.4 145.0
 1    2 1 105.9 106.6
 1    2 2 112.9 119.2
 1    2 3 121.6 126.7
 2    1 1 121.9 133.5
 2    1 2 136.5 146.1
 2    1 3 145.8 154.0
 2    2 1 102.1 116.0
 2    2 2 112.0 121.3
 2    2 3 114.6 137.3
 3    1 1 133.4 132.4
 3    1 2 139.1 141.8
 3    1 3 157.3 156.1
 3    2 1 101.2  89.0
 3    2 2 109.8 104.6
 3    2 3 111.0 107.7
 4    1 1 124.9 133.4
 4    1 2 140.3 147.7
 4    1 3 147.1 157.7
 4    2 1 110.5  99.1
 4    2 2 117.7 100.9
 4    2 3 129.5 116.2

Thank in advance for your time and cooperation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ˇ宁静的妩媚 2024-12-18 08:01:20

有时可以在 nlme/lme4 中通过简单地重新格式化数据

require(reshape)
Data = melt(data, id.vars=1:3, variable_name='Y')
Data$Y = factor(gsub('Y(.+)', '\\1', Data$Y))
> Data
  Block A B Y value
1     1 1 1 1 135.8
2     1 1 2 1 149.4
3     1 1 3 1 155.4
4     1 2 1 1 105.9
5     1 2 2 1 112.9
6     1 2 3 1 121.6
...

,然后在线性混合模型中包含新变量 Y 来令人满意地伪造这一点。

但是,对于真正的多元广义线性混合模型 (MGLMM),您可能需要 sabreR 包或类似包。该软件包还附带一整本书,使用 R 的多元广义线性混合模型。如果您有订阅机构的代理,您甚至可以从 http://www.crcnetbase.com 免费下载/isbn/9781439813270。我会向您推荐任何进一步的建议,因为这是一个内容丰富的话题,而且我是一个新手。

This can sometimes be faked satisfactorily in nlme/lme4 by simply reformatting your data like

require(reshape)
Data = melt(data, id.vars=1:3, variable_name='Y')
Data$Y = factor(gsub('Y(.+)', '\\1', Data$Y))
> Data
  Block A B Y value
1     1 1 1 1 135.8
2     1 1 2 1 149.4
3     1 1 3 1 155.4
4     1 2 1 1 105.9
5     1 2 2 1 112.9
6     1 2 3 1 121.6
...

and then including the new variable Y in your linear mixed model.

However, for true Multivariate Generalized Linear Mixed Models (MGLMM), you will probably need the sabreR package or similar. There is also an entire book to accompany the package, Multivariate Generalized Linear Mixed Models Using R. If you have a proxy to a subscribing institution, you might even be able to download it for free from http://www.crcnetbase.com/isbn/9781439813270. I would refer you there for any further advice, as this is a meaty topic and I am very much a novice.

睫毛溺水了 2024-12-18 08:01:20

lmer 及其兄长 lme 本质上是“~ 左边的一个参数”。看看汽车包装;它不提供现成的重复测量支持,但您可以通过搜索 R 列表找到有关该主题的一些评论:

约翰·福克斯在汽车套餐

lmer and its elder sibling lme are inherently "one parameter left of ~". Have a look at the car packages; it offers no off-the shelf repeated measurement support, but you will find a few comments on the subject by searching the R list:

John Fox on car package

‖放下 2024-12-18 08:01:20

@John 上面的答案应该基本上是正确的。您向模型添加一个虚拟变量(即因子变量Y)。这里有 3 个下标 i= 1...N 用于观察,j=1,...,4 用于块,以及 h=1,2 为依赖变量。但您还需要将 1 级误差项强制为 0(或接近于零),我不确定 lme4 是否会这样做。本·博尔克可能会提供更多信息。 Goldstein (2011) 第 6 章和第 7 章针对潜在多元模型对此进行了更多描述。

Y_hij = \beta_{01} z_{1ij} + \beta_{02} z_{2ij} + \beta X + u_{1j} z_{1ij} + u_{2j} z_{2ij}

所以:

require(reshape2)
Data = melt(data, id.vars=1:3, variable_name='Y')
Data$Y = factor(gsub('Y(.+)', '\\1', Data$Y))

m1 <- lmer(value ~ Y + A*B + (1|Block) + (1|Block*A), data= Data)
# not sure how to set the level 1 variance to 0, @BenBolker
# also unclear to me if you're requesting Y*A*B instead of Y + A*B

@John's answer above should be largely right. You add a dummy variable (ie--the factor variable Y) to the model. Here you have 3 subscripts i= 1...N for observations, j=1,...,4 for blocks, and h=1,2 for the dependent var. But you also need to force the level 1 error term to 0 (or to near zero), which I'm not sure lme4 does. Ben Bolker might provide more information. This is described more in Goldstein (2011) Chap 6 and Chap 7 for latent multivariate models.

IE

Y_hij = \beta_{01} z_{1ij} + \beta_{02} z_{2ij} + \beta X + u_{1j} z_{1ij} + u_{2j} z_{2ij}

So:

require(reshape2)
Data = melt(data, id.vars=1:3, variable_name='Y')
Data$Y = factor(gsub('Y(.+)', '\\1', Data$Y))

m1 <- lmer(value ~ Y + A*B + (1|Block) + (1|Block*A), data= Data)
# not sure how to set the level 1 variance to 0, @BenBolker
# also unclear to me if you're requesting Y*A*B instead of Y + A*B
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文