Main Content

# random

Class: RepeatedMeasuresModel

Generate new random response values given predictor values

## Syntax

``ysim = random(rm,tnew)``

## Description

example

````ysim = random(rm,tnew)` generates random response values from the repeated measures model `rm` using the predictor variables from table `tnew`.```

## Input Arguments

expand all

Repeated measures model, returned as a `RepeatedMeasuresModel` object.

For properties and methods of this object, see `RepeatedMeasuresModel`.

New data including the values of the response variables and the between-subject factors used as predictors in the repeated measures model, `rm`, specified as a table. `tnew` must contain all of the between-subject factors used to create `rm`.

## Output Arguments

expand all

Random response values random generates, returned as an n-by-r matrix, where n is the number of rows in `tnew`, and r is the number of repeated measures in `rm`.

## Examples

expand all

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 = dataset([1 2 3 4]','VarNames',{'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);`

Randomly generate new response values.

`ysim = random(rm);`

`random` uses the predictor values in the original sample data you use to fit the repeated measures model `rm` in table `t`.

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.

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);`

Define a table with new values for the predictor variables.

`tnew = table(16,93,{'B'},{'Male'},'VariableNames',{'Age','IQ','Group','Gender'})`
```tnew=1×4 table Age IQ Group Gender ___ __ _____ ________ 16 93 {'B'} {'Male'} ```

Randomly generate new response values using the values in the new table `tnew`.

`ysim = random(rm,tnew)`
```ysim = 1×8 46.2252 66.8003 -40.4987 -1.9930 27.5213 -37.9809 4.8905 -3.7568 ```

## Algorithms

`random` computes `ysim` by creating predicted values and adding random noise values. For each row, the noise has a multivariate normal distribution with covariance the same as `rm.Covariance`.