Chapter 17 - Analysis of Variance

17.1 ANOVA - One Factor

# get summary & structure and create a boxplot of our differences
str(chickwts)
## 'data.frame':    71 obs. of  2 variables:
##  $ weight: num  179 160 136 227 217 168 108 124 143 140 ...
##  $ feed  : Factor w/ 6 levels "casein","horsebean",..: 2 2 2 2 2 2 2 2 2 2 ...
#> 'data.frame':    71 obs. of  2 variables:
#>  $ weight: num  179 160 136 227 217 168 108 124 143 140 ...
#>  $ feed  : Factor w/ 6 levels "casein","horsebean",..: 2 2 2 2 2 2 2 2 2 2 ...
summary(chickwts)
##      weight             feed   
##  Min.   :108.0   casein   :12  
##  1st Qu.:204.5   horsebean:10  
##  Median :258.0   linseed  :12  
##  Mean   :261.3   meatmeal :11  
##  3rd Qu.:323.5   soybean  :14  
##  Max.   :423.0   sunflower:12
#>      weight           feed   
#>  Min.   :108   casein   :12  
#>  1st Qu.:204   horsebean:10  
#>  Median :258   linseed  :12  
#>  Mean   :261   meatmeal :11  
#>  3rd Qu.:324   soybean  :14  
#>  Max.   :423   sunflower:12
with(chickwts,boxplot(weight~feed,
     col= "lightgray",
     main= "",
     xlab= "Feed type", ylab= "Weight (g)", ylim= c(100,450), las= 1)) 

Figure 17.1: Chicken weight distributions for six different feed types

with(chickwts, tapply(weight, feed, mean))
##    casein horsebean   linseed  meatmeal   soybean sunflower 
##  323.5833  160.2000  218.7500  276.9091  246.4286  328.9167
#>    casein horsebean   linseed  meatmeal   soybean sunflower 
#>       324       160       219       277       246       329
with(chickwts, tapply(weight, feed, sd))
##    casein horsebean   linseed  meatmeal   soybean sunflower 
##  64.43384  38.62584  52.23570  64.90062  54.12907  48.83638
#>    casein horsebean   linseed  meatmeal   soybean sunflower 
#>      64.4      38.6      52.2      64.9      54.1      48.8
with(chickwts, bartlett.test(weight ~ feed))  # long data format, use ~ not ,
## 
##  Bartlett test of homogeneity of variances
## 
## data:  weight by feed
## Bartlett's K-squared = 3.2597, df = 5, p-value = 0.66
#> 
#>  Bartlett test of homogeneity of variances
#> 
#> data:  weight by feed
#> Bartlett's K-squared = 3, df = 5, p-value = 0.7
with(chickwts, oneway.test(weight ~ feed, var.equal = TRUE))
## 
##  One-way analysis of means
## 
## data:  weight and feed
## F = 15.365, num df = 5, denom df = 65, p-value = 5.936e-10
#> 
#>    One-way analysis of means
#> 
#> data:  weight and feed
#> F = 15, num df = 5, denom df = 65, p-value = 6e-10

17.1.1 Pair-wise Comparisons

with(chickwts, pairwise.t.test(weight, feed, pool.sd = TRUE))
## 
##  Pairwise comparisons using t tests with pooled SD 
## 
## data:  weight and feed 
## 
##           casein  horsebean linseed meatmeal soybean
## horsebean 2.9e-08 -         -       -        -      
## linseed   0.00016 0.09435   -       -        -      
## meatmeal  0.18227 9.0e-05   0.09435 -        -      
## soybean   0.00532 0.00298   0.51766 0.51766  -      
## sunflower 0.81249 1.2e-08   8.1e-05 0.13218  0.00298
## 
## P value adjustment method: holm
#> 
#>  Pairwise comparisons using t tests with pooled SD 
#> 
#> data:  weight and feed 
#> 
#>           casein horsebean linseed meatmeal soybean
#> horsebean 3e-08  -         -       -        -      
#> linseed   2e-04  0.094     -       -        -      
#> meatmeal  0.182  9e-05     0.094   -        -      
#> soybean   0.005  0.003     0.518   0.518    -      
#> sunflower 0.812  1e-08     8e-05   0.132    0.003  
#> 
#> P value adjustment method: holm