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

collapse all

### `t` — Input datatable

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 specificationstring of the form `'y1-yk ~ terms'`

Formula for model specification, specified as a string 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, char arrays, or a cell array of strings.

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

## Properties

collapse all

### `BetweenDesign ` — Design for between-subject factorstable

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

Data Types: `table`

### `BetweenModel` — Model for between-subjects factorsstring

Model for between-subjects factors, stored as a string. This string 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 factorscell array of strings

Names of variables used as between-subject factors in the repeated measures model, `rm`, stored as a cell array of strings.

Data Types: `cell`

### `ResponseNames` — Names of variables used as response variablescell array of strings

Names of variables used as response variables in the repeated measures model, `rm`, stored as a cell array of strings.

Data Types: `cell`

### `WithinDesign` — Values of within-subject factorstable

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

Data Types: `table`

### `WithinModel` — Model for within-subjects factorsstring

Model for within-subjects factors, stored as a string.

Data Types: `char`

### `WithinFactorNames` — Names of within-subject factorscell array of strings

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

Data Types: `cell`

### `Coefficients` — Values of estimated coefficientstable

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 covariancestable

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 freedomscalar 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

## Definitions

### 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 NotationDescription
`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 NotationFactors in Standard Notation
`1`Constant (intercept) term
`X^k`, where `k` is a positive integer`X`, `X2`, ..., `Xk`
`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`.

## Examples

collapse all

### Fit a Repeated Measures Model

`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 = 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:

$\text{species_setosa}=\left\{\begin{array}{c}1,\text{ }if\text{\hspace{0.17em}}setosa\text{ }\text{ }\\ 0,\text{ }if\text{\hspace{0.17em}}versicolor\\ -1,\text{ }if\text{\hspace{0.17em}}virginica\text{ }\end{array}\text{ }\text{and}\text{ }\text{species_versicolor}=\left\{\begin{array}{c}0,\text{\hspace{0.17em}}\text{ }if\text{\hspace{0.17em}}setosa\text{\hspace{0.17em}}\text{ }\\ 1,\text{ }if\text{\hspace{0.17em}}versicolor\\ -1,\text{ }if\text{\hspace{0.17em}}virginica\text{ }\end{array}$

Display the covariance matrix.

`rm.Covariance`
```ans = 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.