anova
Analysis of variance for between-subject effects in a repeated measures model
Description
Examples
Load the sample data.
load fisheriris
The column vector species
consists of iris flowers of three different species: setosa, versicolor, and 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 = [1 2 3 4];
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 analysis of variance.
anova(rm)
ans=3×7 table
Within Between SumSq DF MeanSq F pValue
________ ________ ______ ___ _______ ______ ___________
Constant constant 7201.7 1 7201.7 19650 2.0735e-158
Constant species 309.61 2 154.8 422.39 1.1517e-61
Constant Error 53.875 147 0.36649
There are 150 observations and 3 species. The degrees of freedom for species is 3 - 1 = 2, and for error it is 150 - 3 = 147. The small -value of 1.1517e-61 indicates that the measurements differ significantly according to species.
Load and display the panel data.
load panelTbl;
panelTbl
panelTbl=48×4 table
Growth City Year Employ
______ ____ ____ ______
354.02 1 1 64.187
352.05 1 2 65.094
207.1 1 3 55.521
328.17 1 4 63.594
251.53 1 5 63.102
341.74 1 6 53.252
296.93 2 1 52.38
330.16 2 2 59.967
275.24 2 3 69.195
289.41 2 4 56.808
242.91 2 5 61.705
350.68 2 6 54.476
355.71 3 1 65.025
307.06 3 2 55.102
230.33 3 3 60.119
332.27 3 4 63.982
⋮
The table, panelTbl
, contains yearly observations on eight cities for 6 years. The first variable, Growth
, measures economic growth (the response variable). The second and third variables are city and year indicators, respectively. The last variable, Employ
, measures employment (the predictor variable). This is simulated data.
Convert the data in a proper format to do repeated measures analysis.
panelTblNew = unstack(panelTbl(:,["City","Growth","Year"]),"Growth","Year",NewDataVariableNames=... ["year1","year2","year3","year4","year5","year6"])
panelTblNew=8×7 table
City year1 year2 year3 year4 year5 year6
____ ______ ______ ______ ______ ______ ______
1 354.02 352.05 207.1 328.17 251.53 341.74
2 296.93 330.16 275.24 289.41 242.91 350.68
3 355.71 307.06 230.33 332.27 267.99 422.25
4 335.78 302.9 199.29 289.92 266.1 348.24
5 378.11 296.3 276.22 292.43 207.5 341.99
6 354.62 307.09 222.17 339.27 254.71 366.67
7 385.74 297.64 260.1 332.27 230.96 370.99
8 304.3 265.08 236.25 330.61 287.85 316.02
Add the mean employment level over the years as a predictor variable to the table panelTblNew
.
meanEmploy = grpstats(panelTbl.Employ,panelTbl.City); panelTblNew.meanEmploy = meanEmploy;
Define the within-subjects variable.
Year = [1 2 3 4 5 6]';
Fit a repeated measures model, where the growth figures over the 6 years are the responses and the mean employment is the predictor variable.
rm = fitrm(panelTblNew,"year1-year6 ~ meanEmploy",WithinDesign=Year);
Perform analysis of variance.
anovatbl = anova(rm,WithinModel=Year)
anovatbl=3×7 table
Within Between SumSq DF MeanSq F pValue
_________ __________ __________ __ __________ ________ _________
Contrast1 constant 588.17 1 588.17 0.038495 0.85093
Contrast1 meanEmploy 3.7064e+05 1 3.7064e+05 24.258 0.0026428
Contrast1 Error 91675 6 15279
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 blood levels are the responses and gender is the predictor variable.
rm = fitrm(t,'t0-t8 ~ Gender','WithinDesign',Time);
Perform analysis of variance.
anovatbl = anova(rm)
anovatbl=3×7 table
Within Between SumSq DF MeanSq F pValue
________ ________ ______ __ ______ ______ __________
Constant constant 54702 1 54702 1079.2 1.1897e-14
Constant Gender 2251.7 1 2251.7 44.425 1.0693e-05
Constant Error 709.6 14 50.685
There are 2 genders and 16 observations, so the degrees of freedom for gender is (2 - 1) = 1 and for error it is (16 - 2)*(2 - 1) = 14. The small -value of 1.0693e-05 indicates that there is a significant effect of gender on blood pressure.
Repeat analysis of variance using orthogonal contrasts.
anovatbl = anova(rm,'WithinModel','orthogonalcontrasts')
anovatbl=15×7 table
Within Between SumSq DF MeanSq F pValue
________ ________ __________ __ __________ __________ __________
Constant constant 54702 1 54702 1079.2 1.1897e-14
Constant Gender 2251.7 1 2251.7 44.425 1.0693e-05
Constant Error 709.6 14 50.685
Time constant 310.83 1 310.83 31.023 6.9065e-05
Time Gender 13.341 1 13.341 1.3315 0.26785
Time Error 140.27 14 10.019
Time^2 constant 565.42 1 565.42 98.901 1.0003e-07
Time^2 Gender 1.4076 1 1.4076 0.24621 0.62746
Time^2 Error 80.039 14 5.7171
Time^3 constant 2.6127 1 2.6127 1.4318 0.25134
Time^3 Gender 7.8853e-06 1 7.8853e-06 4.3214e-06 0.99837
Time^3 Error 25.546 14 1.8247
Time^4 constant 2.8404 1 2.8404 0.47924 0.50009
Time^4 Gender 2.9016 1 2.9016 0.48956 0.49559
Time^4 Error 82.977 14 5.9269
Input Arguments
Repeated measures model, returned as a RepeatedMeasuresModel
object.
For properties and methods of this object, see RepeatedMeasuresModel
.
Within-subject model, specified as one of the following:
'separatemeans'
— The response is the average of the repeated measures (average across the within-subject model).'orthogonalcontrasts'
— This is valid when the within-subject model has a single numeric factor T. Responses are the average, the slope of centered T, and, in general, all orthogonal contrasts for a polynomial up to T^(p – 1), where p is the number of rows in the within-subject model.anova
multipliesY
, the response you use in the repeated measures modelrm
by the orthogonal contrasts, and uses the columns of the resulting product matrix as the responses.anova
computes the orthogonal contrasts for T using the Q factor of a QR factorization of the Vandermonde matrix.A character vector or string scalar that defines a model specification in the within-subject factors. Responses are defined by the terms in that model.
anova
multiplies Y, the response matrix you use in the repeated measures modelrm
by the terms of the model, and uses the columns of the result as the responses.For example, if there is a Time factor and
'Time'
is the model specification, thenanova
uses two terms, the constant and the uncentered Time term. The default is'1'
to perform on the average response.An r-by-nc matrix, C, specifying nc contrasts among the r repeated measures. If Y represents the matrix of repeated measures you use in the repeated measures model
rm
, then the outputtbl
contains a separate analysis of variance for each column of Y*C.
The anova
table contains a separate univariate
analysis of variance results for each response.
Example: 'WithinModel','Time'
Example: 'WithinModel','orthogonalcontrasts'
Output Arguments
Results of analysis of variance for between-subject effects, returned as a table. This includes all terms on the between-subjects model and the following columns.
Column Name | Definition |
---|---|
Within | Within-subject factors |
Between | Between-subject factors |
SumSq | Sum of squares |
DF | Degrees of freedom |
MeanSq | Mean squared error |
F | F-statistic |
pValue | p-value corresponding to the F-statistic |
More About
Vandermonde matrix is the matrix where columns are the powers of the vector a, that is, V(i,j) = a(i)(n — j), where n is the length of a.
QR factorization of an m-by-n matrix A is the factorization that matrix into the product A = Q*R, where R is an m-by-n upper triangular matrix and Q is an m-by-m unitary matrix.
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.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)