ANOVA Testing in R

To conduct an ANOVA test in R, we have to do a couple of steps first.

The most important is to STACK you data before conducting the test.

One Way Anova

http://www.sthda.com/english/wiki/one-way-anova-test-in-r
# We are using PlantGrowth today. 
# What is important is that the data is already organized into two rows, one with data values and the other with the group that he data is in. You may have to organize the data into this way. If you dont know how run the below line of code. 

# ?cbind()
# ?stack()
# View(PlantGrowth)
# ---- 

# We can run a one-way anova test with the below code
attach(PlantGrowth)
summary(aov(weight ~ group, data = PlantGrowth))
##             Df Sum Sq Mean Sq F value Pr(>F)  
## group        2  3.766  1.8832   4.846 0.0159 *
## Residuals   27 10.492  0.3886                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

MANOVA

We want to test multiple variables at the same time.Look at heighs and weight in two groups of mice being treated differently.

Here is an example code - we’ll use the iris dataset.

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
attach(iris)
septal = Sepal.Length
Petal = Petal.Length

My_Manova = manova(cbind(septal, Petal) ~ Species, data = iris)

summary(My_Manova)
##            Df Pillai approx F num Df den Df    Pr(>F)    
## Species     2 0.9885   71.829      4    294 < 2.2e-16 ***
## Residuals 147                                            
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary.aov(My_Manova)
##  Response septal :
##              Df Sum Sq Mean Sq F value    Pr(>F)    
## Species       2 63.212  31.606  119.26 < 2.2e-16 ***
## Residuals   147 38.956   0.265                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response Petal :
##              Df Sum Sq Mean Sq F value    Pr(>F)    
## Species       2 437.10 218.551  1180.2 < 2.2e-16 ***
## Residuals   147  27.22   0.185                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1