【问题标题】:how to run 2 way mixed anova with type 3 error using for loop in R?如何使用 R 中的 for 循环运行 2 路混合方差分析和类型 3 错误?
【发布时间】:2021-05-13 23:40:56
【问题描述】:

我的统计分析设计是多个 2 路混合方差分析。自变量是 Team(即,在因子之间)和 Box(即,在因子内)。由于参与者的数量不匹配,我想使用aov_carcar::Anova 而不是aovrstatix::anova_test 来计算欧米茄平方。当我单独运行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返回错误。

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 错误的多个 ANOVA?


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

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

【问题讨论】:

  • 对于您遇到的错误,这里的解决方案可能会解决它:stackoverflow.com/a/59626119/6288065。如果没有,请提供一个我们可以使用的可重现示例。我们需要查看您的代码的哪一部分导致了此错误。例如,它是 for 循环中的第二行还是第三行?尝试注释掉或删除第三行,让我们知道会发生什么。另外,c[i] 中的c 是什么?
  • 如果您创建一个小的可重现示例以及预期的输出,这将更容易提供帮助。阅读how to give a reproducible example
  • @LC-datascientist c 是数据集的列名。我添加了一个可重现的示例。
  • @RonakShah 我添加了一个可重现的示例。谢谢,
  • 举个例子,你如何在没有循环的情况下运行它?我正在尝试m &lt;- aov_car(var1~ between*within+Error(id/within), data = dat)a &lt;- omega_squared(m, partial = TRUE) a 返回Error in cat(insight::format_table(x, digits = digits)) : argument 1 (type 'list') cannot be handled by 'cat'

标签: r anova


【解决方案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]])
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多