# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

# randomEffects

Class: LinearMixedModel

Estimates of random effects and related statistics

## Syntax

``B = randomEffects(lme)``
``````[B,Bnames] = randomEffects(lme)``````
``````[B,Bnames,stats] = randomEffects(lme)``````
``````[B,Bnames,stats] = randomEffects(lme,Name,Value)``````

## Description

example

````B = randomEffects(lme)` returns the estimates of the best linear unbiased predictors (BLUPs) of random effects in the linear mixed-effects model `lme`.```

example

``````[B,Bnames] = randomEffects(lme)``` also returns the names of the coefficients in `Bnames`. Each name corresponds to a coefficient in `B`.```

example

``````[B,Bnames,stats] = randomEffects(lme)``` also returns the estimated BLUPs of random effects in the linear mixed-effects model `lme` and related statistics.```

example

``````[B,Bnames,stats] = randomEffects(lme,Name,Value)``` also returns the BLUPs of random effects in the linear mixed-effects model `lme` and related statistics with additional options specified by one or more `Name,Value` pair arguments.```

## Input Arguments

expand all

Linear mixed-effects model, specified as a `LinearMixedModel` object constructed using `fitlme` or `fitlmematrix`.

### Name-Value Pair 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 single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

expand all

Confidence level, specified as the comma-separated pair consisting of `'Alpha'` and a scalar value in the range 0 to 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`

Method for computing approximate degrees of freedom for the t-statistics that test the random-effects coefficients against 0, specified as the comma-separated pair consisting of `'DFMethod'` and one of the following.

 `'residual'` Default. The degrees of freedom are assumed to be constant and equal to n – p, where n is the number of observations and p is the number of fixed effects. `'satterthwaite'` Satterthwaite approximation. `'none'` All degrees of freedom are set to infinity.

For example, you can specify the Satterthwaite approximation as follows.

Example: `'DFMethod','satterthwaite'`

## Output Arguments

expand all

Estimated best linear unbiased predictors of random effects of linear mixed-effects model `lme`, returned as a column vector.

Suppose `lme` has R grouping variables g1, g2, ..., gR, with levels m1, m2, ..., mR, respectively. Also suppose q1, q2, ..., qR are the lengths of the random-effects vectors that are associated with g1, g2, ..., gR, respectively. Then, `B` is a column vector of length q1*m1 + q2*m2 + ... + qR*mR.

`randomEffects` creates `B` by concatenating the best linear unbiased predictors of random-effects vectors corresponding to each level of each grouping variable as ```[g1level1; g1level2; ...; g1levelm1; g2level1; g2level2; ...; g2levelm2; ...; gRlevel1; gRlevel2; ...; gRlevelmR]'```.

Names of random-effects coefficients in `B`, returned as a table.

Estimates of random effects BLUPs and related statistics, returned as a dataset array that has one row for each of the fixed effects and one column for each of the following statistics.

 `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` Best linear unbiased predictor (BLUP) of random effect `SEPred` Standard error of the estimate (BLUP minus random effect) `tStat` t-statistic for a test that the random effect is zero `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 effect `Upper` Upper limit of a 95% confidence interval for the random effect

## Examples

expand all

Load the sample data.

```load carbig ```

Fit a linear mixed-effects model for miles per gallon (MPG), with fixed effects for acceleration and horsepower, and potentially correlated random effects for intercept and acceleration, grouped by the model year. First, store the data in a table.

```tbl = table(Acceleration,Horsepower,Model_Year,MPG); ```

Fit the model.

```lme = fitlme(tbl, 'MPG ~ Acceleration + Horsepower + (Acceleration|Model_Year)'); ```

Compute the BLUPs of the random-effects coefficients and display the names of the corresponding random effects.

```[B,Bnames] = randomEffects(lme) ```
```B = 3.1270 -0.2426 -1.6532 -0.0086 1.2075 -0.2179 4.4107 -0.4887 -1.3103 -0.0208 2.8029 -0.3790 0.0865 -0.1280 0.4216 -0.0259 -2.3889 0.1634 0.9618 0.0117 -2.2345 0.5020 -2.1332 0.3254 -3.2979 0.5090 Bnames = 26x3 table Group Level Name ____________ _____ ______________ 'Model_Year' '70' '(Intercept)' 'Model_Year' '70' 'Acceleration' 'Model_Year' '71' '(Intercept)' 'Model_Year' '71' 'Acceleration' 'Model_Year' '72' '(Intercept)' 'Model_Year' '72' 'Acceleration' 'Model_Year' '73' '(Intercept)' 'Model_Year' '73' 'Acceleration' 'Model_Year' '74' '(Intercept)' 'Model_Year' '74' 'Acceleration' 'Model_Year' '75' '(Intercept)' 'Model_Year' '75' 'Acceleration' 'Model_Year' '76' '(Intercept)' 'Model_Year' '76' 'Acceleration' 'Model_Year' '77' '(Intercept)' 'Model_Year' '77' 'Acceleration' 'Model_Year' '78' '(Intercept)' 'Model_Year' '78' 'Acceleration' 'Model_Year' '79' '(Intercept)' 'Model_Year' '79' 'Acceleration' 'Model_Year' '80' '(Intercept)' 'Model_Year' '80' 'Acceleration' 'Model_Year' '81' '(Intercept)' 'Model_Year' '81' 'Acceleration' 'Model_Year' '82' '(Intercept)' 'Model_Year' '82' 'Acceleration' ```

Since intercept and acceleration have potentially correlated random effects, grouped by model year of the cars, `randomEffects` creates a separate row for intercept and acceleration at each level of the grouping variable.

Compute the covariance parameters of the random effects.

```[~,~,stats] = covarianceParameters(lme) stats{1} ```
```stats = 2x1 cell array {3x7 classreg.regr.lmeutils.titleddataset} {1x5 classreg.regr.lmeutils.titleddataset} ans = COVARIANCE TYPE: FULLCHOLESKY Group Name1 Name2 Type Model_Year '(Intercept)' '(Intercept)' 'std' Model_Year 'Acceleration' '(Intercept)' 'corr' Model_Year 'Acceleration' 'Acceleration' 'std' Estimate Lower Upper 3.3475 1.2862 8.7119 -0.87971 -0.98501 -0.29676 0.33789 0.1825 0.62558 ```

The correlation value suggests that random effects seem negatively correlated. Plot the random effects for intercept versus acceleration to confirm this.

```plot(B(1:2:end),B(2:2:end),'r*') ```

Load the sample data.

```load(fullfile(matlabroot,'examples','stats','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 BLUPs and related statistics for random effects.

```[~,~,stats] = randomEffects(lme) ```
```stats = RANDOM EFFECT COEFFICIENTS: DFMETHOD = 'RESIDUAL', ALPHA = 0.05 Group Level Name Estimate 'Soil' 'Loamy' '(Intercept)' 1.0061 'Soil' 'Sandy' '(Intercept)' -1.5236 'Soil' 'Silty' '(Intercept)' 0.51744 'Soil:Tomato' 'Loamy Cherry' '(Intercept)' 12.46 'Soil:Tomato' 'Loamy Grape' '(Intercept)' -2.6429 'Soil:Tomato' 'Loamy Heirloom' '(Intercept)' 16.681 'Soil:Tomato' 'Loamy Plum' '(Intercept)' -5.0172 'Soil:Tomato' 'Loamy Vine' '(Intercept)' -4.6874 'Soil:Tomato' 'Sandy Cherry' '(Intercept)' -17.393 'Soil:Tomato' 'Sandy Grape' '(Intercept)' -7.3679 'Soil:Tomato' 'Sandy Heirloom' '(Intercept)' -8.621 'Soil:Tomato' 'Sandy Plum' '(Intercept)' 7.669 'Soil:Tomato' 'Sandy Vine' '(Intercept)' 0.28246 'Soil:Tomato' 'Silty Cherry' '(Intercept)' 4.9326 'Soil:Tomato' 'Silty Grape' '(Intercept)' 10.011 'Soil:Tomato' 'Silty Heirloom' '(Intercept)' -8.0599 'Soil:Tomato' 'Silty Plum' '(Intercept)' -2.6519 'Soil:Tomato' 'Silty Vine' '(Intercept)' 4.405 SEPred tStat DF pValue Lower Upper 2.3374 0.43044 40 0.66918 -3.718 5.7303 2.3374 -0.65181 40 0.51825 -6.2477 3.2006 2.3374 0.22137 40 0.82593 -4.2067 5.2416 7.1765 1.7362 40 0.090224 -2.0443 26.964 7.1765 -0.36827 40 0.71461 -17.147 11.861 7.1765 2.3244 40 0.025269 2.1766 31.185 7.1765 -0.69911 40 0.48853 -19.522 9.4872 7.1765 -0.65316 40 0.51739 -19.192 9.8169 7.1765 -2.4235 40 0.019987 -31.897 -2.8882 7.1765 -1.0267 40 0.31075 -21.872 7.1364 7.1765 -1.2013 40 0.23671 -23.125 5.8833 7.1765 1.0686 40 0.29165 -6.8353 22.173 7.1765 0.039359 40 0.9688 -14.222 14.787 7.1765 0.68732 40 0.49585 -9.5718 19.437 7.1765 1.3949 40 0.17073 -4.4935 24.515 7.1765 -1.1231 40 0.2681 -22.564 6.4444 7.1765 -0.36952 40 0.71369 -17.156 11.852 7.1765 0.6138 40 0.54282 -10.099 18.909 ```

The first three rows contain the random-effects estimates and the statistics for the three levels, `Loamy`, `Sandy`, and `Silty` of the grouping variable `Soil`. The corresponding -values 0.66918, 0.51825, and 0.82593 indicate that these random-effects are not significantly different from 0. The following 15 rows include the BLUPS of random-effects estimates for the intercept, grouped by the variable Tomato nested in Soil, i.e. interaction of `Tomato` and `Soil`.

Load the sample data.

`load carsmall`

`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 there is a significant difference in the performance according to the time of the shift. Use the restricted maximum likelihood method.

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

Compute the 99% confidence intervals for random effects using the residuals option to compute the degrees of freedom. This is the default method.

```[~,~,stats] = randomEffects(lme,'alpha',0.01) ```
```stats = Random effect coefficients: DFMethod = 'Residual', Alpha = 0.01 Group Level Name Estimate SEPred tStat DF pValue Lower Upper 'Operator' '1' '(Intercept)' 0.57753 0.90378 0.63902 12 0.53482 -2.1831 3.3382 'Operator' '2' '(Intercept)' 1.1757 0.90378 1.3009 12 0.21772 -1.5849 3.9364 'Operator' '3' '(Intercept)' -2.1715 0.90378 -2.4027 12 0.033352 -4.9322 0.58909 'Operator' '4' '(Intercept)' 2.3655 0.90378 2.6174 12 0.022494 -0.39511 5.1261 'Operator' '5' '(Intercept)' -1.9472 0.90378 -2.1546 12 0.052216 -4.7079 0.81337```

Compute the 99% confidence intervals for random effects using the Satterthwaite approximation to compute the degrees of freedom.

`[~,~,stats] = randomEffects(lme,'DFMethod','satterthwaite','alpha',0.01)`
```stats = Random effect coefficients: DFMethod = 'Satterthwaite', Alpha = 0.01 Group Level Name Estimate SEPred tStat DF pValue Lower Upper 'Operator' '1' '(Intercept)' 0.57753 0.90378 0.63902 6.4253 0.5449 -2.684 3.839 'Operator' '2' '(Intercept)' 1.1757 0.90378 1.3009 6.4253 0.23799 -2.0858 4.4372 'Operator' '3' '(Intercept)' -2.1715 0.90378 -2.4027 6.4253 0.050386 -5.433 1.09 'Operator' '4' '(Intercept)' 2.3655 0.90378 2.6174 6.4253 0.037302 -0.89598 5.627 'Operator' '5' '(Intercept)' -1.9472 0.90378 -2.1546 6.4253 0.071626 -5.2087 1.3142```

The Satterhwaite method usually produces smaller values for the degrees of freedom (`DF`), which results in larger p-values (`pValue`) and larger confidence intervals (`Lower` and `Upper`) for the random-effects estimates.

## See Also

Was this topic helpful?

Download now