A one-factor repeated-measures ANOVA compares a quantitative outcome across three or more conditions or time points measured on the same experimental units. Because measurements from one subject are correlated, the analysis must account for subject-level dependence.
Repeated-measures designs can improve precision by comparing each subject with itself, but only when the repeated observations are correctly identified and the design is followed. They do not remove the need for appropriate sampling, measurement, and model assumptions.
- Individual differences or error term can be considerably reduced as a source of between group differences.
- The sample size is not divided between conditions or groups and thus inferential testing becomes more powerful.
- The design proves to be economical when sample members are difficult to recruit.
A repeated-measures ANOVA can be viewed as an extension of the paired t-test when there are more than two levels of the within-subject factor. The same subjects contribute observations at each level.
With two time points, a paired t-test is a common analysis of the within-subject change. With more than two levels, an omnibus repeated-measures test can assess whether any mean differs, followed by planned contrasts or adjusted pairwise comparisons.
The model accounts for variation among subjects separately from within-subject residual variation. When there are more than two repeated levels, the classical univariate ANOVA also relies on sphericity; consider a correction or a mixed model if that assumption is not reasonable.
The error decomposition includes subject-to-subject variability and residual variation within subjects. The correct denominator for the condition effect follows the repeated-measures model and its assumptions.
- A random subject effect
- A fixed treatment or time effect
Treating subjects as a random sample supports inference beyond those individuals only when the sampling and study design justify that generalization. The model alone does not make a convenience sample representative.
Description of data
The example records each participant’s tolerance after receiving a pain-relieving drug at four time points. Verify that each subject has one observation at every time and that the time variable is coded in the intended order.
Clear R environment
Clear workspace objects only if needed, after saving anything to retain. Closing graphics devices or clearing the console is optional and does not change the data or analysis.
rm(list = ls(all = TRUE))
graphics.off()
shell("cls")Importing data
Import the CSV file with the correct path and header setting. Check for duplicate subject-time records, missing observations, and unexpected values before fitting a repeated-measures model.
Convert subject ID and time to factors when they are categorical. Retain subject IDs consistently across time, and use explicit data-frame references in reusable scripts rather than attach().
data = read.csv(
file = 'repeated.csv',
header = TRUE
)
head(data)
data$subject = as.factor(data$subject)
data$time = as.factor(data$time)
attach(data)# subject time resp
# 1 A 1 0.12
# 2 B 1 1.25
# 3 C 1 2.35
# 4 D 1 3.31
# 5 E 1 2.21
# 6 F 1 2.47
One way repeated measures ANOVA model
The aov() model with Error(subject/time) represents subject-level variation and within-subject error for this one-factor design. Confirm that the data are balanced as expected and review the output strata. For incomplete repeated measures or more flexible covariance structures, a mixed-effects model may be more suitable.
model = aov(formula = resp ~ time + Error(subject/time))
summary(model)#
# Error: subject
# Df Sum Sq Mean Sq F value Pr(>F)
# Residuals 5 6.895 1.379
#
# Error: subject:time
# Df Sum Sq Mean Sq F value Pr(>F)
# time 3 28.561 9.520 27.4 2.47e-06 ***
# Residuals 15 5.212 0.347
# ---
# Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The repeated-measures model separates subject variability from the within-subject error used to test time. This is the key difference from an independent-groups one-way ANOVA; the observations are not treated as independent.
Visualizing results
Line graph
Plot the observed response over time to examine the trajectory. A line plot of group means alone hides individual variation, so consider showing paired subject profiles or model-based means with uncertainty.
library(dplyr)
summary = data %>%
group_by(time) %>%
summarize(Mean_resp = mean(resp),
SD_resp = sd(resp),
SE_resp = sd(resp)/sqrt(length(resp)),
n = n())
print(summary)# # A tibble: 4 x 5
# time Mean_resp SD_resp SE_resp n
# <fct> <dbl> <dbl> <dbl> <int>
# 1 1 1.95 1.11 0.454 6
# 2 2 3.04 0.903 0.369 6
# 3 3 3.64 0.449 0.183 6
# 4 4 4.97 0.409 0.167 6
Calculate descriptive summaries by time using a consistent definition of the experimental unit. Check the number of observed subjects contributing to each time point.
plot(summary$Mean_resp,
type = 'o',
xlab = 'Time',
ylab = 'response')Label axes with the measured response and time units. A plotted trend is descriptive; assess inferential differences with the fitted repeated-measures model and its contrasts.
Mean separation test
Use paired comparisons only when they preserve the within-subject pairing. Apply an adjustment such as Bonferroni when controlling the family-wise error across multiple tests, and report the number of comparisons and adjusted p-values.
pairwise.t.test(
x = resp,
g = time,
p.adjust.method = 'bonferroni'
)#
# Pairwise comparisons using t tests with pooled SD
#
# data: resp and time
#
# 1 2 3
# 2 0.1501 - -
# 3 0.0074 1.0000 -
# 4 9.1e-06 0.0021 0.0456
#
# P value adjustment method: bonferroni
Interpret adjusted comparisons alongside estimated mean differences and confidence intervals. Do not infer that all time points differ simply because the omnibus test is significant; identify which contrasts show evidence of differences and report the adjustment.
Please comment below if you have any questions.
Download data file — Click_here
Download Rscript — Click_here
Download R program —
Click_hereDownload R studio —
Click_here

Comments
Post a Comment