DEV Community

Dr. Azad Rasul
Dr. Azad Rasul

Posted on

10- ANOVA test using R Programming

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
Enter fullscreen mode Exit fullscreen mode

Visualize data

library(ggplot2)
ggplot(df1, aes(x = group, y = value)) +
  geom_boxplot()
Enter fullscreen mode Exit fullscreen mode

image.png

One way ANOVA

It uses to examine statistically significant differences between two samples means.

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

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

Interpret the result:

P-value ≤ α: The differences between some of the means are statistically significant.

P-value > α: The differences between the means are not statistically significant.

p-value of df1 = 0.9358 > 0.05, so we can not reject the null hypothesis (means are equal) and conclude that means are equal.

Two way ANOVA

A two-way ANOVA, has two independents. The 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]][1,4:5]
summary(df2.anova)[[1]][2,4:5]
summary(df2.anova)[[1]][3,4:5]
Enter fullscreen mode Exit fullscreen mode

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]][1,4:5]
Enter fullscreen mode Exit fullscreen mode
TukeyHSD(df1.anova) #Tukey Honest Significant Differences
TukeyHSD(df2.anova)
Enter fullscreen mode Exit fullscreen mode

If you like the content, please SUBSCRIBE to my channel for the future content

To get full video tutorial and certificate, please, enroll in the course through this link:
https://www.udemy.com/course/r-for-research/?referralCode=B6DCFDE343F0592EA61A

Top comments (0)