11 Repeated Measures ANOVA

In this example we’ll use the ezANOVA() function for fitting a Repeated-Measures ANOVA model, which will take the following form:

\[y_i=\beta_0+\beta_1\left(X1_L\right)+\beta_2\left(X1_Q\right)+U_i+\epsilon_{ij}\]

The code in this chapter only works if you’re following along with the Github folder for this book (which you can download here), you’ve correctly set your working directory to the data folder (which you can learn how to do in Chapter 4), and run the code in the order it appears in this chapter.

Importing

We’ll be using the cardiovascular.csv file for this example.

data <- read.csv("cardiovascular.csv")

Viewing

This dataset contains mean arterial pressure values of subjects during Rest and at four time points while exercising, MAP_T1:T4. The time points were spaced equally apart and correspond to a linearly increasing exercise workload on a treadmill.

head(data)
  SubID      Rest    MAP_T1   MAP_T2   MAP_T3   MAP_T4
1     1  94.33333 102.00000 112.6667 115.3333 115.6667
2     2  90.33333  88.33333 106.0000 106.6667 114.0000
3     3 103.00000  98.33333 119.0000 118.6667 121.3333
4     4 106.33333 123.00000 134.0000 130.6667 134.0000
5     5  97.66667 105.66667 101.0000 109.0000 104.6667
6     6  97.00000 100.66667  99.0000 102.6667 101.0000

More examples of viewing data can be found in Chapter 5

Formatting

The data should be converted to long format to make it compatible with the ezANOVA() function. We can use the pivot_longer() function from the tidyverse package. Make sure you have that package loaded before using this function.

data <- pivot_longer(data = data, 
                     cols = c("Rest", "MAP_T1", "MAP_T2", "MAP_T3", "MAP_T4"),
                     names_to = "Time",
                     values_to = "MAP")

More examples of formatting data can be found in Chapter 6

Modeling

The ezANOVA()Function

ezANOVA(
    data
    , dv
    , wid
    , within = NULL
    , within_full = NULL
    , within_covariates = NULL
    , between = NULL
    , between_covariates = NULL
    , observed = NULL
    , diff = NULL
    , reverse_diff = FALSE
    , type = 2
    , white.adjust = FALSE
    , detailed = FALSE
    , return_aov = FALSE
)

The ezANOVA() function is used for ANOVA models. There are many arguments for this function, but not all of them need to be specified. The ones that do need to be specified will depend on the exact ANOVA that you’re performing. If you’d like to learn more about functions and arguments, Chapter 2 covers basic programming concepts, including functions and arguments.

Model

In this example we need to specify the data, dv, wid, and within arguments. The data argument is set equal to the entire dataset, the dv argument, which is short for dependent variable, is set equal to the column of the dependent variable. The wid argument is set equal to the column that contains the variable specifying the case/Ss identifier; usually the subID column, or equivalent. The within argument is set equal to the within-subject factor(s).

ez::ezANOVA(data = data, 
            dv = MAP, 
            wid = SubID, 
            within = .(Time))
$ANOVA
  Effect DFn DFd       F            p p<.05       ges
2   Time   4  76 24.5791 4.476507e-13     * 0.2902555

$`Mauchly's Test for Sphericity`
  Effect         W           p p<.05
2   Time 0.2282138 0.002359703     *

$`Sphericity Corrections`
  Effect       GGe       p[GG] p[GG]<.05       HFe        p[HF] p[HF]<.05
2   Time 0.6209806 6.49275e-09         * 0.7216975 5.046718e-10         *

Notice the formatting in the code above. When using the within and between arguments, you need to use a period and parentheses, .(), when listing the column. You can list additional columns for both of these arguments by separating each column with a comma: within = .(Time, Column2).