# RepeatedMeasuresModel class

Repeated measures model class

## Description

A `RepeatedMeasuresModel`

object represents
a model fitted to data with multiple measurements per subject. The
object comprises data, fitted coefficients, covariance parameters,
design matrix, error degrees of freedom, and between- and within-subjects
factor names for a repeated measures model. You can predict model
responses using the `predict`

method and generate
random data at new design points using the `random`

method.

## Construction

You can fit a repeated measures model using `fitrm(t,modelspec)`

.

### Input Arguments

`t`

— Input data

table

Input data, which includes the values of the response variables and the between-subject factors to use as predictors in the repeated measures model, specified as a table.

**Data Types: **`table`

`modelspec`

— Formula for model specification

character vector or string scalar of the form ```
'y1-yk ~
terms'
```

Formula for model specification, specified as a character vector or string scalar of the form
`'y1-yk ~ terms'`

. Specify the terms using
Wilkinson notation. `fitrm`

treats the variables used
in model terms as categorical if they are categorical (nominal or
ordinal), logical, character arrays, string arrays, or a cell array of
character vectors.

**Example: **`'y1-y4 ~ x1 + x2 * x3'`

**Data Types: **`char`

| `string`

## Properties

`BetweenDesign `

— Design for between-subject factors

table

Design for between-subject factors and values of repeated measures, stored as a table.

**Data Types: **`table`

`BetweenModel`

— Model for between-subjects factors

character vector

Model for between-subjects factors, stored as a character vector.
This character vector is the text representation to the right of the
tilde in the model specification you provide when fitting the repeated
measures model using `fitrm`

.

**Data Types: **`char`

`BetweenFactorNames`

— Names of variables used as between-subject factors

cell array of character vectors

Names of variables used as between-subject factors in the repeated
measures model, `rm`

, stored as a cell array of
character vectors.

**Data Types: **`cell`

`ResponseNames`

— Names of variables used as response variables

cell array of character vectors

Names of variables used as response variables in the repeated
measures model, `rm`

, stored as a cell array of
character vectors.

**Data Types: **`cell`

`WithinDesign`

— Values of within-subject factors

table

Values of the within-subject factors, stored as a table.

**Data Types: **`table`

`WithinModel`

— Model for within-subjects factors

character vector

Model for within-subjects factors, stored as a character vector.

You can specify `WithinModel`

as a character vector or
a string scalar using dot notation: ```
Mdl.WithinModel =
newWithinModelValue
```

.

`WithinFactorNames`

— Names of within-subject factors

cell array of character vectors

Names of the within-subject factors, stored as a cell array of character vectors.

**Data Types: **`cell`

`Coefficients`

— Values of estimated coefficients

table

Values of the estimated coefficients for fitting the repeated measures as a function of the terms in the between-subjects model, stored as a table.

`fitrm'`

defines the coefficients for a categorical
term using 'effects' coding, which means coefficients sum to 0. There
is one coefficient for each level except the first. The implied coefficient
for the first level is the sum of the other coefficients for the term.

You can display the coefficient values as a matrix rather than
a table using `coef = r.Coefficients{:,:}`

.

You can display marginal means for all levels using the `margmean`

method.

**Data Types: **`table`

`Covariance`

— Estimated response covariances

table

Estimated response covariances, that is, covariance of the repeated
measures, stored as a table. `fitrm`

computes the
covariances around the mean returned by the fitted repeated measures
model `rm`

.

You can display the covariance values as a matrix rather than
a table using `coef = r.Covariance{:,:}`

.

**Data Types: **`table`

`DFE`

— Error degrees of freedom

scalar value

Error degrees of freedom, stored as a scalar value. `DFE`

is
the number of observations minus the number of estimated coefficients
in the between-subjects model.

**Data Types: **`double`

## Methods

anova | Analysis of variance for between-subject effects |

epsilon | Epsilon adjustment for repeated measures anova |

grpstats | Compute descriptive statistics of repeated measures data by group |

manova | Multivariate analysis of variance |

margmean | Estimate marginal means |

mauchly | Mauchly’s test for sphericity |

multcompare | Multiple comparison of estimated marginal means |

plot | Plot data with optional grouping |

plotprofile | Plot expected marginal means with optional grouping |

predict | Compute predicted values given predictor values |

random | Generate new random response values given predictor values |

ranova | Repeated measures analysis of variance |

## Examples

### Fit a Repeated Measures Model

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)

rm = RepeatedMeasuresModel with properties: Between Subjects: BetweenDesign: [150x5 table] ResponseNames: {'meas1' 'meas2' 'meas3' 'meas4'} BetweenFactorNames: {'species'} BetweenModel: '1 + species' Within Subjects: WithinDesign: [4x1 table] WithinFactorNames: {'Measurements'} WithinModel: 'separatemeans' Estimates: Coefficients: [3x4 table] Covariance: [4x4 table]

Display the coefficients.

rm.Coefficients

`ans=`*3×4 table*
meas1 meas2 meas3 meas4
________ ________ ______ ________
(Intercept) 5.8433 3.0573 3.758 1.1993
species_setosa -0.83733 0.37067 -2.296 -0.95333
species_versicolor 0.092667 -0.28733 0.502 0.12667

`fitrm`

uses the `'effects'`

contrasts, which means that the coefficients sum to 0. The `rm.DesignMatrix`

has one column of 1s for the intercept, and two other columns `species_setosa`

and `species_versicolor`

, which are as follows:

$$species\_setosa=\{\begin{array}{c}1,\phantom{\rule{1em}{0ex}}if\phantom{\rule{0.2777777777777778em}{0ex}}setosa\phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}\\ 0,\phantom{\rule{1em}{0ex}}if\phantom{\rule{0.2777777777777778em}{0ex}}versicolor\\ -1,\phantom{\rule{1em}{0ex}}if\phantom{\rule{0.2777777777777778em}{0ex}}virginica\phantom{\rule{1em}{0ex}}\end{array}$$

and

$$species\_versicolor=\{\begin{array}{c}0,\phantom{\rule{0.2777777777777778em}{0ex}}\phantom{\rule{1em}{0ex}}if\phantom{\rule{0.2777777777777778em}{0ex}}setosa\phantom{\rule{0.2777777777777778em}{0ex}}\phantom{\rule{1em}{0ex}}\\ 1,\phantom{\rule{1em}{0ex}}if\phantom{\rule{0.2777777777777778em}{0ex}}versicolor\\ -1,\phantom{\rule{1em}{0ex}}if\phantom{\rule{0.2777777777777778em}{0ex}}virginica\phantom{\rule{1em}{0ex}}\end{array}.$$

Display the covariance matrix.

rm.Covariance

`ans=`*4×4 table*
meas1 meas2 meas3 meas4
________ ________ ________ ________
meas1 0.26501 0.092721 0.16751 0.038401
meas2 0.092721 0.11539 0.055244 0.03271
meas3 0.16751 0.055244 0.18519 0.042665
meas4 0.038401 0.03271 0.042665 0.041882

Display the error degrees of freedom.

rm.DFE

ans = 147

The error degrees of freedom is the number of observations minus the number of estimated coefficients in the between-subjects model, e.g. 150 – 3 = 147.

## More About

### Wilkinson Notation

Wilkinson notation describes the factors present in models. It does not describe the multipliers (coefficients) of those factors.

Use these rules to specify the responses in `modelspec`

.

Wilkinson Notation | Description |
---|---|

`Y1,Y2,Y3` | Specific list of variables |

`Y1-Y5` | All table variables from `Y1` through `Y5` |

Use these rules to specify terms in `modelspec`

.

Wilkinson Notation | Factors in Standard Notation |
---|---|

`1` | Constant (intercept) term |

`X^k` , where `k` is a positive
integer | `X` , `X` ,
..., `X` |

`X1 + X2` | `X1` , `X2` |

`X1*X2` | `X1` , `X2` , `X1*X2` |

`X1:X2` | `X1*X2` only |

`-X2` | Do not include `X2` |

`X1*X2 + X3` | `X1` , `X2` , `X3` , `X1*X2` |

`X1 + X2 + X3 + X1:X2` | `X1` , `X2` , `X3` , `X1*X2` |

`X1*X2*X3 - X1:X2:X3` | `X1` , `X2` , `X3` , `X1*X2` , `X1*X3` , `X2*X3` |

`X1*(X2 + X3)` | `X1` , `X2` , `X3` , `X1*X2` , `X1*X3` |

Statistics and Machine Learning Toolbox™ notation always includes a constant term
unless you explicitly remove the term using `-1`

.

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## 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)