Documentation

# LinearMixedModel.fit

Class: LinearMixedModel

(Not Recommended) Fit linear mixed-effects model using tables

`LinearMixedModel.fit` is not recommended. Use `fitlme` instead.

## Syntax

``lme = LinearMixedModel.fit(tbl,formula)``
``lme = LinearMixedModel.fit(tbl,formula,Name,Value)``

## Description

example

````lme = LinearMixedModel.fit(tbl,formula)` returns a linear mixed-effects model, specified by `formula`, fitted to the variables in the table or dataset array `tbl`.```

example

````lme = LinearMixedModel.fit(tbl,formula,Name,Value)` returns a linear mixed-effects model with additional options specified by one or more `Name,Value` pair arguments.For example, you can specify the covariance pattern of the random-effects terms, the method to use in estimating the parameters, or options for the optimization algorithm.```

## Input Arguments

expand all

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 (see Grouping Variables). You must specify the model for the variables using `formula`.

Data Types: `table`

Formula for model specification, specified as a character vector or string scalar of the form ```'y ~ fixed + (random1|grouping1) + ... + (randomR|groupingR)'```. For full description, see Formula.

Example: `'y ~ treatment +(1|block)'`

Data Types: `char` | `string`

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

Pattern of the covariance matrix of the random effects, specified as the comma-separated pair consisting of `'CovariancePattern'` and a character vector, a string scalar, a square symmetric logical matrix, a string array, or a cell array of character vectors or logical matrices.

If there are R random-effects terms, then the value of `'CovariancePattern'` must be a string array or cell array of length R, where each element r of the array specifies the pattern of the covariance matrix of the random-effects vector associated with the rth random-effects term. The options for each element follow.

 `'FullCholesky'` Default. Full covariance matrix using the Cholesky parameterization. `fitlme` estimates all elements of the covariance matrix. `'Full'` Full covariance matrix, using the log-Cholesky parameterization. `fitlme` estimates all elements of the covariance matrix. `'Diagonal'` Diagonal covariance matrix. That is, off-diagonal elements of the covariance matrix are constrained to be 0. `$\left(\begin{array}{ccc}{\sigma }_{b1}^{2}& 0& 0\\ 0& {\sigma }_{b2}^{2}& 0\\ 0& 0& {\sigma }_{b3}^{2}\end{array}\right)$` `'Isotropic'` Diagonal covariance matrix with equal variances. That is, off-diagonal elements of the covariance matrix are constrained to be 0, and the diagonal elements are constrained to be equal. For example, if there are three random-effects terms with an isotropic covariance structure, this covariance matrix looks like `$\left(\begin{array}{ccc}{\sigma }_{b}^{2}& 0& 0\\ 0& {\sigma }_{b}^{2}& 0\\ 0& 0& {\sigma }_{b}^{2}\end{array}\right)$`where σ2b is the common variance of the random-effects terms. `'CompSymm'` Compound symmetry structure. That is, common variance along diagonals and equal correlation between all random effects. For example, if there are three random-effects terms with a covariance matrix having a compound symmetry structure, this covariance matrix looks like `$\left(\begin{array}{ccc}{\sigma }_{b1}^{2}& {\sigma }_{b1,b2}& {\sigma }_{b1,b2}\\ {\sigma }_{b1,b2}& {\sigma }_{b1}^{2}& {\sigma }_{b1,b2}\\ {\sigma }_{b1,b2}& {\sigma }_{b1,b2}& {\sigma }_{b1}^{2}\end{array}\right)$`where σ2b1 is the common variance of the random-effects terms and σb1,b2 is the common covariance between any two random-effects term . `PAT` Square symmetric logical matrix. If `'CovariancePattern'` is defined by the matrix `PAT`, and if ```PAT(a,b) = false```, then the `(a,b)` element of the corresponding covariance matrix is constrained to be 0.

Example: `'CovariancePattern','Diagonal'`

Example: `'CovariancePattern',{'Full','Diagonal'}`

Data Types: `char` | `string` | `logical` | `cell`

Method for estimating parameters of the linear mixed-effects model, specified as the comma-separated pair consisting of `'FitMethod'` and either of the following.

 `'ML'` Default. Maximum likelihood estimation `'REML'` Restricted maximum likelihood estimation

Example: `'FitMethod','REML'`

Observation weights, specified as the comma-separated pair consisting of `'Weights'` and a vector of length n, where n is the number of observations.

Data Types: `single` | `double`

Indices for rows to exclude from the linear mixed-effects model in the data, specified as the comma-separated pair consisting of `'Exclude'` and a vector of integer or logical values.

For example, you can exclude the 13th and 67th rows from the fit as follows.

Example: `'Exclude',[13,67]`

Data Types: `single` | `double` | `logical`

Coding to use for dummy variables created from the categorical variables, specified as the comma-separated pair consisting of `'DummyVarCoding'` and one of the following.

ValueDescription
`'reference'`Default. Coefficient for first category set to 0.
`'effects'`Coefficients sum to 0.
`'full'`One dummy variable for each category.

Example: `'DummyVarCoding','effects'`

Optimization algorithm, specified as the comma-separated pair consisting of `'Optimizer'` and either of the following.

 `'quasinewton'` Default. Uses a trust region based quasi-Newton optimizer. Change the options of the algorithm using `statset('LinearMixedModel')`. If you don’t specify the options, then `LinearMixedModel` uses the default options of `statset('LinearMixedModel')`. `'fminunc'` You must have Optimization Toolbox™ to specify this option. Change the options of the algorithm using `optimoptions('fminunc')`. If you don’t specify the options, then `LinearMixedModel` uses the default options of `optimoptions('fminunc')` with `'Algorithm'` set to `'quasi-newton'`.

Example: `'Optimizer','fminunc'`

Options for the optimization algorithm, specified as the comma-separated pair consisting of `'OptimizerOptions'` and a structure returned by `statset('LinearMixedModel')` or an object returned by `optimoptions('fminunc')`.

• If `'Optimizer'` is `'fminunc'`, then use `optimoptions('fminunc')` to change the options of the optimization algorithm. See `optimoptions` for the options `'fminunc'` uses. If `'Optimizer'` is `'fminunc'` and you do not supply `'OptimizerOptions'`, then the default for `LinearMixedModel` is the default options created by `optimoptions('fminunc')` with `'Algorithm'` set to `'quasi-newton'`.

• If `'Optimizer'` is `'quasinewton'`, then use `statset('LinearMixedModel')` to change the optimization parameters. If you don’t change the optimization parameters, then `LinearMixedModel` uses the default options created by `statset('LinearMixedModel')`:

The `'quasinewton'` optimizer uses the following fields in the structure created by `statset('LinearMixedModel')`.

Relative tolerance on the gradient of the objective function, specified as a positive scalar value.

Absolute tolerance on the step size, specified as a positive scalar value.

Maximum number of iterations allowed, specified as a positive scalar value.

Level of display, specified as one of `'off'`, `'iter'`, or `'final'`.

Method to start iterative optimization, specified as the comma-separated pair consisting of `'StartMethod'` and either of the following.

ValueDescription
`'default'`An internally defined default value
`'random'`A random initial value

Example: `'StartMethod','random'`

Indicator to display the optimization process on screen, specified as the comma-separated pair consisting of `'Verbose'` and either `false` or `true`. Default is `false`.

The setting for `'Verbose'` overrides the field `'Display'` in `'OptimizerOptions'`.

Example: `'Verbose',true`

Indicator to check the positive definiteness of the Hessian of the objective function with respect to unconstrained parameters at convergence, specified as the comma-separated pair consisting of `'CheckHessian'` and either `false` or `true`. Default is `false`.

Specify `'CheckHessian'` as `true` to verify optimality of the solution or to determine if the model is overparameterized in the number of covariance parameters.

Example: `'CheckHessian',true`

## Output Arguments

expand all

Linear mixed-effects model, returned as a `LinearMixedModel` object.

## Examples

expand all

`load flu`

The `flu` dataset array has a `Date` variable, and 10 variables containing estimated influenza rates (in 9 different regions, estimated from Google® searches, plus a nationwide estimate from the CDC).

To fit a linear-mixed effects model, your data must be in a properly formatted dataset array. To fit a linear mixed-effects model with the influenza rates as the responses, combine the nine columns corresponding to the regions into an array. The new dataset array, `flu2`, must have the response variable `FluRate`, the nominal variable `Region` that shows which region each estimate is from, the nationwide estimate `WtdILI`, and the grouping variable `Date`.

```flu2 = stack(flu,2:10,'NewDataVarName','FluRate',... 'IndVarName','Region'); flu2.Date = nominal(flu2.Date);```

Fit a linear mixed-effects model with the nationwide a random intercept that varies by `Date`. The model corresponds to

`${y}_{im}={\beta }_{0}+{\beta }_{1}{WtdILI}_{im}+{b}_{0m}+{\epsilon }_{im},\phantom{\rule{1em}{0ex}}i=1,2,...,468,\phantom{\rule{1em}{0ex}}m=1,2,...,52,$`

where ${y}_{im}$ is the observation $i$ for level $m$ of grouping variable `Date`, ${b}_{0m}$ is the random effect for level $m$ of the grouping variable `Date`, and ${\epsilon }_{im}$ is the observation error for observation $i$. The random effect has the prior distribution,

`${b}_{0m}\sim N\left(0,{\sigma }_{b}^{2}\right),$`

and the error term has the distribution,

`${\epsilon }_{im}\sim N\left(0,{\sigma }^{2}\right).$`

`lme = LinearMixedModel.fit(flu2,'FluRate ~ 1 + WtdILI + (1|Date)')`
```lme = Linear mixed-effects model fit by ML Model information: Number of observations 468 Fixed effects coefficients 2 Random effects coefficients 52 Covariance parameters 2 Formula: FluRate ~ 1 + WtdILI + (1 | Date) Model fit statistics: AIC BIC LogLikelihood Deviance 286.24 302.83 -139.12 278.24 Fixed effects coefficients (95% CIs): Name Estimate SE tStat DF pValue {'(Intercept)'} 0.16385 0.057525 2.8484 466 0.0045885 {'WtdILI' } 0.7236 0.032219 22.459 466 3.0502e-76 Lower Upper 0.050813 0.27689 0.66028 0.78691 Random effects covariance parameters (95% CIs): Group: Date (52 Levels) Name1 Name2 Type Estimate {'(Intercept)'} {'(Intercept)'} {'std'} 0.17146 Lower Upper 0.13227 0.22226 Group: Error Name Estimate Lower Upper {'Res Std'} 0.30201 0.28217 0.32324 ```

The confidence limits for the standard deviation of the random-effects term, ${\sigma }_{b}$, do not include 0 (0.13227, 0.22226), which indicates that the random-effects term is significant. You can also test the significance of the random-effects terms using the `compare` method.

The estimated value of an observation is the sum of the fixed effects and the random-effect value at the grouping variable level corresponding to that observation. For example, the estimated flu rate for observation 28 is

`$\begin{array}{rl}{\underset{}{\overset{ˆ}{y}}}_{28}& ={\underset{}{\overset{ˆ}{\beta }}}_{0}{\underset{}{\overset{ˆ}{\beta }}}_{1}{WtdILI}_{28}+{\underset{}{\overset{ˆ}{b}}}_{10/30/2005}\\ & =0.1639+0.7236*\left(1.343\right)+0.3318\\ & =1.46749,\end{array}$`

where $\underset{}{\overset{ˆ}{b}}$ is the BLUP of the random effects for the intercept. You can compute this value in the following way.

```beta = fixedEffects(lme); [~,~,STATS] = randomEffects(lme); % Compute the random-effects statistics (STATS) STATS.Level = nominal(STATS.Level); y_hat = beta(1) + beta(2)*flu2.WtdILI(28) + STATS.Estimate(STATS.Level=='10/30/2005')```
```y_hat = 1.4674 ```

You can display the fitted value using the `fitted` method.

```F = fitted(lme); F(28)```
```ans = 1.4674 ```

`load(fullfile(matlabroot,'examples','stats','shift.mat'));`

The dataset array shows the absolute deviations from the target quality characteristic measured from the products each of five operators manufacture over 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 absolute deviations of the quality characteristics from the target value. This is simulated data.

Fit a linear mixed-effects model with a random intercept grouped by operator, to assess if there is significant difference in the performance according to the time of the shift. Use the restricted maximum likelihood method and `'effects'` contrasts.

`'effects'` contrasts mean that the coefficients sum to 0, and `LinearMixedModel.fit` creates a matrix called a fixed effects design matrix to describe the effect of Shift. This matrix has two columns, $Shift_Evening$ and $Shift_Morning$, where

The model corresponds to

where $i$ represents the observations, and $m$ represents the operators, $i$ = 1, 2, ..., 15, and $m$ = 1, 2, ..., 5. The random effects and the observation error have the following distributions:

`${b}_{0m}\sim N\left(0,{\sigma }_{b}^{2}\right)$`

and

`${\epsilon }_{im}\sim N\left(0,{\sigma }^{2}\right).$`

```lme = LinearMixedModel.fit(shift,'QCDev ~ Shift + (1|Operator)',... 'FitMethod','REML','DummyVarCoding','effects')```
```lme = Linear mixed-effects model fit by REML Model information: Number of observations 15 Fixed effects coefficients 3 Random effects coefficients 5 Covariance parameters 2 Formula: QCDev ~ 1 + Shift + (1 | Operator) Model fit statistics: AIC BIC LogLikelihood Deviance 58.913 61.337 -24.456 48.913 Fixed effects coefficients (95% CIs): Name Estimate SE tStat DF pValue {'(Intercept)' } 3.6525 0.94109 3.8812 12 0.0021832 {'Shift_Evening'} -0.53293 0.31206 -1.7078 12 0.11339 {'Shift_Morning'} -0.91973 0.31206 -2.9473 12 0.012206 Lower Upper 1.6021 5.703 -1.2129 0.14699 -1.5997 -0.23981 Random effects covariance parameters (95% CIs): Group: Operator (5 Levels) Name1 Name2 Type Estimate {'(Intercept)'} {'(Intercept)'} {'std'} 2.0457 Lower Upper 0.98207 4.2612 Group: Error Name Estimate Lower Upper {'Res Std'} 0.85462 0.52357 1.395 ```

Compute the best linear unbiased predictor (BLUP) estimates of random effects.

`B = randomEffects(lme)`
```B = 5×1 0.5775 1.1757 -2.1715 2.3655 -1.9472 ```

The estimated absolute deviation from the target quality characteristics for the third operator working in the evening shift is

`$\begin{array}{rl}{\underset{}{\overset{ˆ}{y}}}_{\text{Evening},\text{Operator}3}& ={\underset{}{\overset{ˆ}{\beta }}}_{0}+{\underset{}{\overset{ˆ}{\beta }}}_{1}\text{Shift}\text{_}\text{Evening}+{\underset{}{\overset{ˆ}{b}}}_{03}\\ & =3.6525-0.53293-2.1715\\ & =0.94807.\end{array}$`

You can also display this value as follows.

```F = fitted(lme); F(shift.Shift=='Evening' & shift.Operator=='3')```
```ans = 0.9481 ```

Similarly, you can calculate the estimated absolute deviation from the target quality characteristics for the third operator working in the morning shift is

`$\begin{array}{rl}{\underset{}{\overset{ˆ}{y}}}_{\text{Morning},\text{Operator}3}& ={\underset{}{\overset{ˆ}{\beta }}}_{0}+{\underset{}{\overset{ˆ}{\beta }}}_{2}\text{Shift}\text{_}\text{Morning}+{\underset{}{\overset{ˆ}{b}}}_{03}\\ & =3.6525-0.91973-2.1715\\ & =0.56127.\end{array}$`

You can also display this value in the following way.

`F(shift.Shift=='Morning' & shift.Operator=='3')`
```ans = 0.5613 ```

The operator tends to make a smaller magnitude of error in the morning shift.

`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 types of tomato plants, (cherry, heirloom, grape, vine, and plum) are randomly assigned to these plots. Then, the tomato plants in the plots are divided into subplots, where each subplot is treated by one of the 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.

This model corresponds to

`$\begin{array}{rl}{y}_{imjk}& ={\beta }_{0}+\sum _{m=2}^{4}{\beta }_{1m}I\left[F{\right]}_{im}+\sum _{j=2}^{5}{\beta }_{2j}I\left[T{\right]}_{ij}+\sum _{j=2}^{5}\sum _{m=2}^{4}{\beta }_{3mj}I\left[F{\right]}_{im}I\left[T{\right]}_{ij}\\ & +{b}_{0k}{S}_{k}+{b}_{0jk}\left(S*T{\right)}_{jk}+{ϵ}_{imjk},\end{array}$`

where $i=1,2,...,60$, the index $m$ corresponds to the fertilizer types, $j$ corresponds to the tomato types, and $k=1,2,3$ corresponds to the blocks (soil). ${S}_{k}$ represents the kth soil type, and $\left(S*T{\right)}_{jk}$ represents the jth tomato type nested in the kth soil type. $I\left[F{\right]}_{im}$ is the dummy variable representing level $m$ of the fertilizer. Similarly, $I\left[T{\right]}_{ij}$ is the dummy variable representing the level $j$ of the tomato type.

The random effects and observation error have the following prior distributions:

`${b}_{0k}\sim N\left(0,{\sigma }_{S}^{2}\right)$`

`${b}_{0jk}\sim N\left(0,{\sigma }_{S*T}^{2}\right)$`

`${ϵ}_{imjk}\sim N\left(0,{\sigma }^{2}\right)$`

`lme = LinearMixedModel.fit(ds,'Yield ~ Fertilizer * Tomato + (1|Soil) + (1|Soil:Tomato)')`
```lme = Linear mixed-effects model fit by ML Model information: Number of observations 60 Fixed effects coefficients 20 Random effects coefficients 18 Covariance parameters 3 Formula: Yield ~ 1 + Tomato*Fertilizer + (1 | Soil) + (1 | Soil:Tomato) Model fit statistics: AIC BIC LogLikelihood Deviance 522.57 570.74 -238.29 476.57 Fixed effects coefficients (95% CIs): Name Estimate SE tStat DF {'(Intercept)' } 77 8.5836 8.9706 40 {'Tomato_Grape' } -16 11.966 -1.3371 40 {'Tomato_Heirloom' } -6.6667 11.966 -0.55714 40 {'Tomato_Plum' } 32.333 11.966 2.7022 40 {'Tomato_Vine' } -13 11.966 -1.0864 40 {'Fertilizer_2' } 34.667 8.572 4.0442 40 {'Fertilizer_3' } 33.667 8.572 3.9275 40 {'Fertilizer_4' } 47.667 8.572 5.5607 40 {'Tomato_Grape:Fertilizer_2' } -2.6667 12.123 -0.21997 40 {'Tomato_Heirloom:Fertilizer_2'} -8 12.123 -0.65992 40 {'Tomato_Plum:Fertilizer_2' } -15 12.123 -1.2374 40 {'Tomato_Vine:Fertilizer_2' } -16 12.123 -1.3198 40 {'Tomato_Grape:Fertilizer_3' } 16.667 12.123 1.3748 40 {'Tomato_Heirloom:Fertilizer_3'} 3.3333 12.123 0.27497 40 {'Tomato_Plum:Fertilizer_3' } 3.6667 12.123 0.30246 40 {'Tomato_Vine:Fertilizer_3' } 3 12.123 0.24747 40 {'Tomato_Grape:Fertilizer_4' } 13.333 12.123 1.0999 40 {'Tomato_Heirloom:Fertilizer_4'} -19 12.123 -1.5673 40 {'Tomato_Plum:Fertilizer_4' } -2.6667 12.123 -0.21997 40 {'Tomato_Vine:Fertilizer_4' } 8.6667 12.123 0.71492 40 pValue Lower Upper 4.0206e-11 59.652 94.348 0.18873 -40.184 8.1837 0.58053 -30.85 17.517 0.010059 8.1496 56.517 0.28379 -37.184 11.184 0.00023272 17.342 51.991 0.00033057 16.342 50.991 1.9567e-06 30.342 64.991 0.82701 -27.167 21.834 0.51309 -32.501 16.501 0.22317 -39.501 9.5007 0.19439 -40.501 8.5007 0.17683 -7.8341 41.167 0.78476 -21.167 27.834 0.76387 -20.834 28.167 0.80581 -21.501 27.501 0.27796 -11.167 37.834 0.12492 -43.501 5.5007 0.82701 -27.167 21.834 0.47881 -15.834 33.167 Random effects covariance parameters (95% CIs): Group: Soil (3 Levels) Name1 Name2 Type Estimate {'(Intercept)'} {'(Intercept)'} {'std'} 2.5028 Lower Upper 0.027711 226.05 Group: Soil:Tomato (15 Levels) Name1 Name2 Type Estimate {'(Intercept)'} {'(Intercept)'} {'std'} 10.225 Lower Upper 6.1497 17.001 Group: Error Name Estimate Lower Upper {'Res Std'} 10.499 8.5389 12.908 ```

The $p$-values corresponding to the last 12 rows in the fixed-effects coefficients display (0.82701 to 0.47881) indicate that interaction coefficients between the tomato and fertilizer types are not significant. To test for the overall interaction between tomato and fertilizer, use the `anova` method after refitting the model using `'effects'` contrasts.

The confidence interval for the standard deviations of the random-effects terms (${\sigma }_{S}^{2}$), where the intercept is grouped by soil is very large. This term does not appear significant.

Refit the model after removing the interaction term `Tomato:Fertilizer` and the random-effects term `(1 | Soil)`.

`lme = LinearMixedModel.fit(ds,'Yield ~ Fertilizer + Tomato + (1|Soil:Tomato)')`
```lme = Linear mixed-effects model fit by ML Model information: Number of observations 60 Fixed effects coefficients 8 Random effects coefficients 15 Covariance parameters 2 Formula: Yield ~ 1 + Tomato + Fertilizer + (1 | Soil:Tomato) Model fit statistics: AIC BIC LogLikelihood Deviance 511.06 532 -245.53 491.06 Fixed effects coefficients (95% CIs): Name Estimate SE tStat DF {'(Intercept)' } 77.733 7.3293 10.606 52 {'Tomato_Grape' } -9.1667 9.6045 -0.95441 52 {'Tomato_Heirloom'} -12.583 9.6045 -1.3102 52 {'Tomato_Plum' } 28.833 9.6045 3.0021 52 {'Tomato_Vine' } -14.083 9.6045 -1.4663 52 {'Fertilizer_2' } 26.333 4.5004 5.8514 52 {'Fertilizer_3' } 39 4.5004 8.6659 52 {'Fertilizer_4' } 47.733 4.5004 10.607 52 pValue Lower Upper 1.3108e-14 63.026 92.441 0.34429 -28.439 10.106 0.1959 -31.856 6.6895 0.0041138 9.5605 48.106 0.14858 -33.356 5.1895 3.3024e-07 17.303 35.364 1.1459e-11 29.969 48.031 1.308e-14 38.703 56.764 Random effects covariance parameters (95% CIs): Group: Soil:Tomato (15 Levels) Name1 Name2 Type Estimate {'(Intercept)'} {'(Intercept)'} {'std'} 10.02 Lower Upper 6.0812 16.509 Group: Error Name Estimate Lower Upper {'Res Std'} 12.325 10.024 15.153 ```

You can compare the two models using the `compare` method with the simulated likelihood ratio test since both a fixed-effect and a random-effect term will be tested.

`load(fullfile(matlabroot,'examples','stats','weight.mat'));`

`weight` contains data from a longitudinal study, where 20 subjects are randomly assigned to 4 exercise programs (A, B, C, D), and their weight loss is recorded over six two-week time periods. This is simulated data.

Store the data in a table. Define `Subject` and `Program` as categorical variables.

```tbl = table(InitialWeight,Program,Subject,Week,y); tbl.Subject = nominal(tbl.Subject); tbl.Program = nominal(tbl.Program);```

Fit a linear mixed-effects model where the initial weight, type of program, week, and the interaction between the week and type of program are the fixed effects. The intercept and week vary by subject.

`LinearMixedModel.fit` uses Program A as a reference and creates the necessary dummy variables $I$[.]. Since the model already has an intercept, `LinearMixedModel.fit` only creates dummy variables for program types B, C, D. This is also known as the `'reference'` method of coding dummy variables.

This model corresponds to

`$\begin{array}{l}{y}_{im}={\beta }_{0}+{\beta }_{1}I{W}_{i}+{\beta }_{2}Wee{k}_{i}+{\beta }_{3}I{\left[PB\right]}_{i}+{\beta }_{4}I{\left[PC\right]}_{i}+{\beta }_{5}I{\left[PD\right]}_{i}\\ \phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}+{\beta }_{6}\left(Wee{k}_{i}*I{\left[PB\right]}_{i}\right)+{\beta }_{7}\left(Wee{k}_{i}*I{\left[PC\right]}_{i}\right)+{\beta }_{8}\left(Wee{k}_{i}*I{\left[PD\right]}_{i}\right)\\ \phantom{\rule{1em}{0ex}}\phantom{\rule{1em}{0ex}}+b{}_{0m}+\phantom{\rule{0.16666666666666666em}{0ex}}{b}_{1m}Wee{k}_{im}+{\epsilon }_{im},\end{array}$`

where $i$ = 1, 2, ..., 120, and $m$ = 1, 2, ..., 20. ${\beta }_{j}$ are the fixed-effects coefficients, $j$ = 0, 1, ..., 8, and ${b}_{0m}$ and ${b}_{1m}$ are random effects. $IW$ stands for initial weight and $I\left[\cdot \right]$ is a dummy variable representing a type of program. For example, $I\left[PB{\right]}_{i}$ is the dummy variable representing program type B. The random effects and observation error have the following prior distributions:

`${b}_{0m}\sim N\left(0,{\sigma }_{0}^{2}\right)$`

`${b}_{1m}\sim N\left(0,{\sigma }_{1}^{2}\right)$`

`${\epsilon }_{im}\sim N\left(0,{\sigma }^{2}\right).$`

`lme = LinearMixedModel.fit(tbl,'y ~ InitialWeight + Program*Week + (Week|Subject)')`
```lme = Linear mixed-effects model fit by ML Model information: Number of observations 120 Fixed effects coefficients 9 Random effects coefficients 40 Covariance parameters 4 Formula: y ~ 1 + InitialWeight + Program*Week + (1 + Week | Subject) Model fit statistics: AIC BIC LogLikelihood Deviance -22.981 13.257 24.49 -48.981 Fixed effects coefficients (95% CIs): Name Estimate SE tStat DF {'(Intercept)' } 0.66105 0.25892 2.5531 111 {'InitialWeight' } 0.0031879 0.0013814 2.3078 111 {'Program_B' } 0.36079 0.13139 2.746 111 {'Program_C' } -0.033263 0.13117 -0.25358 111 {'Program_D' } 0.11317 0.13132 0.86175 111 {'Week' } 0.1732 0.067454 2.5677 111 {'Program_B:Week'} 0.038771 0.095394 0.40644 111 {'Program_C:Week'} 0.030543 0.095394 0.32018 111 {'Program_D:Week'} 0.033114 0.095394 0.34713 111 pValue Lower Upper 0.012034 0.14798 1.1741 0.022863 0.00045067 0.0059252 0.0070394 0.10044 0.62113 0.80029 -0.29319 0.22666 0.39068 -0.14706 0.3734 0.011567 0.039536 0.30686 0.68521 -0.15026 0.2278 0.74944 -0.15849 0.21957 0.72915 -0.15592 0.22214 Random effects covariance parameters (95% CIs): Group: Subject (20 Levels) Name1 Name2 Type Estimate {'(Intercept)'} {'(Intercept)'} {'std' } 0.18407 {'Week' } {'(Intercept)'} {'corr'} 0.66841 {'Week' } {'Week' } {'std' } 0.15033 Lower Upper 0.12281 0.27587 0.21076 0.88573 0.11004 0.20537 Group: Error Name Estimate Lower Upper {'Res Std'} 0.10261 0.087882 0.11981 ```

The $p$-values 0.022863 and 0.011567 indicate significant effects of subject initial weights and time in the amount of weight lost. The weight loss of subjects who are in Program B is significantly different relative to the weight loss of subjects who are in Program A. The lower and upper limits of the covariance parameters for the random effects do not include 0, thus they are significant. You can also test the significance of the random effects using the `compare` method.

expand all

## Tips

• If your model is not easily described using a formula, you can create matrices to define the fixed and random effects, and fit the model using `fitlmematrix`.

## Alternatives

You can also construct a linear mixed-effects model using `fitlme`. If your data is in matrix format, then use `fitlmematrix`.