简体   繁体   English

R 中的事后检验,用于具有 2 个内变量的重复测量 ANOVA

[英]Post hoc test in R for repeated measures ANOVA with 2 within-variables

Say, I have a full factorial design with 30 participants (ppt_id), each of which was tested in 4 conditions:比如说,我有一个包含 30 个参与者 (ppt_id) 的全因子设计,每个参与者都在 4 个条件下进行了测试:

(left side + blue color) AND (left side + red color) AND (right side + blue color) AND (right side + red color) (左侧+蓝色)AND(左侧+红色)AND(右侧+蓝色)AND(右侧+红色)

And my dependent variable is reaction time (RT).我的因变量是反应时间(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))

Now, I calculate rmANOVA and find out that there is a significant interaction between factors Side and Color:现在,我计算 rmANOVA 并发现因素 Side 和 Color 之间存在显着的交互作用:

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

How can I perform multiple comparisons of all conditions with each other (a post hoc test) in R?如何在 R 中对所有条件进行多次比较(事后测试)? All I found were functions for one variable with more than two levels (one-way ANOVA) or for mixed designs (ie, one between- and one within-variable).我发现的只是一个具有两个以上水平的变量(单向方差分析)或混合设计(即一个变量之间和一个变量内)的函数。

One option may be to use the afex package to fit the ANOVA and the marginaleffects package to compute contrasts.一种选择可能是使用afex包来拟合 ANOVA 和marginaleffects效应包来计算对比度。 (Disclaimer: I am the author of marginaleffects .) (免责声明:我是marginaleffects的作者。)

First, fit the model:首先,拟合模型:

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)

Then, do post hoc processing to get the contrasts.然后,进行事后处理以获得对比。 This will tell you how the values of the outcome predicted by the model change when we manipulate the explanators (and their pairwise combinations):这将告诉您当我们操纵解释器(及其成对组合)时,模型预测的结果值如何变化:

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

There is a detailed vignette on contrasts here: https://vincentarelbundock.github.io/marginaleffects/articles/contrasts.html这里有一个关于对比的详细插图: https ://vincentarelbundock.github.io/marginaleffects/articles/contrasts.html

Perhaps I'm not able to understand the answer by @Vincent, or maybe I did not explain clearly what I needed.也许我无法理解@Vincent 的答案,或者我没有清楚地解释我需要什么。

Ideally, I would like to get a table with all possible comparisons for this dataset, ie, there should be six comparisons (because there are 4 possible conditions), something like what this function does:理想情况下,我想得到一个包含该数据集所有可能比较的表,即应该有六个比较(因为有 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)

Now, the problem is that this function can only work with one independent variable (the same is true for a standard function pairwise.t.test), while I actually have two independent variables.现在,问题是这个函数只能使用一个自变量(标准函数pairwise.t.test也是如此),而我实际上有两个自变量。

One possible solution is to calculate ANOVA by using the function aov and then use the function TukeyHSD for calculating pairwise comparisons:一种可能的解决方案是使用函数 aov 计算 ANOVA,然后使用函数 TukeyHSD 计算成对比较:

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

TukeyHSD(anova_df)

The downside is that the calculation is then limited to the Tukey method, which might not always be appropriate.缺点是计算仅限于 Tukey 方法,这可能并不总是合适的。 It is not possible to use other corrections with this function.此功能无法使用其他校正。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM