提问人:Mark Davies 提问时间:11/10/2023 更新时间:11/10/2023 访问量:44
在 R 的 for 语句中执行多个方差分析检验。 “可变长度不同”错误
Performing multiple ANOVA tests in a for statement in R. "variable lengths differ" error
问:
我想对多个 DV 执行方差分析测试。我理解多重比较的危险,但在如何做到这一点方面存在技术问题。
这是我的mwe:
vars = c("Sepal.Length" , "Sepal.Width" ,"Petal.Length" , "Petal.Width")
for (i in vars) {
test = aov(data = iris, i~Species)
print(i)
summary(test)
tukey_hsd(test)
}
我收到此错误: model.frame.default(formula = i ~ Species, data = iris, drop.unused.levels = TRUE) 中的错误: 可变长度不同(适用于“物种”)
我不明白这个错误是从哪里来的。如果我单独进行比较,则没有问题
答:
1赞
scott.pilgrim.vs.r
11/10/2023
#1
用@Axeman的答案,这是一种方法。
vars = c("Sepal.Length" , "Sepal.Width" ,"Petal.Length" , "Petal.Width")
for (i in vars) {
f <- reformulate(paste(i,'~Species'))
test = aov(data = iris, f )
print(i)
print(summary(test))
print(tukey_hsd(test))
}
[1] "Sepal.Length"
Df Sum Sq Mean Sq F value Pr(>F)
Species 2 63.21 31.606 119.3 <2e-16 ***
Residuals 147 38.96 0.265
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# A tibble: 3 × 9
term group1 group2 null.value estimate conf.low conf.high p.adj p.adj.signif
* <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 Species setosa versicolor 0 0.93 0.686 1.17 3.39e-14 ****
2 Species setosa virginica 0 1.58 1.34 1.83 3 e-15 ****
3 Species versicolor virginica 0 0.652 0.408 0.896 8.29e- 9 ****
[1] "Sepal.Width"
Df Sum Sq Mean Sq F value Pr(>F)
Species 2 11.35 5.672 49.16 <2e-16 ***
Residuals 147 16.96 0.115
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# A tibble: 3 × 9
term group1 group2 null.value estimate conf.low conf.high p.adj p.adj.signif
* <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 Species setosa versicolor 0 -0.658 -0.819 -0.497 3.1 e-14 ****
2 Species setosa virginica 0 -0.454 -0.615 -0.293 1.36e- 9 ****
3 Species versicolor virginica 0 0.204 0.0431 0.365 8.78e- 3 **
[1] "Petal.Length"
Df Sum Sq Mean Sq F value Pr(>F)
Species 2 437.1 218.55 1180 <2e-16 ***
Residuals 147 27.2 0.19
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# A tibble: 3 × 9
term group1 group2 null.value estimate conf.low conf.high p.adj p.adj.signif
* <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 Species setosa versicolor 0 2.80 2.59 3.00 3e-15 ****
2 Species setosa virginica 0 4.09 3.89 4.29 3e-15 ****
3 Species versicolor virginica 0 1.29 1.09 1.50 3e-15 ****
[1] "Petal.Width"
Df Sum Sq Mean Sq F value Pr(>F)
Species 2 80.41 40.21 960 <2e-16 ***
Residuals 147 6.16 0.04
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# A tibble: 3 × 9
term group1 group2 null.value estimate conf.low conf.high p.adj p.adj.signif
* <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 Species setosa versicolor 0 1.08 0.983 1.18 3e-15 ****
2 Species setosa virginica 0 1.78 1.68 1.88 3e-15 ****
3 Species versicolor virginica 0 0.700 0.603 0.797 3e-15 ****
评论
0赞
Mark Davies
11/10/2023
这有效,谢谢。但事实并非如此:test = kruskal.test(data = iris,f)
评论
i
vars
i
reformulate
as.formula(paste(...))
f <- reformulate(paste(i,'~stage'))
print(t_test(all_pav, formula =f, paired = T))