The first step for conducting an ANOVA in R is to create an ANOVA object. There are two ways of doing this, using the lm()
command, and using the aov()
command. For simplicity we will be using the aov()
command now, but we will get to the lm()
object later.
By using the aov()
command, we can create an object that tells summary()
, plot()
or any other commands that the object is specifically for an ANOVA and as such, will be treated as one.
The syntax for almost all analyses in R is the same. Within our analysis command (aov()
in this example) we write a line equation for our analysis:
y ~ x
This is simply your response variable (Y) and explanatory variable(s) (X) separated by a tilde ~. The tilde acts as an = sign for the analysis.
For this analysis, we will be using the weeds dataset.
weeds.aov <- aov(flowers ~ species, data=weeds) # flowers (Y variable) ~ species (X variable), then data = weeds to direct it to our dataframe.
As per usual, name your newly created object something that will remind you of what it is. I tend to name it something to remind me of the dataset as well as the statistical technique. Generally dataset.analysis.
Now we have our aov()
object, it is good practice to test the assumptions before we look at the results of the analysis. Having this workflow in place will hopefully prevent you from conveniently “forgetting” assumptions after seeing a significant result.