By John M Quick

The R Tutorial Series provides a collection of user-friendly tutorials to people who want to learn how to use R for statistical analysis.


My Statistical Analysis with R book is available from Packt Publishing and Amazon.


R Tutorial Series: Two-Way Omnibus ANOVA

As with the one-way case, testing the omnibus hypothesis via two-way ANOVA is simple process in R. This tutorial will explore how R can be used to perform a two-way ANOVA to test the difference between two (or more) group means.

Tutorial Files

Before we begin, you may want to download the sample data (.csv) used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 60 participants who are divided by gender (male and female) and treatment group (control and treatment). The values represent a scale that ranges from 1 to 5. For instance, this dataset could be conceptualized as a comparison between two professional training programs, where the control group participated the company's longstanding program and the treatment group participated in an experimental program. The values could represent the attitudes of employees towards the training programs on a scale from 1 (poor) to 5 (excellent).

Beginning Steps

To begin, we need to read our dataset into R and store its contents in a variable.
  1. > #read the two-way ANOVA dataset into an R variable using the read.csv(file) function
  2. > dataTwoWay <- read.csv("dataset_ANOVA_TwoWay.csv")
  3. > #display the data
  4. > dataTwoWay

The first ten rows of our two-way ANOVA dataset.

Two-Way ANOVA

Now that our data are ready, we can conduct a two-way omnibus ANOVA test using the anova(object) function. Note that the only step necessary to add a second independent variable into our ANOVA model is to incorporate it into our lm(model, dataset) function using the * operator. Whereas our one-way model was lm(Values ~ Group), our two-way model becomes lm(Values ~ Group * Gender). As you can see from the results below, adding a second independent variable in this manner also gives us information about the interaction between our variables.
  1. > #use anova(object) to test the omnibus hypothesis in two-way ANOVA
  2. > #Are the differences between the group means for treatment and gender statistically significant?
  3. > #Is there a statistically significant interaction between treatment and gender?
  4. > anova(lm(Values ~ Group * Gender, dataTwoWay))

Our two-way ANOVA table.

The output of our ANOVA test indicates that the difference between our treatment group means is statistically significant (p < .001) and that the difference between genders is not (p = .585). However, in light of the statistically significant interaction between treatment group and gender (p = .032), we would generally elect to forgo these main effects. Subsequently, a series of follow-up procedures could be carried out to examine the simple main effects for treatment group and gender.

Two-Way Multiple Group ANOVA

Conducting a two-way omnibus ANOVA with multiple groups is identical to the demonstrated two-group test. The only difference is that the values in your dataset would be associated with more than two groups. Subsequently, the omnibus hypothesis would test for mean differences across all of the groups. The anova(object) function and its contained lm(formula, data) function would remain the same.

Complete Two-Way Omnibus ANOVA Example

To see a complete example of how a two-way omnibus ANOVA can be conducted in R, please download the two-way ANOVA example (.txt) file.

R Tutorial Series: One-Way Omnibus ANOVA

Testing the omnibus hypothesis via one-way ANOVA is simple process in R. This tutorial will explore how R can be used to perform a one-way ANOVA to test the difference between two (or more) group means.

Tutorial Files

Before we begin, you may want to download the sample data (.csv) used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains a hypothetical sample of 60 participants, who are divided into two groups (control and treatment) of 30. The values represent a scale that ranges from 1 to 5. For instance, this dataset could be conceptualized as a comparison between two professional training programs, where the control group participated the company's longstanding program and the treatment group participated in an experimental program. The values could represent the attitudes of employees towards the training programs on a scale from 1 (poor) to 5 (excellent).

Beginning Steps

To begin, we need to read our dataset into R and store its contents in a variable.
  1. > #read the one-way ANOVA dataset into an R variable using the read.csv(file) function
  2. > dataOneWay <- read.csv("dataset_ANOVA_OneWay.csv")
  3. > #display the data
  4. > dataOneWay

The first ten rows of our one-way ANOVA dataset.

One-Way ANOVA

Now that our data are ready, we can conduct a one-way omnibus ANOVA test using the anova(object) function.
  1. > #use anova(object) to test the omnibus hypothesis in one-way ANOVA
  2. > #is the difference between the group means statistically significant?
  3. > anova(lm(Values ~ Group, dataOneWay))

Our one-way ANOVA table.

The output of our ANOVA test indicates that the difference between our group means is statistically significant (p < .001). Conceptually, this suggests that employee attitudes towards the experimental training program were significantly higher than their attitudes towards the preexisting program.
Note that the object argument in our anova(object) function contained a linear model generated by the lm(formula, data) function. This is the same type of model that is used when conducting linear regression in R. A more detailed explanation of the lm(formula, data) function and examples of its use are available in my Simple Linear Regression article.

One-Way Multiple Group ANOVA

Conducting a one-way omnibus ANOVA with multiple groups is identical to the demonstrated two-group test. The only difference is that the values in your dataset would be associated with more than two groups. Subsequently, the omnibus hypothesis would test for mean differences across all of the groups. The anova(object) function and its contained lm(formula, data) function would remain the same.

Complete One-Way Omnibus ANOVA Example

To see a complete example of how a one-way omnibus ANOVA can be conducted in R, please download the one-way ANOVA example (.txt) file.

R Tutorial Series: ANOVA Tables [Obsolete - See Note]

Update: I have written more detailed tutorials on the subject-matter originally covered in this post. For applying ANOVA to compare linear regression models, see Hierarchical Linear Regression. For general ANOVA, see One-Way Omnibus ANOVA.

The commonly applied analysis of variance procedure, or ANOVA, is a breeze to conduct in R. This tutorial will explore how R can be used to perform ANOVA to analyze a single regression model and to compare multiple models.

Tutorial Files

Before we begin, you may want to download the sample data (.csv) used in this tutorial. Be sure to right-click and save the file to your R working directory. This dataset contains information used to estimate undergraduate enrollment at the University of New Mexico (Office of Institutional Research, 1990). Note that all code samples in this tutorial assume that these data have already been read into an R variable and have been attached.

Pre-ANOVA Steps

Prior to running ANOVA, we need to have one or more regression models. In the segments on simple linear regression and multiple linear regression, we created a series of models using one, two, and three predictors to estimate the fall undergraduate enrollment at the University of New Mexico. The complete code used to derive these models is provided in their respective tutorials. This article assumes that you are familiar with these models and how they were created. Therefore, a shorthand method for generating the models is displayed below.

  1. > #create three linear models using lm(FORMULA, DATAVAR)
  2. > #one predictor model
  3. > onePredictorModel <- lm(ROLL ~ UNEM, datavar)

  4. > #two predictor model

  5. > twoPredictorModel <- lm(ROLL ~ UNEM + HGRAD, datavar)

  6. > #three predictor model

  7. > threePredictorModel <- lm(ROLL ~ UNEM + HGRAD + INC, datavar)

One Model ANOVA

In R, the anova(MODEL) function can be used to run ANOVA, where MODEL is the variable containing the model to be analyzed. The output of the anova(MODEL) function is a standard ANOVA table. An example of how to use the anova(MODEL) function is demonstrated below.

  1. > #use anova(MODEL) to create an ANOVA table for a given model
  2. > #what are the ANOVA results for the two predictor model?
  3. > anova(twoPredictorModel)

The output of the preceding function is pictured below. A similar procedure could be followed to produce ANOVA tables for the one and three predictor models.

Multiple Model ANOVA Comparison

ANOVA can also be used to compare successive models. The following code demonstrates how to do this using the anova(MODEL1, MODEL2, … MODELi) function, where MODEL1, MODEL2, etc. are all model variables.

  1. > #use anova(MODEL1, MODEL2, … MODELi) to compare successive models
  2. > #how do the one predictor, two predictor, and three predictor models compare to one another according to ANOVA?
  3. > anova(onePredictorModel, twoPredictorModel, threePredictorModel)

The output of the preceding function is pictured below. These results give us one context in which to compare the models.

Complete ANOVA Table Example

To see a complete example of how ANOVA tables can be generated in R, please download the ANOVA tables example (.txt) file.

References

Office of Institutional Research (1990). Enrollment Forecast [Data File]. Retrieved November 22, 2009 from http://lib.stat.cmu.edu/DASL/Datafiles/enrolldat.html