Mark Davies
Mark Davies

Reputation: 940

Performing multiple ANOVA tests in a for statement in R. "variable lengths differ" error

I'd like to perform ANOVA tests for multiple DVs. I appreciate the dangers of multiple comparisons, but have a technical issue with how to do this.

This is my 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)
}

I get this error: Error in model.frame.default(formula = i ~ Species, data = iris, drop.unused.levels = TRUE) : variable lengths differ (found for 'Species')

I don't understand where this error comes from. If I perform the comparisons individually, there is no problem

Upvotes: 0

Views: 49

Answers (1)

scott.pilgrim.vs.r
scott.pilgrim.vs.r

Reputation: 507

Using @Axeman 's answer, this is one way to do it.

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 ****     

Upvotes: 1

Related Questions