(This article was first published on

When an interaction is present in a two-way ANOVA, we typically choose to ignore the main effects and elect to investigate the simple main effects when making pairwise comparisons. This tutorial will demonstrate how to conduct pairwise comparisons when an interaction is present in a two-way ANOVA.**R Tutorial Series**, and kindly contributed to R-bloggers)### 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 three stress reduction treatment groups (mental, physical, and medical) and two gender groups (male and female). The stress reduction values are represented on a scale that ranges from 1 to 5. This dataset can be conceptualized as a comparison between three stress treatment programs, one using mental methods, one using physical training, and one using medication across genders. The values represent how effective the treatment programs were at reducing participant's stress levels, with higher numbers indicating higher effectiveness.### Beginning Steps

To begin, we need to read our dataset into R and store its contents in a variable.

- > #read the dataset into an R variable using the read.csv(file) function
- > dataTwoWayInteraction <- read.csv("dataset_ANOVA_TwoWayInteraction.csv")
- > #display the data
- > dataTwoWayInteraction

The first ten rows of our dataset.

### Omnibus Test

Let's run a general omnibus test to assess the main effects and interactions present in the dataset.

- > #use anova(object) to test the omnibus hypothesis
- > #Are main effects or interaction effects present in the independent variables?
- > anova(lm(StressReduction ~ Treatment * Gender, dataTwoWayInteraction))

The omnibus ANOVA test

### Divide the Data

The significant omnibus interaction suggests that we should ignore the main effects and instead investigate the simple main effects for our independent variables. To do so, we need to divide our dataset along each level of our treatment variable. We can create subsets of our dataset using the subset(data, condition) function, where data is the original dataset and condition contains the parameters defining the subset.

- > #use subset(data, condition) to divide the original dataset
- > #medical subset
- > dataMedical <- subset(dataTwoWayInteraction, Treatment == "medical")
- > #mental subset
- > dataMental <- subset(dataTwoWayInteraction, Treatment == "mental")
- > #physical subset
- > dataPhysical <- subset(dataTwoWayInteraction, Treatment == "physical")

### Group ANOVAs

With datasets representing each of our treatment groups, we can now run an ANOVA for each that investigates the impact of gender. You may notice that this is effectively running three one-way ANOVAs with gender being the independent variable. Therefore, we should control for Type I error by dividing our typical .05 alpha level by three (.017).

- > #run ANOVA on the treatment subsets to investigate the impacts of gender within each
- > #medical
- > anova(lm(StressReduction ~ Gender, dataMedical))
- > #mental
- > anova(lm(StressReduction ~ Gender, dataMental))
- > #physical
- > anova(lm(StressReduction ~ Gender, dataPhysical))

The gender within treatment group ANOVA tests

At an alpha level of .017, the gender effect within the mental (

*p*= .014) and physical (

*p*< .001) groups was statistically significant. In the mental condition, the means are 3 for males and 4 for females. In the physical condition, the means are 4 for males and 2 for females. These results suggest that the mental treatment is more effective in reducing stress for females than males, while the physical treatment is more effective for males than females. Further, there is insufficient statistical support for a gender difference in the medical treatment.

### Pairwise Comparisons

Note that since our gender variable contains only two levels, there is no need to conduct follow-up comparisons. However, should you reach this point with an independent variable of more than three levels, you could conduct pairwise comparisons in the same manner as demonstrated in the Two-Way ANOVA with Comparisons tutorial. In this case, remember to carry through your reduced Type I error rate from the preceding ANOVA tests.### Complete Two-Way ANOVA with Interactions Example

To see a complete example of how two-way ANOVA simple main effects can be explored in R, please download the two-way ANOVA interaction example (.txt) file.To

**leave a comment**for the author, please follow the link and comment on his blog:**R Tutorial Series**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...