R-bloggers

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Repeated Measures of ANOVA in R Complete Tutorial

Posted on April 6, 2021 by finnstats in R bloggers | 0 Comments

[This article was first published on Methods – finnstats, 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.

Repeated Measures of ANOVA in R, in this tutorial we are going to discuss one-way and two-way repeated measures of ANOVA.

In this case, the same individuals are measured the same outcome variable under different time points or conditions.

This test is also known as a within-subjects ANOVA or ANOVA with repeated measures.

Assumptions

1. No significant outlier

To identify the outlier, you can use the box plot method or the three sigma limit method.

2. Normality Assumption

The outcome of the dependent variable should be normally distributed in each cell of the design.

Based on the Shapiro Wilks test can use it for the same.

3. Assumption of Sphericity

These can be checked based on the anova_test function. The variance of the differences between groups should be equal.

Sphericity can be checked using Mauchly’s test of sphericity, which is automatically reported when using the R function anova_test()

The above assumptions are not met then you can use an alternative of Repeated Measures ANOVA in the nonparametric method (Friedman Test).

One-way Repeated Measures of ANOVA in R

Prerequisites

library(xlsx) library(rstatix) library(reshape) library(tidyverse) library(dplyr) library(ggpubr) library(plyr) library(datarium)

Getting Data

data<-read.xlsx("D:/RStudio/data.xlsx",sheetName="Sheet1") data <- data %>% gather(key = "time", value = "score", T0, T1, T2) %>% convert_as_factor(id, time) data.frame(head(data, 3)) id time score 1 S1 T0 7.454333333 2 S2 T0 8.030000000 3 S3 T0 9.911666667

Objective

The objective is to identify any significant difference between time points exit or not.

Summary statistics

summary% group_by(time) %>% get_summary_stats(score, type = "mean_sd") data.frame(summary) time variable n mean sd 1 T0 score 24 7.853 3.082 2 T1 score 24 9.298 2.090 3 T2 score 24 5.586 0.396

Visualization

Create a box plot and add points corresponding to individual values:

You can use a box plot also for outlier identification.

Based on boxplot no outlier detected in the dataset.

Outlier Detection

outlier% group_by(time) %>% identify_outliers(score) data.frame(outlier) [1] time id score is.outlier is.extreme (or 0-length row.names)

These indicates no outlier in the data set.

Normality Checking

You can use Shapiro-Wilk’s test for normality checking, Please note if the data set is small then Shapiro-Wilk’s test is ideal otherwise go for the QQ plot.

normality% group_by(time) %>% shapiro_test(score) data.frame(normality) time variable statistic p 1 T0 score 0.9540460218 0.33073045010 2 T1 score 0.9868670126 0.98282491871 3 T2 score 0.9261641361 0.08004275234

The normality assumption can be checked by computing the Shapiro-Wilk test for each time point. If the data is normally distributed, the p-value should be greater than 0.05.

Tested data was normally distributed at each time point, as assessed by Shapiro-Wilk’s test (p > 0.05).

If your sample size is greater than 50, the normal QQ plot is preferred because at larger sample sizes the Shapiro-Wilk test becomes very sensitive even to a minor deviation from normality.

Sphericity Assumption

The data set score was significantly different at the different time points during the diet, F(1.42, 33) = 24.4, p 0.05), except for treatment B at time point T2, as assessed by Shapiro-Wilk’s test.

QQ Plot

Based on Shapiro-Wilk’s analysis at time point T2 non-normality observed, so further you need to check the QQ plot for the verification.

From the plot above, as all the points fall approximately along the reference line, we can assume normality.

Sphericity Assumption

res.aov 

Observed statistically significant two-way interactions between treatment and time p

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.