在R中的for语句中执行多个ANOVA检验,“变量长度不同”错误

x0fgdtte  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(80)

我想对多个DV进行方差分析测试。我理解多重比较的危险性,但在如何做到这一点方面存在技术问题。
这是我的妈妈:

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):变量长度不同(为“Species”找到)
我不明白这个错误是从哪里来的。如果我单独执行比较,就没有问题

9lowa7mx

9lowa7mx1#

使用@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 ****

相关问题