One way analysis of variance helps us understand the relationship between one continuous dependent variable and one categorical independent variable. When we have one continuous dependent variable and more than one independent categorical variable we cannot use one way ANOVA. When we have two independent categorical variable we need to use two way ANOVA. When we have more than two categorical independent variables we need to use N way ANOVA.

In two way ANOVA there are three hypotheses of interest as listed below

- H
_{0}: There is an effect of the first factor on the dependent continuous variable (main effect) - H
_{0}: There is an effect of the second factor variable on the dependent continuous variable (main effect) - H
_{0}: There is a combined effect of the first and second factor variable on the continuous dependent variable (interaction)

The above hypotheses can be extended from two factor variables to N factor variables.

For results of two way ANOVA to be valid there are several assumptions that need to be satisfied. They are listed below.

- Observations must be independent within and across groups
- Observations are approximately normally distributed.
- There is equal variance in the observations
- We should not have any outliers especially when our design is unbalanced
- The errors are independent

When the normality and equal variance assumptions are violated you need to transform your data.

In this exercise we will use data on a moth experiment which is available here here. The data is not well formatted in that link so use this csv file moth-trap-experiment.

The dependent variable is the number of moths in a trap. The independent variables are location and type of lure. There were four locations (top, middle, lower and ground). There were three types of lure (scent, sugar and chemical).

Solutions to these exercises are found here

Exercise 1

Read in the data and inspect its structure

Exercise 2

Create summary statistics for location

Exercise 3

Create summary statistics for type of lure

Exercise 4

Create boxplots for each category

Exercise 5

Check for normality

Exercise 6

Check for equality of variance

Exercise 7

Take a log transformation of our data

Exercise 8

Perform a power analysis

Exercise 9

Perform anova

Exercise 10

Check homogeneity of variance

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

Mike Anderson says

Great exercise! It hits all the points I’m trying to teach in my undergrad biostatistics course, once I rearrange the syllabus so this all makes sense when it shows up. Do I have your permission to use this (with attribution, of course) as a course assignment?

Han de Vries says

Dear Prof Anderson,

Thanks for your kind words. I responded with a more elaborate answer by email, but in short: Yes, you have our permission.