Repeated measures ANOVA in R

Wait 5 sec.

[This article was first published on R on Stats and R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.IntroductionDataAim and hypothesesAssumptionsVariable type and designIndependence between subjectsNormalitySphericityOutliersRepeated measures ANOVA in RWith the {rstatix} packageWith base RInterpretationsPost-hoc testsSummaryReferencesIntroductionIn a previous article, we presented the one-way ANOVA, the statistical test used to compare a quantitative variable between three groups or more. One of its assumptions was stated very explicitly in that article: the observations must be independent, both within and between the groups. It was also mentioned that if observations between samples are dependent (for example, if three measurements have been collected on the same individuals, as it is often the case in medical studies when a metric is measured (i) before, (ii) during and (iii) after a treatment), the repeated measures ANOVA should be preferred. The present article is dedicated to that test.The repeated measures ANOVA compares the means of a quantitative variable measured several times on the same subjects, that is, under \(k \geq 3\) related conditions or at \(k \geq 3\) different points in time. The aim is exactly the same as the aim of the one-way ANOVA (testing whether the means are equal across the \(k\) conditions), but the design is different: instead of \(k\) independent groups formed by different subjects, we have a single group of subjects who go through all the conditions. The factor whose levels are the conditions is then called a within-subjects factor, as opposed to the between-subjects factor of the one-way ANOVA.The relationship between the two tests mirrors the relationship between the two versions of the Student’s t-test:Independent samplesRelated samples2 groupsStudent’s t-test for independent samplesStudent’s t-test for paired samples3 groups or moreOne-way ANOVARepeated measures ANOVAIn other words, the repeated measures ANOVA is to the paired Student’s t-test what the one-way ANOVA is to the Student’s t-test for independent samples: the generalization of the same logic to three groups or more. This is also why the repeated measures ANOVA appears in the list of related methods presented in the article about the two-way ANOVA, next to the mixed ANOVA (used when a between-subjects factor and a within-subjects factor are present at the same time).Taking the repeated structure of the data into account is not a detail, and it is beneficial for two reasons:Analyzing repeated measurements as if they came from independent groups violates the independence assumption of the one-way ANOVA, so the results of that test could simply not be trusted.Since each subject serves as its own control, the variability between subjects (the fact that some patients are, in general, more sensitive to pain than others, for instance) is isolated and removed from the error term. The test is therefore usually more powerful than a one-way ANOVA run on the same number of measurements.The price to pay for this gain is an additional assumption, called sphericity, which does not exist in the independent-groups case and which is discussed in detail in this article.In the remaining of the post, we present the data, the aim, the hypotheses and the assumptions of the test, and we finally show how to perform it in R, how to complement it with post-hoc tests and how to interpret the results.DataDatasets with a genuinely repeated structure are not so common among the datasets shipped with R, so we simulate our own data. This has the additional advantage that we know exactly how the data have been generated.Suppose that a treatment against chronic pain is administered to 30 randomly selected patients, and that the intensity of the pain is measured on each patient on a scale from 0 (no pain at all) to 100 (unbearable pain) at three different moments:before the treatment,during the treatment, andone month after the end of the treatment, in order to see whether the benefit of the treatment persists over time.# number of patientsn