# designMatrix

Fixed- and random-effects design matrices

## Syntax

## Description

## Examples

### Display Fixed- and Random-Effects Design Matrices

Load the sample data.

`load('shift.mat');`

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 = `*15×3*
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
⋮

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 = `*15×5*
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
⋮

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

.

### Random-Effects Design Matrix of Multiple Grouping Variables

Load the sample data.

`load('fertilizer.mat');`

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, `Soil`

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

### Subset of the Random-Effects Design Matrix

Load the sample data.

`load('fertilizer.mat');`

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 = `*12×15*
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 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 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 = `*1x1 cell array*
{'Soil:Tomato'}

## Input Arguments

`lme`

— Linear mixed-effects model

`LinearMixedModel`

object

Linear mixed-effects model, specified as a `LinearMixedModel`

object constructed using `fitlme`

or `fitlmematrix`

.

`gnumbers`

— Grouping variable numbers

integer array

Grouping 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`

## Output Arguments

`D`

— Design matrix

matrix

Design 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. The order of fixed-effects terms in`D`

matches the order of terms in the`CoefficientNames`

property of the`LinearMixedModel`

object`lme`

.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 matrix

matrix

Submatrix 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

_{1}; g

*level*

_{r}_{2}; ...; g

*level*

_{r}*]'*

_{mr}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 variables

*k*-by-1 cell array

Names 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`

## Version History

**Introduced in R2013b**

## See Also

## Comando MATLAB

Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:

Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.

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)