对 R 中的混合效应模型重复测量方差分析和 link
Repeated measures ANOVA and link to mixed-effect models in R
我在 R 中对以下数据执行双向 rm 方差分析时遇到问题 (link : https://drive.google.com/open?id=1nIlFfijUm4Ib6TJoHUUNeEJnZnnNzO29):
subjectnbr is the id of the subject and blockType and linesTTL are the independent variables. RT2 is the dependent variable
我首先使用 ezANOVA 执行 rm 方差分析,代码如下:
ANOVA_RTS <- ezANOVA(
data=castRTs
, dv=RT2
, wid=subjectnbr
, within = .(blockType,linesTTL)
, type = 2
, detailed = TRUE
, return_aov = FALSE
)
ANOVA_RTS
结果是正确的(我用statistica仔细检查过)。
但是,当我使用 lme 函数执行 rm 方差分析时,我没有得到相同的答案,我也不知道为什么。
这是我的代码:
lmeRTs <- lme(
RT2 ~ blockType*linesTTL,
random = ~1|subjectnbr/blockType/linesTTL,
data=castRTs)
anova(lmeRTs)
这里是the outputs of both ezANOVA and lme。
我希望我已经足够清楚并且已经为您提供了所有需要的信息。
期待您的帮助,因为我至少花了 4 个小时才弄明白!
提前致谢。
这是一个关于如何使用 nlme::lme
.
重现 ezANOVA
结果的分步示例
数据
我们读入数据并确保所有分类变量都是 factor
s。
# Read in data
library(tidyverse);
df <- read.csv("castRTs.csv");
df <- df %>%
mutate(
blockType = factor(blockType),
linesTTL = factor(linesTTL));
结果来自 ezANOVA
作为检查,我们重现 ez::ezANOVA
结果。
## ANOVA using ez::ezANOVA
library(ez);
model1 <- ezANOVA(
data = df,
dv = RT2,
wid = subjectnbr,
within = .(blockType, linesTTL),
type = 2,
detailed = TRUE,
return_aov = FALSE);
model1;
# $ANOVA
# Effect DFn DFd SSn SSd F p
#1 (Intercept) 1 13 2047405.6654 34886.767 762.9332235 6.260010e-13
#2 blockType 1 13 236.5412 5011.442 0.6136028 4.474711e-01
#3 linesTTL 1 13 6584.7222 7294.620 11.7348665 4.514589e-03
#4 blockType:linesTTL 1 13 1019.1854 2521.860 5.2538251 3.922784e-02
# p<.05 ges
#1 * 0.976293831
#2 0.004735442
#3 * 0.116958989
#4 * 0.020088855
结果来自 nlme::lme
我们现在运行nlme::lme
## ANOVA using nlme::lme
library(nlme);
model2 <- anova(lme(
RT2 ~ blockType * linesTTL,
random = list(subjectnbr = pdBlocked(list(~1, pdIdent(~blockType - 1), pdIdent(~linesTTL - 1)))),
data = df))
model2;
# numDF denDF F-value p-value
#(Intercept) 1 39 762.9332 <.0001
#blockType 1 39 0.6136 0.4382
#linesTTL 1 39 11.7349 0.0015
#blockType:linesTTL 1 39 5.2538 0.0274
Results/conclusion
我们可以看到两种方法的F检验结果完全相同。 lme
中 random
效应定义的结构有些复杂,这是因为您有 两个 交叉随机效应。这里“交叉”意味着对于 blockType
和 linesTTL
的每个组合,每个 subjectnbr
.
都存在一个观察值
一些额外的(可选)详细信息
要理解pdBlocked
和pdIdent
的作用我们需要看一下对应的二级混合效应模型
预测变量 是您的分类变量 blockType
和 linesTTL
,它们通常使用虚拟变量进行编码。
随机效应的方差-协方差矩阵 可以采用不同的形式,具体取决于随机效应系数的潜在相关结构。为了与两级重复测量方差分析的假设一致,我们必须指定块对角方差协方差矩阵 pdBlocked
,我们在其中为偏移量 ~1
和分类创建对角块预测变量 blockType
pdIdent(~blockType - 1)
和 linesTTL
pdIdent(~linesTTL - 1)
。请注意,我们需要从最后两个块中减去偏移量(因为我们已经考虑了偏移量)。
一些relevant/interesting资源
Pinheiro and Bates, Mixed-Effects Models in S and S-PLUS, Springer (2000)
我在 R 中对以下数据执行双向 rm 方差分析时遇到问题 (link : https://drive.google.com/open?id=1nIlFfijUm4Ib6TJoHUUNeEJnZnnNzO29):
subjectnbr is the id of the subject and blockType and linesTTL are the independent variables. RT2 is the dependent variable
我首先使用 ezANOVA 执行 rm 方差分析,代码如下:
ANOVA_RTS <- ezANOVA(
data=castRTs
, dv=RT2
, wid=subjectnbr
, within = .(blockType,linesTTL)
, type = 2
, detailed = TRUE
, return_aov = FALSE
)
ANOVA_RTS
结果是正确的(我用statistica仔细检查过)。
但是,当我使用 lme 函数执行 rm 方差分析时,我没有得到相同的答案,我也不知道为什么。
这是我的代码:
lmeRTs <- lme(
RT2 ~ blockType*linesTTL,
random = ~1|subjectnbr/blockType/linesTTL,
data=castRTs)
anova(lmeRTs)
这里是the outputs of both ezANOVA and lme。
我希望我已经足够清楚并且已经为您提供了所有需要的信息。
期待您的帮助,因为我至少花了 4 个小时才弄明白!
提前致谢。
这是一个关于如何使用 nlme::lme
.
ezANOVA
结果的分步示例
数据
我们读入数据并确保所有分类变量都是 factor
s。
# Read in data
library(tidyverse);
df <- read.csv("castRTs.csv");
df <- df %>%
mutate(
blockType = factor(blockType),
linesTTL = factor(linesTTL));
结果来自 ezANOVA
作为检查,我们重现 ez::ezANOVA
结果。
## ANOVA using ez::ezANOVA
library(ez);
model1 <- ezANOVA(
data = df,
dv = RT2,
wid = subjectnbr,
within = .(blockType, linesTTL),
type = 2,
detailed = TRUE,
return_aov = FALSE);
model1;
# $ANOVA
# Effect DFn DFd SSn SSd F p
#1 (Intercept) 1 13 2047405.6654 34886.767 762.9332235 6.260010e-13
#2 blockType 1 13 236.5412 5011.442 0.6136028 4.474711e-01
#3 linesTTL 1 13 6584.7222 7294.620 11.7348665 4.514589e-03
#4 blockType:linesTTL 1 13 1019.1854 2521.860 5.2538251 3.922784e-02
# p<.05 ges
#1 * 0.976293831
#2 0.004735442
#3 * 0.116958989
#4 * 0.020088855
结果来自 nlme::lme
我们现在运行nlme::lme
## ANOVA using nlme::lme
library(nlme);
model2 <- anova(lme(
RT2 ~ blockType * linesTTL,
random = list(subjectnbr = pdBlocked(list(~1, pdIdent(~blockType - 1), pdIdent(~linesTTL - 1)))),
data = df))
model2;
# numDF denDF F-value p-value
#(Intercept) 1 39 762.9332 <.0001
#blockType 1 39 0.6136 0.4382
#linesTTL 1 39 11.7349 0.0015
#blockType:linesTTL 1 39 5.2538 0.0274
Results/conclusion
我们可以看到两种方法的F检验结果完全相同。 lme
中 random
效应定义的结构有些复杂,这是因为您有 两个 交叉随机效应。这里“交叉”意味着对于 blockType
和 linesTTL
的每个组合,每个 subjectnbr
.
一些额外的(可选)详细信息
要理解pdBlocked
和pdIdent
的作用我们需要看一下对应的二级混合效应模型
预测变量 blockType
和 linesTTL
,它们通常使用虚拟变量进行编码。
随机效应的方差-协方差矩阵 pdBlocked
,我们在其中为偏移量 ~1
和分类创建对角块预测变量 blockType
pdIdent(~blockType - 1)
和 linesTTL
pdIdent(~linesTTL - 1)
。请注意,我们需要从最后两个块中减去偏移量(因为我们已经考虑了偏移量)。
一些relevant/interesting资源
Pinheiro and Bates, Mixed-Effects Models in S and S-PLUS, Springer (2000)