# randomEffects

**Class: **GeneralizedLinearMixedModel

Estimates of random effects and related statistics

## Syntax

## Description

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

.

### Name-Value Arguments

Specify optional
comma-separated pairs of `Name,Value`

arguments. `Name`

is
the argument name and `Value`

is the corresponding value.
`Name`

must appear inside quotes. You can specify several name and value
pair arguments in any order as
`Name1,Value1,...,NameN,ValueN`

.

`Alpha`

— Significance level

0.05 (default) | scalar value in the range [0,1]

Significance level, specified as the comma-separated pair consisting of
`'Alpha'`

and a scalar value in the range [0,1]. For a value α, the
confidence level is 100 × (1 – α)%.

For example, for 99% confidence intervals, you can specify the confidence level as follows.

**Example: **`'Alpha',0.01`

**Data Types: **`single`

| `double`

`DFMethod`

— Method for computing approximate degrees of freedom

`'residual'`

(default) | `'none'`

Method for computing approximate degrees of freedom, specified
as the comma-separated pair consisting of `'DFMethod'`

and
one of the following.

Value | Description |
---|---|

`'residual'` | The degrees of freedom value is assumed to be constant and equal to n –
p, where n is the number of
observations and p is the number of fixed
effects. |

`'none'` | The degrees of freedom is set to infinity. |

**Example: **`'DFMethod','none'`

## Output Arguments

`B`

— Estimated empirical Bayes predictors for the random effects

column vector

Estimated empirical Bayes predictors (EBPs) for the random effects
in the generalized linear mixed-effects model `glme`

,
returned as a column vector. The EBPs in `B`

are
approximated by the mode of the empirical posterior distribution of
the random effects given the estimated covariance parameters and the
observed response.

Suppose `glme`

has *R* grouping
variables g_{1}, g_{2}, ...,
g_{R}, with levels *m*_{1}, *m*_{2},
..., *m*_{R},
respectively. Also suppose *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}.

`randomEffects`

creates `B`

by
concatenating the empirical Bayes 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_{m}_{1};
g_{2}level_{1}; g_{2}level_{2};
...; g_{2}level_{m}_{2};
...; g* _{R}*level

_{1}; g

*level*

_{R}_{2}; ...; g

*level*

_{R}*]'*

_{mR}`BNames`

— Names of random-effects coefficients

table

Names of random-effects coefficients in `B`

,
returned as a table.

`stats`

— Estimated empirical Bayes predictors and related statistics

table

Estimated empirical Bayes predictors (EBPs) and related statistics
for the random effects in the generalized linear mixed-effects model `glme`

,
returned as a table. `stats`

has one row for each
of the random effects, and one column for each of the following statistics.

Column Name | Description |
---|---|

`Group` | Grouping variable associated with the random effect |

`Level` | Level within the grouping variable corresponding to the random effect |

`Name` | Name of the random-effect coefficient |

`Estimate` | Empirical Bayes predictor (EBP) of random effect |

`SEPred` | Square root of the conditional mean squared error of prediction (CMSEP) given covariance parameters and response |

`tStat` | t-statistic for a test that the random-effects
coefficient is equal to 0 |

`DF` | Estimated degrees of freedom for the t-statistic |

`pValue` | p-value for the t-statistic |

`Lower` | Lower limit of a 95% confidence interval for the random-effects coefficient |

`Upper` | Upper limit of a 95% confidence interval for the random-effects coefficient |

`randomEffects`

computes the confidence intervals
using the conditional mean squared error of prediction (CMSEP) approach
conditional on the estimated covariance parameters and the observed
response. An alternative interpretation of the confidence intervals
is that they are approximate Bayesian credible intervals conditional
on the estimated covariance parameters and the observed response.

When fitting a GLME model using `fitglme`

and
one of the pseudo likelihood fit methods (`'MPL'`

or `'REMPL'`

), `randomEffects`

computes
confidence intervals and related statistics based on the fitted linear
mixed-effects model from the final pseudo likelihood iteration.

## Examples

### Compute and Plot Estimated Random Effects

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

Compute and display the names and estimated values of the empirical Bayes predictors (EBPs) for the random effects.

[B,BNames] = randomEffects(glme)

`B = `*20×1*
0.2913
0.1542
-0.2633
-0.4257
0.5453
-0.1069
0.3040
-0.1653
-0.1458
-0.0816
⋮

`BNames=`*20×3 table*
Group Level Name
___________ ______ _______________
{'factory'} {'1' } {'(Intercept)'}
{'factory'} {'2' } {'(Intercept)'}
{'factory'} {'3' } {'(Intercept)'}
{'factory'} {'4' } {'(Intercept)'}
{'factory'} {'5' } {'(Intercept)'}
{'factory'} {'6' } {'(Intercept)'}
{'factory'} {'7' } {'(Intercept)'}
{'factory'} {'8' } {'(Intercept)'}
{'factory'} {'9' } {'(Intercept)'}
{'factory'} {'10'} {'(Intercept)'}
{'factory'} {'11'} {'(Intercept)'}
{'factory'} {'12'} {'(Intercept)'}
{'factory'} {'13'} {'(Intercept)'}
{'factory'} {'14'} {'(Intercept)'}
{'factory'} {'15'} {'(Intercept)'}
{'factory'} {'16'} {'(Intercept)'}
⋮

Each row of `B`

contains the estimated EPB for the random-effects coefficient named in the corresponding row of `Bnames`

. For example, the value –0.2633 in row 3 of `B`

is the estimated EPB for `'(Intercept)'`

for level `'3'`

of `factory`

.

### Compute 99% Confidence Intervals for Random Effects

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

Compute and display the 99% confidence intervals for the random-effects coefficients.

```
[B,BNames,stats] = randomEffects(glme,'Alpha',0.01);
stats
```

stats = Random effect coefficients: DFMethod = 'residual', Alpha = 0.01 Group Level Name Estimate SEPred {'factory'} {'1' } {'(Intercept)'} 0.29131 0.19163 {'factory'} {'2' } {'(Intercept)'} 0.15423 0.19216 {'factory'} {'3' } {'(Intercept)'} -0.26325 0.21249 {'factory'} {'4' } {'(Intercept)'} -0.42568 0.21667 {'factory'} {'5' } {'(Intercept)'} 0.5453 0.17963 {'factory'} {'6' } {'(Intercept)'} -0.10692 0.20133 {'factory'} {'7' } {'(Intercept)'} 0.30404 0.18397 {'factory'} {'8' } {'(Intercept)'} -0.16527 0.20505 {'factory'} {'9' } {'(Intercept)'} -0.14577 0.203 {'factory'} {'10'} {'(Intercept)'} -0.081632 0.20256 {'factory'} {'11'} {'(Intercept)'} 0.014529 0.21421 {'factory'} {'12'} {'(Intercept)'} 0.17706 0.20721 {'factory'} {'13'} {'(Intercept)'} 0.24872 0.20522 {'factory'} {'14'} {'(Intercept)'} 0.21145 0.20678 {'factory'} {'15'} {'(Intercept)'} 0.2777 0.20345 {'factory'} {'16'} {'(Intercept)'} -0.25175 0.22568 {'factory'} {'17'} {'(Intercept)'} -0.13507 0.22301 {'factory'} {'18'} {'(Intercept)'} -0.1627 0.22269 {'factory'} {'19'} {'(Intercept)'} -0.32083 0.23294 {'factory'} {'20'} {'(Intercept)'} 0.058418 0.21481 tStat DF pValue Lower Upper 1.5202 94 0.13182 -0.21251 0.79514 0.80259 94 0.42423 -0.351 0.65946 -1.2389 94 0.21846 -0.82191 0.29541 -1.9646 94 0.052408 -0.99534 0.14398 3.0356 94 0.0031051 0.073019 1.0176 -0.53105 94 0.59664 -0.63625 0.42241 1.6527 94 0.10173 -0.17964 0.78771 -0.80597 94 0.42229 -0.70438 0.37385 -0.71806 94 0.4745 -0.67949 0.38795 -0.403 94 0.68786 -0.61419 0.45093 0.067826 94 0.94607 -0.54866 0.57772 0.85446 94 0.39502 -0.36774 0.72185 1.212 94 0.22857 -0.29083 0.78827 1.0226 94 0.30913 -0.33221 0.75511 1.365 94 0.17552 -0.25719 0.81259 -1.1156 94 0.26746 -0.84509 0.34158 -0.60568 94 0.54619 -0.7214 0.45125 -0.73061 94 0.46684 -0.74817 0.42278 -1.3773 94 0.17168 -0.93325 0.29159 0.27195 94 0.78626 -0.50635 0.62319

The first three columns of `stats`

contain the group name, level, and random-effects coefficient name. Column 4 contains the estimated EBP of the random-effects coefficient. The last two columns of `stats`

, `Lower`

and `Upper`

, contain the lower and upper bounds of the 99% confidence interval, respectively. For example, for the coefficient for `'(Intercept)'`

for level `3`

of `factory`

, the estimated EBP is -0.26325, and the 99% confidence interval is [-0.82191,0.29541].

## References

[1] Booth, J.G., and J.P. Hobert. “Standard Errors
of Prediction in Generalized Linear Mixed Models.” *Journal
of the American Statistical Association*, Vol. 93, 1998,
pp. 262–272.

## See Also

`GeneralizedLinearMixedModel`

| `coefCI`

| `coefTest`

| `fixedEffects`

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

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