ANOVA using R – Brief Version

Doing an ANOVA test to infer if the means of three or more groups are the same, when you only have samples from the groups, is really easy.

First create a data frame to hold the data, where one column holds the sample values and the other column holds labels indicating which group the score belongs to:

c1 <- c(3,4,6,5,
c2 <- c("G1","G1","G1","G1",

df <- data.frame(c1,c2)
names(df) <- c("Score","Group")

Now call the aov() function:

model <- aov(df$Score ~ df$Group)
sm <- summary(model)

For this example, the output is:

          Df Sum Sq Mean Sq F value Pr(>F)    
df$Group   2  94.07  47.03  15.88 0.000425
Residuals 12  35.53   2.96

Here the probability that all three source means are the same is only 0.000425 so you conclude the means are not all the same. You can follow up with a Tukey test or separate t-tests to determine which group means are different.


This entry was posted in R Language. Bookmark the permalink.