R为重复测量方差分析,变量内为2

  • 本文关键字:变量 方差 测量 r anova posthoc
  • 更新时间 :
  • 英文 :


假设我有一个全因子设计,有30个参与者(ppt_id),每个参与者都在4种条件下进行了测试:

(左侧+蓝色)AND(左侧+红色)AND(右侧+蓝色)AND(右侧+红色)

我的因变量是反应时间(RT)

df = matrix(nrow = 120, ncol = 0) 
df = as.data.frame(df)
df$ppt_id = c(rep(c(1:30),4))
df$color = c(rep("red", 60), rep("blue", 60))
df$side = c(rep(c(rep("left", 30), rep("right", 30)),2))
df$RT = c(rnorm(30, 600, 50),
rnorm(30, 650, 50),
rnorm(30, 700, 50),
rnorm(30, 600, 50))

现在,我计算rmANOVA,发现因素Side和Color之间存在显著的交互作用:

anova_test(
data = df, 
dv = RT, 
wid = ppt_id,
within = c(color, side),
effect.size = "pes")

我如何在R中执行所有条件彼此之间的多个比较(事后测试)?我所发现的都是一个变量的函数,具有两个以上的水平(单向方差分析)或混合设计(即一个变量之间和一个变量内)。

一种选择可能是使用afex包来拟合ANOVA和marginaleffects包来计算对比度。(免责声明:我是marginaleffects的作者。)

首先,拟合模型:

library(marginaleffects)
library(afex)
df = matrix(nrow = 120, ncol = 0) 
df = as.data.frame(df)
df$ppt_id = c(rep(c(1:30),4))
df$color = c(rep("red", 60), rep("blue", 60))
df$side = c(rep(c(rep("left", 30), rep("right", 30)),2))
df$RT = c(rnorm(30, 600, 50),
rnorm(30, 650, 50),
rnorm(30, 700, 50),
rnorm(30, 600, 50))
mod <- aov_ez(
id = "ppt_id",
dv = "RT",
within = c("side", "color"),
data = df)

然后,进行事后处理以获得对比。这将告诉您,当我们操纵解释符(及其成对组合)时,模型预测的结果值是如何变化的:

cmp <- comparisons(
# the model
mod, 
# hold other regressors at their means if there are any
newdata = "mean",
# vector of variables we want to "manipulate" in the contrast
variables = c("side", "color"),
# we care about interactions
interactions = TRUE)
summary(cmp)
#> Average contrasts 
#>           side      color  Effect Std. Error z value   Pr(>|z|)
#> 1  left - left blue - red 110.489      10.71 10.3148 < 2.22e-16
#> 2 right - left  red - red  51.055      13.84  3.6896 0.00022457
#> 3 right - left blue - red   6.274      12.19  0.5147 0.60674367
#>    2.5 % 97.5 %
#> 1  89.49 131.48
#> 2  23.93  78.18
#> 3 -17.62  30.16
#> 
#> Model type:  afex_aov 
#> Prediction type:  response

这里有一个关于对比的详细插图:https://vincentarelbundock.github.io/marginaleffects/articles/contrasts.html

编辑:一些关于解释的注释

首先使用afex包的默认predict()方法来计算具有color蓝色和side变量的任意值的个体的预测结果:

nd <- data.frame(side = c("left", "right"), color = "blue", ppt_id = 1)
nd
#>    side color ppt_id
#> 1  left  blue      1
#> 2 right  blue      1
predict(mod, newdata = nd)
#>        1        2 
#> 678.7812 609.2096

左+蓝和右+蓝的区别是:

diff(predict(mod, newdata = nd))
#>         2 
#> -69.57154

我们可以使用marginaleffects()中的predictions()comparisons()函数获得相同的标准误差结果:

predictions(mod, newdata = nd)
#>   rowid     type predicted std.error statistic p.value conf.low conf.high  side color ppt_id
#> 1     1 response  678.7812  11.02046  61.59284       0 657.1815  700.3808  left  blue      1
#> 2     2 response  609.2096   9.27690  65.66953       0 591.0272  627.3920 right  blue      1
comparisons(
mod,
variables = "side",
newdata = datagrid(color = "blue", ppt_id = 1))
#>   rowid     type        term contrast_side comparison std.error statistic      p.value  conf.low conf.high side color ppt_id
#> 1     1 response interaction  right - left  -69.57154  14.12988 -4.923717 8.491553e-07 -97.26559 -41.87748 left  blue      1

这是一个简单的"对比":当side预测结果发生了什么变化从leftrightcolor等于blue个人1 ?

现在我们可以计算"左"one_answers"右"之间相同的对比,但对于color条件下的个体:

comparisons(
mod,
variables = "side",
newdata = datagrid(color = c("red", "blue"), ppt_id = 1))
#>   rowid     type        term contrast_side comparison std.error statistic      p.value  conf.low conf.high side color ppt_id
#> 1     1 response interaction  right - left   52.41222  13.74332  3.813650 1.369293e-04  25.47581  79.34864 left   red      1
#> 2     2 response interaction  right - left  -69.57154  14.12988 -4.923717 8.491553e-07 -97.26559 -41.87748 left  blue      1

在我最初给您的第一个示例中,您看到了"交互"对比:当sidecolor变量同时更改时会发生什么?

我不是afex包的专家,所以你必须参考他们的文档来回答"within"的问题。

也许我不能理解@Vincent的答案,或者我没有解释清楚我需要什么。

理想情况下,我希望得到一个表所有可能的比较对于这个数据集,也就是说,应该有6个比较(因为有4种可能的条件),类似于这个函数所做的:

## combine both independent variables into one
df$condition = paste0(df$side, "_", df$color)
df$condition = as.factor(df$condition)
## calculate pairwise tests
mult_comp = df %>%
pairwise_t_test(
RT ~ condition, paired = TRUE, 
p.adjust.method = "holm")%>%
select(-statistic, -df)

现在,问题是这个函数只能使用一个自变量(对于标准函数pairwise.t.test也是如此),而我实际上有两个自变量。

一个可能的解决方案是通过使用函数aov来计算ANOVA,然后使用函数TukeyHSD来计算两两比较:

anova_df = aov(RT ~ side*color, data = df)
TukeyHSD(anova_df)

缺点是计算被限制在Tukey方法,这可能并不总是合适的。例如,在这种情况下,使用它在统计上是不正确的,因为我有一个重复测量的设计-并且不可能对该函数使用其他校正。

最新更新