Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Repeated measures data require a different analysis procedure than our typical one-way ANOVA and subsequently follow a different R process. This tutorial will demonstrate how to conduct one-way repeated measures ANOVA in R using the Anova(mod, idata, idesign) function from the car package.

### 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 30 participants whose interest in voting was measured at three different ages (10, 15, and 20). The interest values are represented on a scale that ranges from 1 to 5 and indicate how interested each participant was in voting at each given age.

### Data Setup

Notice that our data are arranged differently for a repeated measures ANOVA. In a typical one-way ANOVA, we would place all of the values of our independent variable in a single column and identify their respective levels with a second column, as demonstrated in this sample one-way dataset. In a repeated measures ANOVA, we instead treat each level of our independent variable as if it were a variable, thus placing them side by side as columns. Hence, rather than having one vertical column for voting interest, with a second column for age, we have three separate columns for voting interest, one for each age level.

### Beginning Steps

To begin, we need to read our dataset into R and store its contents in a variable.
1. > #read the dataset into an R variable using the read.csv(file) function
3. > #display the data
4. > #notice the atypical column arrangement for repeated measures data
5. > dataOneWayRepeatedMeasures

The first ten rows of our dataset

### Preparing the Repeated Measures Factor

Prior to executing our analysis, we must follow a small series of steps in order to prepare our repeated measures factor.

#### Step 1: Define the Levels

1. > #use c() to create a vector containing the number of levels within the repeated measures factor
2. > #create a vector numbering the levels for our three voting interest age groups
3. > ageLevels <- c(1, 2, 3)

#### Step 2: Define the Factor

1. > #use as.factor() to create a factor using the level vector from step 1
2. > #convert the age levels into a factor
3. > ageFactor <- as.factor(ageLevels)

#### Step 3: Define the Frame

1. > #use data.frame() to create a data frame using the factor from step 2
2. > #convert the age factor into a data frame
3. > ageFrame <- data.frame(ageFactor)

#### Step 4: Bind the Columns

1. > #use cbind() to bind the levels of the factor from the original dataset
2. > #bind the age columns
3. > ageBind <- cbind(dataOneWayRepeatedMeasures$Interest10, dataOneWayRepeatedMeasures$Interest15, dataOneWayRepeatedMeasures\$Interest20)

#### Step 5: Define the Model

1. > #use lm() to generate a linear model using the bound factor levels from step 4
2. > #generate a linear model using the bound age levels
3. > ageModel <- lm(ageBind ~ 1)

### Employing the Anova(mod, idata, idesign) Function

Typically, researchers will choose one of several techniques for analyzing repeated measures data, such as an epsilon-correction method, like Huynh-Feldt or Greenhouse-Geisser, or a multivariate method, like Wilks’ Lambda or Hotelling’s Trace. Conveniently, having already prepared our data, we can employ a single Anova(mod, idata, idesign) function from the car package to yield all of the relevant repeated measures results. This allows us simplicity in that only a single function is required, regardless of the technique that we wish to employ. Thus, witnessing our outcomes becomes as simple as locating the desired method in the cleanly printed results.
Our Anova(mod, idata, idesign) function will be composed of three arguments. First, mod will contain our linear model from Step 5 in the preceding section. Second, idata will contain our data frame from Step 3. Third, idesign will contain our factor from Step 2, preceded by a tilde (~). Thus, our final function takes on the following form.
1. > #load the car package (install first, if necessary)
2. library(car)
3. > #compose the Anova(mod, idata, idesign) function
4. > analysis <- Anova(ageModel, idata = ageFrame, idesign = ~ageFactor)

### Visualizing the Results

Finally, we can use the summary(object) function to visualize the results of our repeated measures ANOVA.
1. > #use summary(object) to visualize the results of the repeated measures ANOVA
2. > summary(analysis)

Relevant segment of repeated measures ANOVA results

Supposing that we are interested in the Wilks’ Lambda method, we can see that the differences in the means for voting interest at ages 10, 15, and 20 are statistically significant (p < .001).

### Pairwise Comparisons

Note that we could conduct follow-up comparisons on our age factor to determine which age level means are significantly different from one another. For this purpose, it is recommended that the data be rearranged into the standard ANOVA format that we have used throughout our other tutorials. Subsequently, we could conduct pairwise comparisons in the same manner as demonstrated in the One-Way ANOVA with Comparisons tutorial.

### Complete One-Way Repeated Measures ANOVA Example

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