# 10- The analysis of variance (ANOVA)

Published on Jul 20, 2021

Subscribe to my newsletter and never miss my upcoming articles

Create some data

``````group <- rep(c('A1','A2', 'A3'), times=4)
value <- runif(12, 10, 20)
df1 <- data.frame(group, value)
df1

group <- rep(c('A1','A2', 'A3'), times=4)
value <- runif(12, 10, 20)
year <- as.factor(c(rep("2010",6), rep("2020",6)))
df2 <- data.frame(value,group,year) # dataframe
``````

Visualize data

``````library(ggplot2)
ggplot(df1, aes(x = group, y = value)) +
geom_boxplot()
`````` #### One way ANOVA

It uses to examine statistically significant differences between the means of three or more independent (unrelated) groups

``````df1.anova <- aov(value ~ group, data=df1)
df1.anova
summary(df1.anova)

# extract the p-value and F value
summary(df1.anova)[][1,4:5]
``````

#### Two way ANOVA

A two-way ANOVA, has two independents. Tha style of two way ANOVA is:

aov(dependent~as.factor(independent1)*as.factor(indepndent2),data= filename)

``````df2.anova <- aov(value ~ group*year, data=df2)
summary(df2.anova)

# extract the p-value and F value:
summary(df2.anova)[][1,4:5]
``````

#### Analysis of covariance

``````df2.anova <- aov(value ~ group+year, data=df2)
summary(df2.anova)

# extract the p-value and F value:
summary(df2.anova)[][1,4:5]
``````

#### Interpret the result:

As the p-value is less than the significance level of 0.05, it implies there are no significant differences between tested groups.

``````TukeyHSD(df1.anova)
TukeyHSD(df2.anova)
``````