比方说,我有一个包含30名参与者(ppt_id)的完全阶乘设计,每个设计都在4个条件下进行了测试:
(左侧+蓝色)和(左侧+红色)和(右侧+蓝色)和(右侧+红色)
我的因变量是反应时间(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并发现因素侧与颜色之间存在着显著的交互作用:
anova_test(
data = df,
dv = RT,
wid = ppt_id,
within = c(color, side),
effect.size = "pes")我如何在R中对所有条件进行多个比较(一个后特别测试)?我所发现的只是一个多水平变量的函数(单向方差分析)或混合设计函数(即一个变量之间的函数和一个变量内的函数)。
发布于 2022-07-13 23:17:52
一种选择可能是使用afex软件包来拟合方差分析和the marginaleffects package来计算对比。(免责声明:我是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.2096left+blue和right+blue的区别是:
diff(predict(mod, newdata = nd))
#> 2
#> -69.57154我们可以使用predictions()和comparisons()函数从marginaleffects()中获得与标准错误相同的结果。
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从left更改为right,而color对于个体1等于blue时,预测的结果会发生什么?
现在,我们可以计算“左”和“右”之间的相同对比,但对于在任一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在我最初给您的第一个示例中,您看到了“交互”的对比:当side和color变量同时变化时会发生什么?
我不是afex包的专家,所以您必须参考他们的文档来回答“内部”问题。
发布于 2022-07-15 11:42:09
也许我无法理解文森特的答案,或者我没有清楚地解释我需要什么。
理想情况下,我希望得到一个带有的表--这个数据集的所有可能的比较,也就是说,应该有六个比较(因为有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计算方差,然后使用函数TukeyHSD计算成对的比较:
anova_df = aov(RT ~ side*color, data = df)
TukeyHSD(anova_df)缺点是,计算仅限于Tukey方法,这可能并不总是合适的。例如,在这种情况下,使用它在统计上是不正确的,因为我有一个重复的度量设计,并且不可能使用这个函数的其他更正。
https://stackoverflow.com/questions/72973408
复制相似问题