## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

We can also use the ANOVA structure to test for differences between paired or repeated groups. We are going to use the repeated measures ANOVA to compare the mouse weights taken at the beginning, middle, and end of the trial in the FoSSA Mouse data set.

To do this test, you need to reshape your data so that it is in â€˜longâ€™ format, rather than â€˜wideâ€™ format (as it currently is). Long format means that all the Weight variables are in one column, with a second column specifying which time they were measured at (beginning, middle or end). We need an ID variable to reshape our data, and we need to make sure the Weight variables have a standardised name with a number at the end.

You will need to install and load the packages tidyverse, dplyr, and rstatix to perform the necessary commands.

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

> mice <- mice %>%

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

Then, to run the repeated measures ANOVA, we will load the ez package and use the function ezANOVA. The help file contains lots of useful information about where to put the relevant variables. We must specify the data, our dependant variable (dv=weight), our subject OD (wid=id) and the within subject factor (within=time):

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

```
$ANOVA
Effect DFn DFd F p p<.05 ges
2 time 2 118 0.866249 0.4231827 0.005896862
```

## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

We can also use the ANOVA structure to test for differences between paired or repeated groups. We are going to use the repeated measures ANOVA to compare the mouse weights taken at the beginning, middle, and end of the trial in the FoSSA Mouse data set.

To do this test, you need to reshape your data so that it is in â€˜longâ€™ format, rather than â€˜wideâ€™ format (as it currently is). Long format means that all the Weight variables are in one column, with a second column specifying which time they were measured at (beginning, middle or end). We need an ID variable to reshape our data, and we need to make sure the Weight variables have a standardised name with a number at the end.

You will need to install and load the packages tidyverse, dplyr, and rstatix to perform the necessary commands.

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

> mice <- mice %>%

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

Then, to run the repeated measures ANOVA, we will load the ez package and use the function ezANOVA. The help file contains lots of useful information about where to put the relevant variables. We must specify the data, our dependant variable (dv=weight), our subject OD (wid=id) and the within subject factor (within=time):

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

Mauchly’s Test for Sphericity` Effect W p p<.05 2 time 0.7210093 7.590772e-05 *

## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

We can also use the ANOVA structure to test for differences between paired or repeated groups. We are going to use the repeated measures ANOVA to compare the mouse weights taken at the beginning, middle, and end of the trial in the FoSSA Mouse data set.

To do this test, you need to reshape your data so that it is in â€˜longâ€™ format, rather than â€˜wideâ€™ format (as it currently is). Long format means that all the Weight variables are in one column, with a second column specifying which time they were measured at (beginning, middle or end). We need an ID variable to reshape our data, and we need to make sure the Weight variables have a standardised name with a number at the end.

You will need to install and load the packages tidyverse, dplyr, and rstatix to perform the necessary commands.

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

> mice <- mice %>%

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

Then, to run the repeated measures ANOVA, we will load the ez package and use the function ezANOVA. The help file contains lots of useful information about where to put the relevant variables. We must specify the data, our dependant variable (dv=weight), our subject OD (wid=id) and the within subject factor (within=time):

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

Sphericity Corrections` Effect GGe p[GG] p[GG]<.05 HFe p[HF] p[HF]<.05 2 time 0.7818665 0.4000192 0.7993554 0.4021355

The top table provides the results of a one-way repeated measures ANOVA if the assumption of sphericity is not violated. You want to look at the row for â€˜timeâ€™, which shows that the p=0.42. We fail to reject the null hypothesis for a difference in weight across time.

The middle table indicates whether the assumption of sphericity is violated. As p<0.05, we reject the null hypothesis of sphericity, and so will use the sphericity corrections table below.

The bottom table which presents three types of corrections. We can see that the results are consistent and that we do not find evidence to reject the null hypothesis.

**Controlling for between-subjects factors**

**Â **If we want to run this ANOVA again, but control for â€˜strain groupâ€™, we must first ensure it is a factor. R currently has coded it as an integer value, so we convert to a factor:

> mice$Strain_group=as.factor(mice$Strain_group)

We can then add the between subject factor into the ezANOVA command, using the between argument as below:

> repeat2<-ezANOVA(data=mice,dv=weight,wid=id,within=time, between = Strain_group,type=3)

> repeat2

This produces the following results:

```
$ANOVA
Effect DFn DFd F p p<.05 ges
2 Strain_group 2 57 4.334436 1.768830e-02 * 0.091243692
3 time 2 114 1.269825 2.848232e-01 0.007513324
4 Strain_group:time 4 114 14.743714 9.747582e-10 * 0.149509880
```

## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

```
$ANOVA
Effect DFn DFd F p p<.05 ges
2 time 2 118 0.866249 0.4231827 0.005896862
```

## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

Mauchly’s Test for Sphericity` Effect W p p<.05 2 time 0.7210093 7.590772e-05 *

## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

Sphericity Corrections` Effect GGe p[GG] p[GG]<.05 HFe p[HF] p[HF]<.05 2 time 0.7818665 0.4000192 0.7993554 0.4021355

The top table provides the results of a one-way repeated measures ANOVA if the assumption of sphericity is not violated. You want to look at the row for â€˜timeâ€™, which shows that the p=0.42. We fail to reject the null hypothesis for a difference in weight across time.

The middle table indicates whether the assumption of sphericity is violated. As p<0.05, we reject the null hypothesis of sphericity, and so will use the sphericity corrections table below.

The bottom table which presents three types of corrections. We can see that the results are consistent and that we do not find evidence to reject the null hypothesis.

**Controlling for between-subjects factors**

**Â **If we want to run this ANOVA again, but control for â€˜strain groupâ€™, we must first ensure it is a factor. R currently has coded it as an integer value, so we convert to a factor:

> mice$Strain_group=as.factor(mice$Strain_group)

We can then add the between subject factor into the ezANOVA command, using the between argument as below:

> repeat2<-ezANOVA(data=mice,dv=weight,wid=id,within=time, between = Strain_group,type=3)

> repeat2

This produces the following results:

Mauchly’s Test for Sphericity` Effect W p p<.05 3 time 0.873398 0.02259129 * 4 Strain_group:time 0.873398 0.02259129 *

## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

```
$ANOVA
Effect DFn DFd F p p<.05 ges
2 time 2 118 0.866249 0.4231827 0.005896862
```

## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

Mauchly’s Test for Sphericity` Effect W p p<.05 2 time 0.7210093 7.590772e-05 *

## Learning Outcomes

By the end of this section, students will be able to:

- Explain when and how to use post hoc testingÂ
- Explain the concept of multiple comparisons and be able to correct for it in their analysis
- Apply extensions to the basic ANOVA test and interpret their results
- Explain when and how to use repeated measures statistics

You can download a copy of the slides here: B2.5 Repeated Measures ANOVA.pdf

## B2.5 PRACTICAL: R

To create a subject ID variable, we can use:

> mice$id <- 1:nrow(mice)

We then reshape our data to long format:

>Â Â gather(key = “time”, value = “weight”, Weight_baseline, Weight_mid, Weight_end) %>%

>Â Â convert_as_factor(id, time)

> repeat1<-ezANOVA(data=mice,dv=weight,wid=id,within=time,type=3)

> repeat1

This gives the following output:

Sphericity Corrections` Effect GGe p[GG] p[GG]<.05 HFe p[HF] p[HF]<.05 2 time 0.7818665 0.4000192 0.7993554 0.4021355

The top table provides the results of a one-way repeated measures ANOVA if the assumption of sphericity is not violated. You want to look at the row for â€˜timeâ€™, which shows that the p=0.42. We fail to reject the null hypothesis for a difference in weight across time.

The middle table indicates whether the assumption of sphericity is violated. As p<0.05, we reject the null hypothesis of sphericity, and so will use the sphericity corrections table below.

The bottom table which presents three types of corrections. We can see that the results are consistent and that we do not find evidence to reject the null hypothesis.

**Controlling for between-subjects factors**

**Â **If we want to run this ANOVA again, but control for â€˜strain groupâ€™, we must first ensure it is a factor. R currently has coded it as an integer value, so we convert to a factor:

> mice$Strain_group=as.factor(mice$Strain_group)

We can then add the between subject factor into the ezANOVA command, using the between argument as below:

> repeat2<-ezANOVA(data=mice,dv=weight,wid=id,within=time, between = Strain_group,type=3)

> repeat2

This produces the following results:

Sphericity Corrections` Effect GGe p[GG] p[GG]<.05 HFe p[HF] p[HF]<.05 3 time 0.8876249 2.828744e-01 0.9141235 2.834173e-01 4 Strain_group:time 0.8876249 7.067603e-09 * 0.9141235 4.427869e-09 *

__Question B2.6:__**Â Run the repeated measures ANOVA for difference in weights across the three time points, but this time add â€˜Strain Groupâ€™ as a â€˜Between-Subjects Factor. What difference does this make?**

**Answer**

**Answer B2.6:Â **When we review values for time:strain group we see that there is now a statistically significant difference, which holds when we correct for the violated sphericity assumption. This means that there is a significant different in weight when corrected for strain group. This is something that requires more investigation, but as these sample sizes are small and we have not considered whether the FoSSA Mouse data set meets all of the assumptions of our general linear model method. We will investigate these relationships in more detail in Module B3.

## B2.6 PRACTICAL: Stata

To do this test in Stata, you need to reshape your data so that it is in â€˜longâ€™ format, rather than â€˜wideâ€™ format (as it currently is). Long format means that all the Weight variables are in one column, with a second column specifying which time they were measured at (beginning, middle or end). We need an ID variable to reshape our data, and we need to make sure the Weight variables have a standardised name with a number at the end. Considering all of this, run the following commands:

Â gen id=_n

Â keep Strain_group Weight_baseline Weight_mid Weight_end id

Â rename Weight_baseline weight1

Â rename Weight_mid weight2

Â rename Weight_end weight3

Â reshape long weight, i(id) j(time)

Â br

These commands reshape your data so that all the weight measurements are in one column, and they make a new column called â€˜timeâ€™ which indicates if the weight measurements were taken at the beginning (1), middle (2) or end (3).

Now you are ready to run the repeated measures ANOVA, which has this set up:

anova outcome_var ID_var independent_var, repeated(independent_var)

To test for a difference in mouse weights we therefore type:

anova weight id time, repeated(time)

â€˜Weightâ€™ is the dependent variable and the independent variable (i.e. within-subjects factor) is â€˜timeâ€™ with three time points (1,2,3). By default, Stata assumes the independent variable in the anova command is categorical so you do not need to enter the prefix â€˜i.â€™ before time.

The output is:

Alternatively, if you think the assumption of sphericity may be violated, you can look at the second table which presents three types of corrections. We can see that the results are consistent and that we do not find evidence to reject the null hypothesis.

**Controlling for between-subjects factors **

**Â **If we want to run this ANOVA again, but control for â€˜strain groupâ€™, we need to install a user-written command. Type:

findit wsanova

And then click on the â€˜sg103â€™ package in a blue hyperlink to install.

To re-run the anova we did above we would type:

wsanova weight time, id(id) epsilon

Now, we want to control for strain group, which is a between-subjects factor. So the command is:

wsanova weight time, id(id) between( Strain_group) epsilon

__Question B2.6:__**Run the repeated measures ANOVA for difference in weights across the three time points, but this time add â€˜Strain Groupâ€™ as a â€˜Between-Subjects Factor. What difference does this make?**

**Answer**

**Answer B2.6: **When we review values for time*strain group we see that there is now a statistically significant difference. This means that there is a significant different in weight when corrected for strain group. This is something that requires more investigation, but as these sample sizes are small and we have not considered whether the FoSSA Mouse data set meets all of the assumptions of our general linear model method. We will investigate these relationships in more detail in Module B3.

## B2.5 PRACTICAL: SPSS

Select

Analyze >> General Linear Model >> Repeated Measures

The first box which pops up asks you to define the within-subject factor name. Call this â€˜Weightâ€™. Input â€˜3â€™ for number of levels, then press â€˜Addâ€™ and then â€˜Defineâ€™.

You will then see a repeated measures box where you can input the three variables for weight.

As with the Univariate GLM you can also add between subjects factors and covariates to expand this model. You can also use the options on the right hand side to add interactions into the model, conduct post-hoc tests, display parameter estimates, and save your residuals.

To run post hoc testing from the GLM framework for repeated measures you need to select EM Means from the right hand side (as opposed to Post Hoc). Move the variables you wish to run post hoc testing for into the â€˜Display Means forâ€™ box and then tick â€˜Compare main effectsâ€™. There is a drop-down menu for confidence interval adjustment. Leave this as the default which is LSD(none).

**Run the repeated measures ANOVA for difference in weights across the three time points, with an LSD post hoc test. **

**Once you have done this, go back and add â€˜Strain Groupâ€™ as a â€˜Between-Subjects Factor. What difference does this make? **

**Answer**

The first thing to check is the test of sphericity (explained in the video).

As this is significant (P<0.001) we cannot assume sphericity, so we use the â€˜Wilks Lambdaâ€™ statistics to get our F and P values for this relationship.

If the sphericity test is negative (Pâ‰¥0.05), then we can use the sphericity assumed values instead. SPSS outputs these in a different table.

The Pairwise Comparisons table tells us the differences between groups. Unfortunately, this is one test that does not pull through the variable labels that we have assigned, so you need to have your coding values to hand to interpret it.

When we review Wilks Lambda values for weight combined with strain group we see that there is now a statistically significant difference. This means that there is a significant different in weight when corrected for strain group. This is something that requires more investigation, but as these sample sizes are small and we have not considered whether the FoSSA Mouse data set meets all of the assumptions of our general linear model method we will investigate these relationships in more detail in Module B3.