ranova
Analysis of variance for within-subject effects in a repeated measures model
Description
Examples
Repeated Measures Analysis of Variance
Load the sample data.
load fisheriris
The column vector species
consists of iris flowers of three different species: setosa, versicolor, virginica. The double matrix meas
consists of four types of measurements on the flowers: the length and width of sepals and petals in centimeters, respectively.
Store the data in a table array.
t = table(species,meas(:,1),meas(:,2),meas(:,3),meas(:,4),... 'VariableNames',{'species','meas1','meas2','meas3','meas4'}); Meas = table([1 2 3 4]','VariableNames',{'Measurements'});
Fit a repeated measures model, where the measurements are the responses and the species is the predictor variable.
rm = fitrm(t,'meas1-meas4~species','WithinDesign',Meas);
Perform repeated measures analysis of variance.
ranovatbl = ranova(rm)
ranovatbl=3×8 table
SumSq DF MeanSq F pValue pValueGG pValueHF pValueLB
______ ___ ________ ______ ___________ ___________ ___________ ___________
(Intercept):Measurements 1656.3 3 552.09 6873.3 0 9.4491e-279 2.9213e-283 2.5871e-125
species:Measurements 282.47 6 47.078 586.1 1.4271e-206 4.9313e-156 1.5406e-158 9.0151e-71
Error(Measurements) 35.423 441 0.080324
There are four measurements, three types of species, and 150 observations. So, degrees of freedom for measurements is (4–1) = 3, for species-measurements interaction it is (4–1)*(3–1) = 6, and for error it is (150–3)*(4–1) = 441. ranova
computes the last three -values using Greenhouse-Geisser, Huynh-Feldt, and Lower bound corrections, respectively. You can check the compound symmetry (sphericity) assumption using the mauchly
method, and display the epsilon corrections using the epsilon
method.
Longitudinal Data
Load the sample data.
load('longitudinalData.mat');
The matrix Y
contains response data for 16 individuals. The response is the blood level of a drug measured at five time points (time = 0, 2, 4, 6, and 8). Each row of Y
corresponds to an individual, and each column corresponds to a time point. The first eight subjects are female, and the second eight subjects are male. This is simulated data.
Define a variable that stores gender information.
Gender = ['F' 'F' 'F' 'F' 'F' 'F' 'F' 'F' 'M' 'M' 'M' 'M' 'M' 'M' 'M' 'M']';
Store the data in a proper table array format to do repeated measures analysis.
t = table(Gender,Y(:,1),Y(:,2),Y(:,3),Y(:,4),Y(:,5),... 'VariableNames',{'Gender','t0','t2','t4','t6','t8'});
Define the within-subjects variable.
Time = [0 2 4 6 8]';
Fit a repeated measures model, where the blood levels are the responses and gender is the predictor variable.
rm = fitrm(t,'t0-t8 ~ Gender','WithinDesign',Time);
Perform repeated measures analysis of variance.
ranovatbl = ranova(rm)
ranovatbl=3×8 table
SumSq DF MeanSq F pValue pValueGG pValueHF pValueLB
______ __ ______ _______ __________ __________ __________ __________
(Intercept):Time 881.7 4 220.43 37.539 3.0348e-15 4.7325e-09 2.4439e-10 2.6198e-05
Gender:Time 17.65 4 4.4125 0.75146 0.56126 0.4877 0.50707 0.40063
Error(Time) 328.83 56 5.872
There are 5 time points, 2 genders, and 16 observations. So, the degrees of freedom for time is (5–1) = 4, for gender-time interaction it is (5–1)*(2–1) = 4, and for error it is (16–2)*(5–1) = 56. The small -value of 2.6198e–05 indicates that there is a significant effect of time on blood pressure. The -value of 0.40063 indicates that there is no significant gender-time interaction.
Specify the Within-Subjects Model
Load the sample data.
load repeatedmeas
The table between
includes the between-subject variables age, IQ, group, gender, and eight repeated measures y1
through y8
as responses. The table within includes the within-subject variables w1
and w2
. This is simulated data. Hypothetically, the response can be results of a memory test. The within-subject variable w1
can be the type of exercise the subject does before the test and w2
can be the different points in the day the subject takes the memory test. So, one subject does two different type of exercises A and B before taking the test and takes the test at four different times on different days. For each subject, the measurements are taken under these conditions:
Exercise to perform before the test: A B A B A B A B
Test time: 1 1 2 2 3 3 4 4
Fit a repeated measures model, where the repeated measures y1
through y8
are the responses, and age, IQ, group, gender, and the group-gender interaction are the predictor variables. Also specify the within-subject design matrix.
rm = fitrm(between,'y1-y8 ~ Group*Gender + Age + IQ','WithinDesign',within);
Perform repeated measures analysis of variance.
ranovatbl = ranova(rm)
ranovatbl=7×8 table
SumSq DF MeanSq F pValue pValueGG pValueHF pValueLB
______ ___ ______ _______ _________ ________ _________ ________
(Intercept):Time 6645.2 7 949.31 2.2689 0.031674 0.071235 0.056257 0.14621
Age:Time 5824.3 7 832.05 1.9887 0.059978 0.10651 0.090128 0.17246
IQ:Time 5188.3 7 741.18 1.7715 0.096749 0.14492 0.12892 0.19683
Group:Time 15800 14 1128.6 2.6975 0.0014425 0.011884 0.0064346 0.089594
Gender:Time 4455.8 7 636.55 1.5214 0.16381 0.20533 0.19258 0.23042
Group:Gender:Time 4247.3 14 303.38 0.72511 0.74677 0.663 0.69184 0.49549
Error(Time) 64433 154 418.39
Specify the model for the within-subject factors. Also display the matrices used in the hypothesis test.
[ranovatbl,A,C,D] = ranova(rm,'WithinModel','w1+w2')
ranovatbl=21×8 table
SumSq DF MeanSq F pValue pValueGG pValueHF pValueLB
______ __ ______ ________ _________ _________ _________ _________
(Intercept) 3141.7 1 3141.7 2.5034 0.12787 0.12787 0.12787 0.12787
Age 537.48 1 537.48 0.42828 0.51962 0.51962 0.51962 0.51962
IQ 2975.9 1 2975.9 2.3712 0.13785 0.13785 0.13785 0.13785
Group 20836 2 10418 8.3012 0.0020601 0.0020601 0.0020601 0.0020601
Gender 3036.3 1 3036.3 2.4194 0.13411 0.13411 0.13411 0.13411
Group:Gender 211.8 2 105.9 0.084385 0.91937 0.91937 0.91937 0.91937
Error 27609 22 1255 1 0.5 0.5 0.5 0.5
(Intercept):w1 146.75 1 146.75 0.23326 0.63389 0.63389 0.63389 0.63389
Age:w1 942.02 1 942.02 1.4974 0.23402 0.23402 0.23402 0.23402
IQ:w1 11.563 1 11.563 0.01838 0.89339 0.89339 0.89339 0.89339
Group:w1 4481.9 2 2240.9 3.562 0.045697 0.045697 0.045697 0.045697
Gender:w1 270.65 1 270.65 0.4302 0.51869 0.51869 0.51869 0.51869
Group:Gender:w1 240.37 2 120.19 0.19104 0.82746 0.82746 0.82746 0.82746
Error(w1) 13841 22 629.12 1 0.5 0.5 0.5 0.5
(Intercept):w2 3663.8 3 1221.3 3.8381 0.013513 0.020339 0.01575 0.062894
Age:w2 1199.9 3 399.95 1.2569 0.2964 0.29645 0.29662 0.27432
⋮
A=6×1 cell array
{[1 0 0 0 0 0 0 0]}
{[0 1 0 0 0 0 0 0]}
{[0 0 1 0 0 0 0 0]}
{2x8 double }
{[0 0 0 0 0 1 0 0]}
{2x8 double }
C=1×3 cell array
{8x1 double} {8x1 double} {8x3 double}
D = 0
Display the contents of A
.
[A{1};A{2};A{3};A{4};A{5};A{6}]
ans = 8×8
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
Display the contents of C
.
[C{1} C{2} C{3}]
ans = 8×5
1 1 1 0 0
1 1 0 1 0
1 1 0 0 1
1 1 -1 -1 -1
1 -1 1 0 0
1 -1 0 1 0
1 -1 0 0 1
1 -1 -1 -1 -1
Perform RANOVA Using Within-Subjects Factors
Load the fisheriris
sample data set.
load fisheriris
The column vector species
contains three iris flower species: setosa, versicolor, and virginica. The matrix meas
contains four types of measurements for the flower: the length and width of sepals and petals in centimeters.
Convert the data in species to categorical string vectors by using the string, " "
and categorical
functions. Create a matrix corresponding to the measurements for setosa flowers and use the array2table
function to convert the matrix to a table.
species = categorical(string(species)); m = meas(species=="setosa",:); tbl = array2table(m,VariableNames=["y1","y2","y3","y4"])
tbl=50×4 table
y1 y2 y3 y4
___ ___ ___ ___
5.1 3.5 1.4 0.2
4.9 3 1.4 0.2
4.7 3.2 1.3 0.2
4.6 3.1 1.5 0.2
5 3.6 1.4 0.2
5.4 3.9 1.7 0.4
4.6 3.4 1.4 0.3
5 3.4 1.5 0.2
4.4 2.9 1.4 0.2
4.9 3.1 1.5 0.1
5.4 3.7 1.5 0.2
4.8 3.4 1.6 0.2
4.8 3 1.4 0.1
4.3 3 1.1 0.1
5.8 4 1.2 0.2
5.7 4.4 1.5 0.4
⋮
Each of the four response variables in tbl
corresponds to the length or width of a sepal or petal.
Create a design for the within-subject factors by using the table
function. Specify the first within-subject factor as the part of the flower being measured and the second as the direction in which the measurement was taken.
part = ["sepal";"sepal";"petal";"petal"]; direction = ["length";"width";"length";"width"]; w2design = table(part,direction,VariableNames=["part","direction"])
w2design=4×2 table
part direction
_______ _________
"sepal" "length"
"sepal" "width"
"petal" "length"
"petal" "width"
Each of the rows in w2design
corresponds to a response variable in tbl
.
Fit a repeated measures model to the measurements in tbl
, using w2design
as the design for the within-subject factors.
rm = fitrm(tbl,"y1-y4~1",WithinDesign=w2design)
rm = RepeatedMeasuresModel with properties: Between Subjects: BetweenDesign: [50x4 table] ResponseNames: {'y1' 'y2' 'y3' 'y4'} BetweenFactorNames: {1x0 cell} BetweenModel: '1' Within Subjects: WithinDesign: [4x2 table] WithinFactorNames: {'part' 'direction'} WithinModel: 'separatemeans' Estimates: Coefficients: [1x4 table] Covariance: [4x4 table]
rm
is a RepeatedMeasuresModel
object that contains the results of fitting the repeated measures model to the data.
Perform an RANOVA to determine if the part of the flower and direction of measurement have a statistically significant effect on the measurement value at the 95% confidence level.
ranovatbl = ranova(rm,WithinModel="part+direction");
disp(ranovatbl)
SumSq DF MeanSq F pValue pValueGG pValueHF pValueLB ______ __ _______ ______ __________ __________ __________ __________ (Intercept) 1285.8 1 1285.8 8360.7 2.0347e-56 2.0347e-56 2.0347e-56 2.0347e-56 Error 7.5354 49 0.15378 (Intercept):part 565.49 1 565.49 5329.6 1.1612e-51 1.1612e-51 1.1612e-51 1.1612e-51 Error(part) 5.199 49 0.1061 (Intercept):direction 97.58 1 97.58 3686.4 8.8091e-48 8.8091e-48 8.8091e-48 8.8091e-48 Error(direction) 1.2971 49 0.02647
ranovatbl
is an RANOVA table that includes the p-values for each term in the within-subjects model. The small p-values for the (Intercept):part
and (Intercept):direction
terms indicate that both the part of the flower and direction of measurement have a statistically significant effect on the measurement values.
Input Arguments
rm
— Repeated measures model
RepeatedMeasuresModel
object
Repeated measures model, returned as a RepeatedMeasuresModel
object.
For properties and methods of this object, see RepeatedMeasuresModel
.
WM
— Model specifying responses
'separatemeans'
(default) | r-by-nc contrast matrix | character vector or string scalar that defines a model
specification
Model specifying the responses, specified as one of the following:
'separatemeans'
— Compute a separate mean for each group.C
— r-by-nc contrast matrix specifying the nc contrasts among the r repeated measures. If Y represents a matrix of repeated measures,ranova
tests the hypothesis that the means of Y*C are zero.A character vector or string scalar that defines a model specification in the within-subject factors. You can define the model based on the rules for the
terms
in themodelspec
argument offitrm
. Also see Model Specification for Repeated Measures Models.
For example, if there are three within-subject factors w1
, w2
,
and w3
, then you can specify a model for the within-subject
factors as follows.
Example: 'WithinModel','w1+w2+w2*w3'
Data Types: single
| double
| char
| string
Output Arguments
ranovatbl
— Results of repeated measures anova
table
Results of repeated measures anova, returned as a table
.
ranovatbl
includes a term representing all differences across the
within-subjects factors. This term has either the name of the
within-subjects factor if specified while fitting the model, or the name
Time
if the name of the within-subjects factor is not
specified while fitting the model or there are more than one within-subjects
factors. ranovatbl
also includes all interactions between
the terms in the within-subject model and all between-subject model terms.
It contains the following columns.
Column Name | Definition |
---|---|
SumSq | Sum of squares. |
DF | Degrees of freedom. |
MeanSq | Mean squared error. |
F | F-statistic. |
pValue | p-value for the corresponding F-statistic. A small p-value indicates significant term effect. |
pValueGG | p-value with Greenhouse-Geisser adjustment. |
pValueHF | p-value with Huynh-Feldt adjustment. |
pValueLB | p-value with Lower bound adjustment. |
The last three p-values are the adjusted p-values
for use when the compound symmetry assumption is not satisfied. For
details, see Compound Symmetry Assumption and Epsilon Corrections. The mauchy
method
tests for sphericity (hence, compound symmetry) and epsilon
method
returns the epsilon adjustment values.
A
— Specification based on between-subjects model
matrix | cell array
Specification based on the between-subjects model, returned
as a matrix or a cell array. It permits the hypothesis on the elements
within given columns of B
(within time hypothesis).
If ranovatbl
contains multiple hypothesis tests, A
might
be a cell array.
Data Types: single
| double
| cell
C
— Specification based on within-subjects model
matrix | cell array
Specification based on the within-subjects model, returned as
a matrix or a cell array. It permits the hypotheses on the elements
within given rows of B
(between time hypotheses).
If ranovatbl
contains multiple hypothesis tests, C
might
be a cell array.
Data Types: single
| double
| cell
D
— Hypothesis value
0
Hypothesis value, returned as 0.
Algorithms
ranova
computes the regular p-value
(in the pValue
column of the rmanova
table)
using the F-statistic cumulative distribution function:
p-value = 1 – fcdf(F,v1,v2).
When the compound symmetry assumption is not satisfied, ranova
uses
a correction factor epsilon, ε, to compute
the corrected p-values as follows:
p-value_corrected = 1 – fcdf(F,ε*v1,ε*v2).
The mauchly
method tests for sphericity (hence,
compound symmetry) and epsilon
method returns the
epsilon adjustment values.
Version History
Introduced in R2014a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)