`lme`

— Linear mixed-effects model`LinearMixedModel`

objectLinear mixed-effects model, returned as a `LinearMixedModel`

object.

For properties and methods of this object, see `LinearMixedModel`

.

`gnumbers`

— Grouping variable numbersinteger arrayGrouping variable numbers, specified as an integer array, where *R* is
the length of the cell array that contains the grouping variables
for the linear mixed-effects model `lme`

.

For example, you can specify the grouping variables g_{1},
g_{3}, and g_{r} as
follows.

**Example: **`[1,3,r]`

**Data Types: **`double`

| `single`

`D`

— Design matrixmatrixDesign matrix of a linear mixed-effects model `lme`

returned
as one of the following:

Fixed-effects design matrix —

*n*-by-*p*matrix consisting of the fixed-effects design of`lme`

, where*n*is the number of observations and*p*is the number of fixed-effects terms.Random-effects design matrix —

*n*-by-*k*matrix, consisting of the random-effects design matrix of`lme`

. Here,*k*is equal to`length(B)`

, where`B`

is the random-effects coefficients vector of linear mixed-effects model`lme`

.If

`lme`

has*R*grouping variables g_{1}, g_{2}, ..., g_{R}, with levels*m*_{1},*m*_{2}, ...,*m*_{R}, respectively, and if*q*_{1},*q*_{2}, ...,*q*_{R}are the lengths of the random-effects vectors that are associated with g_{1}, g_{2}, ..., g_{R}, respectively, then`B`

is a column vector of length*q*_{1}**m*_{1}+*q*_{2}**m*_{2}+ ... +*q*_{R}**m*_{R}.`B`

is made by concatenating the best linear unbiased predictors of random-effects vectors corresponding to each level of each grouping variable as`[g`

._{1}level_{1}; g_{1}level_{2}; ...; g_{1}level_{m1}; g_{2}level_{1}; g_{2}level_{2}; ...; g_{2}level_{m2}; ...; g_{R}level_{1}; g_{R}level_{2}; ...; g_{R}level_{mR}]'

**Data Types: **`single`

| `double`

`Dsub`

— Submatrix of random-effects design matrixmatrixSubmatrix of random-effects design matrix corresponding to the
grouping variables indicated by the integers in `gnumbers`

,
returned as an *n*-by-*k* matrix,
where *k* is length of the column vector `Bsub`

.

`Bsub`

contains the concatenated best linear
unbiased predictors (BLUPs) of random-effects vectors, corresponding
to each level of the grouping variables, specified by `gnumbers`

.

If, for example, `gnumbers`

is `[1,3,r]`

,
this corresponds to the grouping variables g_{1},
g_{3}, and g_{r}.
Then, `Bsub`

contains the concatenated BLUPs of random-effects
vectors corresponding to each level of the grouping variables g_{1},
g_{3}, and g* _{r}*,
such as

`[g`

. _{1}level_{1};
g_{1}level_{2}; ...; g_{1}level_{m}_{1};
g_{3}level_{1}; g_{3}level_{2};
...; g_{3}level_{m}_{3};
g* _{r}*level

Thus, `Dsub*Bsub`

represents the contribution
of all random effects corresponding to grouping variables g_{1},
g_{3}, and g* _{r}* to
the response of

`lme`

.If `gnumbers`

is empty, then `Dsub`

is
the full random-effects design matrix.

**Data Types: **`single`

| `double`

`gnames`

— Names of grouping variablesNames of grouping variables corresponding to the integers in `gnumbers`

if
the design type is `'Random'`

, returned as a *k*-by-1
cell array. If the design type is `'Fixed'`

, then `gnames`

is
an empty matrix `[]`

.

**Data Types: **`cell`

Navigate to a folder containing sample data.

```
cd(matlabroot)
cd('help/toolbox/stats/examples')
```

Load the sample data.

`load shift`

The data shows the deviations from the target quality characteristic measured from the products that 5 operators manufacture during three different shifts, morning, evening, and night. This is a randomized block design, where the operators are the blocks. The experiment is designed to study the impact of the time of shift on the performance. The performance measure is the deviation of the quality characteristics from the target value. This is simulated data.

`Shift`

and `Operator`

are
nominal variables.

shift.Shift = nominal(shift.Shift); shift.Operator = nominal(shift.Operator);

Fit a linear mixed-effects model with a random intercept grouped by operator to assess if performance significantly differs according to the time of the shift.

`lme = fitlme(shift,'QCDev ~ Shift + (1|Operator)');`

Display the fixed-effects design matrix.

designMatrix(lme)

ans = 1 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1

The column of 1s represents the constant term in the model. `fitlme`

takes
the evening shift as the reference group and creates two dummy variables
to represent the morning and night shifts, respectively.

Display the random-effects design matrix.

`designMatrix(lme,'random')`

ans = (1,1) 1 (2,1) 1 (3,1) 1 (4,2) 1 (5,2) 1 (6,2) 1 (7,3) 1 (8,3) 1 (9,3) 1 (10,4) 1 (11,4) 1 (12,4) 1 (13,5) 1 (14,5) 1 (15,5) 1

The first number, *i*, in the (*i*,*j*)
indices corresponds to the observation number, and *j* corresponds
to the level of the grouping variable, `Operator`

,
i.e., the operator number.

Show the full display of the random-effects design matrix.

`full(designMatrix(lme,'random'))`

ans = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1

Each column corresponds to a level of the grouping variable, `Operator`

.

Navigate to a folder containing sample data.

```
cd(matlabroot)
cd('help/toolbox/stats/examples')
```

Load the sample data.

`load fertilizer`

The dataset array includes data from a split-plot experiment, where soil is divided into three blocks based on the soil type: sandy, silty, and loamy. Each block is divided into five plots, where five different types of tomato plants (cherry, heirloom, grape, vine, and plum) are randomly assigned to these plots. The tomato plants in the plots are then divided into subplots, where each subplot is treated by one of four fertilizers. This is simulated data.

Store the data in a dataset array called `ds`

,
for practical purposes, and define `Tomato`

, `Soil`

,
and `Fertilizer`

as categorical variables.

ds = fertilizer; ds.Tomato = nominal(ds.Tomato); ds.Soil = nominal(ds.Soil); ds.Fertilizer = nominal(ds.Fertilizer);

Fit a linear mixed-effects model, where `Fertilizer`

and `Tomato`

are
the fixed-effects variables, and the mean yield varies by the block
(soil type), and the plots within blocks (tomato types within soil
types) independently.

`lme = fitlme(ds,'Yield ~ Fertilizer * Tomato + (1|Soil) + (1|Soil:Tomato)');`

Store and examine the full random-effects design matrix.

`D = full(designMatrix(lme,'random'));`

The first three columns of matrix `D`

contain
the indicator variables `fitlme`

creates for the
three levels (`Loamy`

, `Silty`

, `Sandy`

,
respectively) of the first grouping variable, `Soi`

l.
The next 15 columns contain the indicator variables created for the
second grouping variable, `Tomato`

nested under `Soil`

.
These are basically the elementwise products of the dummy variables
representing the levels of `Soil`

(`Loamy`

, `Silty`

,
and `Sandy`

, respectively) and the levels of `Tomato`

(`Cherry`

, `Grape`

, `Heirloom`

, `Plum`

, `Vine`

,
respectively).

Navigate to a folder containing sample data.

```
cd(matlabroot)
cd('help/toolbox/stats/examples')
```

Load the sample data.

`load fertilizer`

The dataset array includes data from a split-plot experiment, where soil is divided into three blocks based on the soil type: sandy, silty, and loamy. Each block is divided into five plots, where five different types of tomato plants (cherry, heirloom, grape, vine, and plum) are randomly assigned to these plots. The tomato plants in the plots are then divided into subplots, where each subplot is treated by one of four fertilizers. This is simulated data.

Store the data in a dataset array called `ds`

,
for practical purposes, and define `Tomato`

, `Soil`

,
and `Fertilizer`

as categorical variables.

ds = fertilizer; ds.Tomato = nominal(ds.Tomato); ds.Soil = nominal(ds.Soil); ds.Fertilizer = nominal(ds.Fertilizer);

Fit a linear mixed-effects model, where `Fertilizer`

and `Tomato`

are
the fixed-effects variables, and the mean yield varies by the block
(soil type), and the plots within blocks (tomato types within soil
types) independently.

`lme = fitlme(ds,'Yield ~ Fertilizer * Tomato + (1|Soil) + (1|Soil:Tomato)');`

Compute the random-effects design matrix for the second grouping variable, and display the first 12 rows.

```
[Dsub,gname] = designMatrix(lme,'random',2);
full(Dsub(1:12,:))
```

ans = 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0

`Dsub`

contains the dummy variables created
for the second grouping variable, that is, tomato nested under soil.
These are the elementwise products of the dummy variables representing
the levels of `Soil`

(`Loamy`

, `Silty`

, `Sandy`

,
respectively) and the levels of `Tomato`

(`Cherry`

, `Grape`

, `Heirloom`

, `Plum`

, `Vine`

,
respectively).

Display the name of the grouping variable.

gname

gname = 'Soil:Tomato'

Was this topic helpful?