如何在 R 中使用 for 循环 运行 2 种混合方差分析与类型 3 错误?

how to run 2 way mixed anova with type 3 error using for loop in R?

我的统计分析设计是多个 2-way 混合方差分析。自变量是 Team(即因素之间)和 Box(即因素内)。由于参与者的数量彼此不匹配,我想使用 aov_carcar::Anova 而不是 aovrstatix::anova_test 来计算 omega 平方。当我单独 运行 aov_car 时,它返回结果没有错误(虽然出现警告消息)如下:

aov_car(data2[[4]] ~ Team*Box+Error(ID/Box), data = data2)
**Contrasts set to contr.sum for the following variables: Team**
Anova Table (Type 3 tests)

Response: [[.data2
    Effect    df  MSE      F  ges p.value
1     Team 2, 24 0.03   1.07 .081    .360
2      Box 1, 24 0.00 5.08 * .003    .034
3 Team:Box 2, 24 0.00   1.98 .002    .160
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1

aov_car(ContactTime ~ Team*Box+Error(ID/Box), data = data2)
Contrasts set to contr.sum for the following variables: Team
Anova Table (Type 3 tests)

Response: ContactTime
    Effect    df  MSE      F  ges p.value
1     Team 2, 24 0.03   1.07 .081    .360
2      Box 1, 24 0.00 5.08 * .003    .034
3 Team:Box 2, 24 0.00   1.98 .002    .160
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘+’ 0.1 ‘ ’ 1

但是,当我在 for 循环中使用此代码并将数字更改为索引(即 i)时,r returns 错误。

for (i in 4:39){
  m <- aov(data2[[i]] ~ Team*Box+Error(ID/Box), data = data2)
  cat(paste('\nDependent var:', c[i], '\n'))
  print(summary(m))
  a <- omega_squared(m, partial = TRUE)
  print(a)
}
Error in `[[<-`(`*tmp*`, i, value = value) : no such index at level 1

有谁知道如何使用 for 循环 运行 这些具有类型 3 错误的多重方差分析?


我在下面添加了一个可重现的例子:

library(afex)
library(effectsize)

dat <- data.frame(id=factor(c(1:27,1:27)),
                   within = factor(c(rep('Pre',27),rep('Post',27))),
                  between=factor(c(rep('a',10),rep('b',9),rep('c',8),
                                   rep('a',10),rep('b',9),rep('c',8))),
                  var1=rnorm(54),
                  var2=rnorm(54),
                  var3=rnorm(54))

head(dat)
  id within between       var1       var2       var3
1  1    Pre       a  0.3665630  0.1497566  0.7439017
2  2    Pre       a -0.8780057  1.9250468 -2.0035645
3  3    Pre       a -0.2363018 -0.8559707 -3.5480145
4  4    Pre       a -0.7437626  1.6332548  0.1368730
5  5    Pre       a  0.9967578  0.1443508  0.8658533
6  6    Pre       a  0.8458148 -0.8844405 -0.2159427

tail(dat)
   id within between        var1       var2       var3
49 22   Post       c  0.34770666  0.2485870 -0.2874768
50 23   Post       c  0.32818704 -1.0625061  0.1099009
51 24   Post       c  0.02007684  0.1477275 -1.7305074
52 25   Post       c -0.35504783  0.9240360  0.9997529
53 26   Post       c  1.27313762 -1.5641873  1.8980272
54 27   Post       c  0.51176220  1.1039483  0.3516720

c <- paste(colnames(dat))

for (i in 4:6){
  m <- aov_car(dat[[i]] ~ between*within+Error(id/within), data = dat)
  cat(paste('\nDependent var:', c[i], '\n'))
  print(summary(m))
  a <- omega_squared(m, partial = TRUE)
  print(a)
}

Error in `[[<-`(`*tmp*`, i, value = value) : no such index at level 1

您可以将公式创建为字符串,然后使用 as.formula.

将其转换为公式
cols <- grep('var', names(dat), value = TRUE)
result <- vector('list', length(cols))

for (i in seq_along(cols)) {
  m <- aov_car(as.formula(sprintf('%s~between*within+Error(id/within)', cols[i])), data = dat)
  cat(paste('\nDependent var:', cols[i], '\n'))
  print(summary(m))
  result[[i]] <- omega_squared(m, partial = TRUE)
  print(result[[i]])
}