# random

**Class: **GeneralizedLinearMixedModel

Generate random responses from fitted generalized linear mixed-effects model

## Description

returns
simulated responses using additional options specified by one or more `ysim`

= random(___,`Name,Value`

)`Name,Value`

pair
arguments, using any of the previous syntaxes. For example, you can
specify observation weights, binomial sizes, or offsets for the model.

## Input Arguments

`glme`

— Generalized linear mixed-effects model

`GeneralizedLinearMixedModel`

object

Generalized linear mixed-effects model, specified as a `GeneralizedLinearMixedModel`

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

.

`tblnew`

— New input data

table | dataset array

New input data, which includes the response variable, predictor
variables, and grouping
variables, specified as a table or dataset array. The predictor
variables can be continuous or grouping variables. `tblnew`

must
contain the same variables as the original table or dataset array, `tbl`

,
used to fit the generalized linear mixed-effects model `glme`

.

### Name-Value Arguments

Specify optional pairs of arguments as
`Name1=Value1,...,NameN=ValueN`

, where `Name`

is
the argument name and `Value`

is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.

*
Before R2021a, use commas to separate each name and value, and enclose*
`Name`

*in quotes.*

`BinomialSize`

— Number of trials for binomial distribution

`ones(m,1)`

(default) | *m*-by-1 vector of positive integer values

Number of trials for binomial distribution, specified as the
comma-separated pair consisting of `'BinomialSize'`

and
an *m*-by-1 vector of positive integer values, where *m* is
the number of rows in `tblnew`

. The `'BinomialSize'`

name-value
pair applies only to the binomial distribution. The value specifies
the number of binomial trials when generating the random response
values.

**Data Types: **`single`

| `double`

`Offset`

— Model offset

`zeros(m,1)`

(default) | vector of scalar values

Model offset, specified as a vector of scalar values of length *m*,
where *m* is the number of rows in `tblnew`

.
The offset is used as an additional predictor and has a coefficient
value fixed at `1`

.

`Weights`

— Observation weights

*m*-by-1 vector of nonnegative scalar values

Observation weights, specified as the comma-separated pair consisting
of `'Weights'`

and an *m*-by-1
vector of nonnegative scalar values, where *m* is
the number of rows in `tblnew`

. If the response
distribution is binomial or Poisson, then `'Weights'`

must
be a vector of positive integers.

**Data Types: **`single`

| `double`

## Output Arguments

`ysim`

— Simulated response values

*m*-by-1 vector

Simulated response values, returned as an *m*-by-1
vector, where *m* is the number of rows in `tblnew`

. `random`

creates `ysim`

by
first generating the random-effects vector based on its fitted prior
distribution. `random`

then generates `ysim`

from
its fitted conditional distribution given the random effects. `random`

takes
into account the effect of observation weights specified when fitting
the model using `fitglme`

, if any.

## Examples

### Simulate Random Responses from a GLME Model

Load the sample data.

`load mfr`

This simulated data is from a manufacturing company that operates 50 factories across the world, with each factory running a batch process to create a finished product. The company wants to decrease the number of defects in each batch, so it developed a new manufacturing process. To test the effectiveness of the new process, the company selected 20 of its factories at random to participate in an experiment: Ten factories implemented the new process, while the other ten continued to run the old process. In each of the 20 factories, the company ran five batches (for a total of 100 batches) and recorded the following data:

Flag to indicate whether the batch used the new process (

`newprocess`

)Processing time for each batch, in hours (

`time`

)Temperature of the batch, in degrees Celsius (

`temp`

)Categorical variable indicating the supplier (

`A`

,`B`

, or`C`

) of the chemical used in the batch (`supplier`

)Number of defects in the batch (

`defects`

)

The data also includes `time_dev`

and `temp_dev`

, which represent the absolute deviation of time and temperature, respectively, from the process standard of 3 hours at 20 degrees Celsius.

Fit a generalized linear mixed-effects model using `newprocess`

, `time_dev`

, `temp_dev`

, and `supplier`

as fixed-effects predictors. Include a random-effects term for intercept grouped by `factory`

, to account for quality differences that might exist due to factory-specific variations. The response variable `defects`

has a Poisson distribution, and the appropriate link function for this model is log. Use the Laplace fit method to estimate the coefficients. Specify the dummy variable encoding as `'effects'`

, so the dummy variable coefficients sum to 0.

The number of defects can be modeled using a Poisson distribution

$${\text{defects}}_{ij}\sim \text{Poisson}({\mu}_{ij})$$

This corresponds to the generalized linear mixed-effects model

$$\mathrm{log}({\mu}_{ij})={\beta}_{0}+{\beta}_{1}{\text{newprocess}}_{ij}+{\beta}_{2}{\text{time}\text{\_}\text{dev}}_{ij}+{\beta}_{3}{\text{temp}\text{\_}\text{dev}}_{ij}+{\beta}_{4}{\text{supplier}\text{\_}\text{C}}_{ij}+{\beta}_{5}{\text{supplier}\text{\_}\text{B}}_{ij}+{b}_{i},$$

where

$${\text{defects}}_{ij}$$ is the number of defects observed in the batch produced by factory $$i$$ during batch $$j$$.

$${\mu}_{ij}$$ is the mean number of defects corresponding to factory $$i$$ (where $$i=1,2,...,20$$) during batch $$j$$ (where $$j=1,2,...,5$$).

$${\text{newprocess}}_{ij}$$, $${\text{time}\text{\_}\text{dev}}_{ij}$$, and $${\text{temp}\text{\_}\text{dev}}_{ij}$$ are the measurements for each variable that correspond to factory $$i$$ during batch $$j$$. For example, $${\text{newprocess}}_{ij}$$ indicates whether the batch produced by factory $$i$$ during batch $$j$$ used the new process.

$${\text{supplier}\text{\_}\text{C}}_{ij}$$ and $${\text{supplier}\text{\_}\text{B}}_{ij}$$ are dummy variables that use effects (sum-to-zero) coding to indicate whether company

`C`

or`B`

, respectively, supplied the process chemicals for the batch produced by factory $$i$$ during batch $$j$$.$${b}_{i}\sim N(0,{\sigma}_{b}^{2})$$ is a random-effects intercept for each factory $$i$$ that accounts for factory-specific variation in quality.

glme = fitglme(mfr,'defects ~ 1 + newprocess + time_dev + temp_dev + supplier + (1|factory)','Distribution','Poisson','Link','log','FitMethod','Laplace','DummyVarCoding','effects');

Use `random`

to simulate a new response vector from the fitted model.

rng(0,'twister'); % For reproducibility ynew = random(glme);

Display the first 10 rows of the simulated response vector.

ynew(1:10)

`ans = `*10×1*
3
3
1
7
5
8
7
9
5
9

Simulate a new response vector using new input values. Create a new table by copying the first 10 rows of `mfr`

into `tblnew`

.

tblnew = mfr(1:10,:);

The first 10 rows of `mfr`

include data collected from trials 1 through 5 for factories 1 and 2. Both factories used the old process for all of their trials during the experiment, so `newprocess = 0`

for all 10 observations.

Change the value of `newprocess`

to `1`

for the observations in `tblnew`

.

tblnew.newprocess = ones(height(tblnew),1);

Simulate new responses using the new input values in `tblnew`

.

ynew2 = random(glme,tblnew)

`ynew2 = `*10×1*
2
3
5
4
2
2
2
1
2
0

## More About

### Conditional Distribution Method

`random`

generates random data from the fitted
generalized linear mixed-effects model as follows:

Sample $${b}_{sim}\sim P\left(b|\widehat{\theta},{\widehat{\sigma}}^{2}\right)$$, where $$P\left(b|\widehat{\theta},{\widehat{\sigma}}^{2}\right)$$ is the estimated prior distribution of random effects, and $$\widehat{\theta}$$ is a vector of estimated covariance parameters, and $${\widehat{\sigma}}^{2}$$ is the estimated dispersion parameter.

Given

*b*, for_{sim}*i*= 1 to*m*, sample $${y}_{sim\_i}\sim P\left({y}_{new\_i}|{b}_{sim},\widehat{\beta},\widehat{\theta},{\widehat{\sigma}}^{2}\right)$$, where $$P\left({y}_{new\_i}|{b}_{sim},\widehat{\beta},\widehat{\theta},{\widehat{\sigma}}^{2}\right)$$ is the conditional distribution of the*i*th new response*y*given_{new_i}*b*and the model parameters._{sim}

## See Also

## Apri esempio

Si dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?

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