Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_correg_mixeff_ml (g02jb)

## Purpose

nag_correg_mixeff_ml (g02jb) fits a linear mixed effects regression model using maximum likelihood (ML).

## Syntax

[nff, nrf, df, ml, b, se, gamma, warn, ifail] = g02jb(nvpr, levels, yvid, fvid, rvid, svid, cwid, vpr, dat, fint, rint, lb, gamma, 'n', n, 'ncol', ncol, 'nfv', nfv, 'nrv', nrv, 'maxit', maxit, 'tol', tol)
[nff, nrf, df, ml, b, se, gamma, warn, ifail] = nag_correg_mixeff_ml(nvpr, levels, yvid, fvid, rvid, svid, cwid, vpr, dat, fint, rint, lb, gamma, 'n', n, 'ncol', ncol, 'nfv', nfv, 'nrv', nrv, 'maxit', maxit, 'tol', tol)
Note: the interface to this routine has changed since earlier releases of the toolbox:
 At Mark 23: maxit and tol were made optional At Mark 22: n was made optional

## Description

nag_correg_mixeff_ml (g02jb) fits a model of the form:
 $y=Xβ+Zν+ε$
where
• $y$ is a vector of $n$ observations on the dependent variable,
• $X$ is a known $n$ by $p$ design matrix for the fixed independent variables,
• $\beta$ is a vector of length $p$ of unknown fixed effects,
• $Z$ is a known $n$ by $q$ design matrix for the random independent variables,
• $\nu$ is a vector of length $q$ of unknown random effects;
and
• $\epsilon$ is a vector of length $n$ of unknown random errors.
Both $\nu$ and $\epsilon$ are assumed to have a Gaussian distribution with expectation zero and
 $Var ν ε = G 0 0 R$
where $R={\sigma }_{R}^{2}I$, $I$ is the $n×n$ identity matrix and $G$ is a diagonal matrix. It is assumed that the random variables, $Z$, can be subdivided into $g\le q$ groups with each group being identically distributed with expectations zero and variance ${\sigma }_{i}^{2}$. The diagonal elements of matrix $G$ therefore take one of the values $\left\{{\sigma }_{i}^{2}:i=1,2,\dots ,g\right\}$, depending on which group the associated random variable belongs to.
The model therefore contains three sets of unknowns, the fixed effects, $\beta$, the random effects $\nu$ and a vector of $g+1$ variance components, $\gamma$, where $\gamma =\left\{{\sigma }_{1}^{2},{\sigma }_{2}^{2},\dots ,{\sigma }_{g-1}^{2},{\sigma }_{g}^{2},{\sigma }_{R}^{2}\right\}$. Rather than working directly with $\gamma$, nag_correg_mixeff_ml (g02jb) uses an iterative process to estimate ${\gamma }^{*}=\left\{{\sigma }_{1}^{2}/{\sigma }_{R}^{2},{\sigma }_{2}^{2}/{\sigma }_{R}^{2},\dots ,{\sigma }_{g-1}^{2}/{\sigma }_{R}^{2},{\sigma }_{g}^{2}/{\sigma }_{R}^{2},1\right\}$. Due to the iterative nature of the estimation a set of initial values, ${\gamma }_{0}$, for ${\gamma }^{*}$ is required. nag_correg_mixeff_ml (g02jb) allows these initial values either to be supplied by you or calculated from the data using the minimum variance quadratic unbiased estimators (MIVQUE0) suggested by Rao (1972).
nag_correg_mixeff_ml (g02jb) fits the model using a quasi-Newton algorithm to maximize the log-likelihood function:
 $-2 l R = log V + n log r ′ V-1 r + log 2 π / n$
where
 $V = ZG Z′ + R, r=y-Xb and b = X ′ V-1 X -1 X ′ V-1 y .$
Once the final estimates for ${\gamma }^{*}$ have been obtained, the value of ${\sigma }_{R}^{2}$ is given by:
 $σR2 = r′ V-1 r / n - p .$
Case weights, ${W}_{c}$, can be incorporated into the model by replacing ${X}^{\prime }X$ and ${Z}^{\prime }Z$ with ${X}^{\prime }{W}_{c}X$ and ${Z}^{\prime }{W}_{c}Z$ respectively, for a diagonal weight matrix ${W}_{c}$.
The log-likelihood, ${l}_{R}$, is calculated using the sweep algorithm detailed in Wolfinger et al. (1994).

## References

Goodnight J H (1979) A tutorial on the SWEEP operator The American Statistician 33(3) 149–158
Harville D A (1977) Maximum likelihood approaches to variance component estimation and to related problems JASA 72 320–340
Rao C R (1972) Estimation of variance and covariance components in a linear model J. Am. Stat. Assoc. 67 112–115
Stroup W W (1989) Predictable functions and prediction space in the mixed model procedure Applications of Mixed Models in Agriculture and Related Disciplines Southern Cooperative Series Bulletin No. 343 39–48
Wolfinger R, Tobias R and Sall J (1994) Computing Gaussian likelihoods and their derivatives for general linear mixed models SIAM Sci. Statist. Comput. 15 1294–1310

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{nvpr}$int64int32nag_int scalar
If ${\mathbf{rint}}=1$ and ${\mathbf{svid}}\ne 0$, nvpr is the number of variance components being $\text{estimated}-2$, ($g-1$), else ${\mathbf{nvpr}}=g$.
If ${\mathbf{nrv}}=0$, ${\mathbf{nvpr}}$ is not referenced.
Constraint: if ${\mathbf{nrv}}\ne 0$, $1\le {\mathbf{nvpr}}\le {\mathbf{nrv}}$.
2:     $\mathrm{levels}\left({\mathbf{ncol}}\right)$int64int32nag_int array
${\mathbf{levels}}\left(i\right)$ contains the number of levels associated with the $i$th variable of the data matrix dat. If this variable is continuous or binary (i.e., only takes the values zero or one) then ${\mathbf{levels}}\left(i\right)$ should be $1$; if the variable is discrete then ${\mathbf{levels}}\left(i\right)$ is the number of levels associated with it and ${\mathbf{dat}}\left(\mathit{j},i\right)$ is assumed to take the values $1$ to ${\mathbf{levels}}\left(i\right)$, for $\mathit{j}=1,2,\dots ,{\mathbf{n}}$.
Constraint: ${\mathbf{levels}}\left(\mathit{i}\right)\ge 1$, for $\mathit{i}=1,2,\dots ,{\mathbf{ncol}}$.
3:     $\mathrm{yvid}$int64int32nag_int scalar
The column of dat holding the dependent, $y$, variable.
Constraint: $1\le {\mathbf{yvid}}\le {\mathbf{ncol}}$.
4:     $\mathrm{fvid}\left({\mathbf{nfv}}\right)$int64int32nag_int array
The columns of the data matrix dat holding the fixed independent variables with ${\mathbf{fvid}}\left(i\right)$ holding the column number corresponding to the $i$th fixed variable.
Constraint: $1\le {\mathbf{fvid}}\left(\mathit{i}\right)\le {\mathbf{ncol}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{nfv}}$.
5:     $\mathrm{rvid}\left({\mathbf{nrv}}\right)$int64int32nag_int array
The columns of the data matrix ${\mathbf{dat}}$ holding the random independent variables with ${\mathbf{rvid}}\left(i\right)$ holding the column number corresponding to the $i$th random variable.
Constraint: $1\le {\mathbf{rvid}}\left(\mathit{i}\right)\le {\mathbf{ncol}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{nrv}}$.
6:     $\mathrm{svid}$int64int32nag_int scalar
The column of dat holding the subject variable.
If ${\mathbf{svid}}=0$, no subject variable is used.
Specifying a subject variable is equivalent to specifying the interaction between that variable and all of the random-effects. Letting the notation ${Z}_{1}×{Z}_{S}$ denote the interaction between variables ${Z}_{1}$ and ${Z}_{S}$, fitting a model with ${\mathbf{rint}}=0$, random-effects ${Z}_{1}+{Z}_{2}$ and subject variable ${Z}_{S}$ is equivalent to fitting a model with random-effects ${Z}_{1}×{Z}_{S}+{Z}_{2}×{Z}_{S}$ and no subject variable. If ${\mathbf{rint}}=1$ the model is equivalent to fitting ${Z}_{S}+{Z}_{1}×{Z}_{S}+{Z}_{2}×{Z}_{S}$ and no subject variable.
Constraint: $0\le {\mathbf{svid}}\le {\mathbf{ncol}}$.
7:     $\mathrm{cwid}$int64int32nag_int scalar
The column of dat holding the case weights.
If ${\mathbf{cwid}}=0$, no weights are used.
Constraint: $0\le {\mathbf{cwid}}\le {\mathbf{ncol}}$.
8:     $\mathrm{vpr}\left({\mathbf{nrv}}\right)$int64int32nag_int array
${\mathbf{vpr}}\left(i\right)$ holds a flag indicating the variance of the $i$th random variable. The variance of the $i$th random variable is ${\sigma }_{j}^{2}$, where $j={\mathbf{vpr}}\left(i\right)+1$ if ${\mathbf{rint}}=1$ and ${\mathbf{svid}}\ne 0$ and $j={\mathbf{vpr}}\left(i\right)$ otherwise. Random variables with the same value of $j$ are assumed to be taken from the same distribution.
Constraint: $1\le {\mathbf{vpr}}\left(\mathit{i}\right)\le {\mathbf{nvpr}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{nrv}}$.
9:     $\mathrm{dat}\left(\mathit{lddat},{\mathbf{ncol}}\right)$ – double array
lddat, the first dimension of the array, must satisfy the constraint $\mathit{lddat}\ge {\mathbf{n}}$.
Array containing all of the data. For the $i$th observation:
• ${\mathbf{dat}}\left(i,{\mathbf{yvid}}\right)$ holds the dependent variable, $y$;
• if ${\mathbf{cwid}}\ne 0$, ${\mathbf{dat}}\left(i,{\mathbf{cwid}}\right)$ holds the case weights;
• if ${\mathbf{svid}}\ne 0$, ${\mathbf{dat}}\left(i,{\mathbf{svid}}\right)$ holds the subject variable.
The remaining columns hold the values of the independent variables.
Constraints:
• if ${\mathbf{cwid}}\ne 0$, ${\mathbf{dat}}\left(i,{\mathbf{cwid}}\right)\ge 0.0$;
• if ${\mathbf{levels}}\left(j\right)\ne 1$, $1\le {\mathbf{dat}}\left(i,j\right)\le {\mathbf{levels}}\left(j\right)$.
10:   $\mathrm{fint}$int64int32nag_int scalar
Flag indicating whether a fixed intercept is included (${\mathbf{fint}}=1$).
Constraint: ${\mathbf{fint}}=0$ or $1$.
11:   $\mathrm{rint}$int64int32nag_int scalar
Flag indicating whether a random intercept is included (${\mathbf{rint}}=1$).
If ${\mathbf{svid}}=0$, rint is not referenced.
Constraint: ${\mathbf{rint}}=0$ or $1$.
12:   $\mathrm{lb}$int64int32nag_int scalar
The size of the array b.
Constraint: ${\mathbf{lb}}\ge {\mathbf{fint}}+\sum _{i=1}^{{\mathbf{nfv}}}\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{levels}}\left({\mathbf{fvid}}\left(i\right)\right)-1,1\right)+{L}_{S}×\left({\mathbf{rint}}+\sum _{i=1}^{{\mathbf{nrv}}}{\mathbf{levels}}\left({\mathbf{rvid}}\left(i\right)\right)\right)$ where ${L}_{S}={\mathbf{levels}}\left({\mathbf{svid}}\right)$ if ${\mathbf{svid}}\ne 0$ and $1$ otherwise.
13:   $\mathrm{gamma}\left({\mathbf{nvpr}}+2\right)$ – double array
Holds the initial values of the variance components, ${\gamma }_{0}$, with ${\mathbf{gamma}}\left(\mathit{i}\right)$ the initial value for ${\sigma }_{\mathit{i}}^{2}/{\sigma }_{R}^{2}$, for $\mathit{i}=1,2,\dots ,g$. If ${\mathbf{rint}}=1$ and ${\mathbf{svid}}\ne 0$, $g={\mathbf{nvpr}}+1$, else $g={\mathbf{nvpr}}$.
If ${\mathbf{gamma}}\left(1\right)=-1.0$, the remaining elements of gamma are ignored and the initial values for the variance components are estimated from the data using MIVQUE0.
Constraint: ${\mathbf{gamma}}\left(1\right)=-1.0\text{​ or ​}{\mathbf{gamma}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,g$.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the first dimension of the array dat.
$n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathrm{ncol}$int64int32nag_int scalar
Default: the dimension of the array levels and the second dimension of the array dat. (An error is raised if these dimensions are not equal.)
The number of columns in the data matrix, dat.
Constraint: ${\mathbf{ncol}}\ge 1$.
3:     $\mathrm{nfv}$int64int32nag_int scalar
Default: the dimension of the array fvid.
The number of independent variables in the model which are to be treated as being fixed.
Constraint: $0\le {\mathbf{nfv}}<{\mathbf{ncol}}$.
4:     $\mathrm{nrv}$int64int32nag_int scalar
Default: the dimension of the arrays rvid, vpr. (An error is raised if these dimensions are not equal.)
The number of independent variables in the model which are to be treated as being random.
Constraints:
• $0\le {\mathbf{nrv}}<{\mathbf{ncol}}$;
• ${\mathbf{nrv}}+{\mathbf{rint}}>0$.
5:     $\mathrm{maxit}$int64int32nag_int scalar
Default: $-1$
The maximum number of iterations.
If ${\mathbf{maxit}}<0$, the default value of $100$ is used.
If ${\mathbf{maxit}}=0$, the parameter estimates $\left(\beta ,\nu \right)$ and corresponding standard errors are calculated based on the value of ${\gamma }_{0}$ supplied in gamma.
6:     $\mathrm{tol}$ – double scalar
Default: $0$
The tolerance used to assess convergence.
If ${\mathbf{tol}}\le 0.0$, the default value of ${\epsilon }^{0.7}$ is used, where $\epsilon$ is the machine precision.

### Output Parameters

1:     $\mathrm{nff}$int64int32nag_int scalar
The number of fixed effects estimated (i.e., the number of columns, $p$, in the design matrix $X$).
2:     $\mathrm{nrf}$int64int32nag_int scalar
The number of random effects estimated (i.e., the number of columns, $q$, in the design matrix $Z$).
3:     $\mathrm{df}$int64int32nag_int scalar
The degrees of freedom.
4:     $\mathrm{ml}$ – double scalar
$-2{l}_{R}\left(\stackrel{^}{\gamma }\right)$ where ${l}_{R}$ is the log of the maximum likelihood calculated at $\stackrel{^}{\gamma }$, the estimated variance components returned in gamma.
5:     $\mathrm{b}\left({\mathbf{lb}}\right)$ – double array
The parameter estimates, $\left(\beta ,\nu \right)$, with the first nff elements of b containing the fixed effect parameter estimates, $\beta$ and the next nrf elements of b containing the random effect parameter estimates, $\nu$.
Fixed effects
If ${\mathbf{fint}}=1$, ${\mathbf{b}}\left(1\right)$ contains the estimate of the fixed intercept. Let ${L}_{i}$ denote the number of levels associated with the $i$th fixed variable, that is ${L}_{i}={\mathbf{levels}}\left({\mathbf{fvid}}\left(i\right)\right)$. Define
• if ${\mathbf{fint}}=1$, ${F}_{1}=2$ else if ${\mathbf{fint}}=0$, ${F}_{1}=1$;
• ${F}_{i+1}={F}_{i}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({L}_{i}-1,1\right)$, $i\ge 1$.
Then for $i=1,2,\dots ,{\mathbf{nfv}}$:
• if ${L}_{i}>1$, ${\mathbf{b}}\left({F}_{i}+\mathit{j}-2\right)$ contains the parameter estimate for the $\mathit{j}$th level of the $i$th fixed variable, for $\mathit{j}=2,3,\dots ,{L}_{i}$;
• if ${L}_{i}\le 1$, ${\mathbf{b}}\left({F}_{i}\right)$ contains the parameter estimate for the $i$th fixed variable.
Random effects
Redefining ${L}_{i}$ to denote the number of levels associated with the $i$th random variable, that is ${L}_{i}={\mathbf{levels}}\left({\mathbf{rvid}}\left(i\right)\right)$. Define
• if ${\mathbf{rint}}=1$, ${R}_{1}=2$ else if ${\mathbf{rint}}=0$, ${R}_{1}=1$;
${R}_{i+1}={R}_{i}+{L}_{i}$, $i\ge 1$.
Then for $i=1,2,\dots ,{\mathbf{nrv}}$:
• if ${\mathbf{svid}}=0$,
• if ${L}_{i}>1$, ${\mathbf{b}}\left({\mathbf{nff}}+{R}_{i}+\mathit{j}-1\right)$ contains the parameter estimate for the $\mathit{j}$th level of the $i$th random variable, for $\mathit{j}=1,2,\dots ,{L}_{i}$;
• if ${L}_{i}\le 1$, ${\mathbf{b}}\left({\mathbf{nff}}+{R}_{i}\right)$ contains the parameter estimate for the $i$th random variable;
• if ${\mathbf{svid}}\ne 0$,
• let ${L}_{S}$ denote the number of levels associated with the subject variable, that is ${L}_{S}={\mathbf{levels}}\left({\mathbf{svid}}\right)$;
• if ${L}_{i}>1$, ${\mathbf{b}}\left({\mathbf{nff}}+\left(\mathit{s}-1\right){L}_{S}+{R}_{i}+\mathit{j}-1\right)$ contains the parameter estimate for the interaction between the $\mathit{s}$th level of the subject variable and the $\mathit{j}$th level of the $i$th random variable, for $\mathit{s}=1,2,\dots ,{L}_{S}$ and $\mathit{j}=1,2,\dots ,{L}_{i}$;
• if ${L}_{i}\le 1$, ${\mathbf{b}}\left({\mathbf{nff}}+\left(\mathit{s}-1\right){L}_{S}+{R}_{i}\right)$ contains the parameter estimate for the interaction between the $\mathit{s}$th level of the subject variable and the $i$th random variable, for $\mathit{s}=1,2,\dots ,{L}_{S}$;
• if ${\mathbf{rint}}=1$, ${\mathbf{b}}\left({\mathbf{nff}}+1\right)$ contains the estimate of the random intercept.
6:     $\mathrm{se}\left({\mathbf{lb}}\right)$ – double array
The standard errors of the parameter estimates given in b.
7:     $\mathrm{gamma}\left({\mathbf{nvpr}}+2\right)$ – double array
${\mathbf{gamma}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,g$, holds the final estimate of ${\sigma }_{\mathit{i}}^{2}$ and ${\mathbf{gamma}}\left(g+1\right)$ holds the final estimate for ${\sigma }_{R}^{2}$.
8:     $\mathrm{warn}$int64int32nag_int scalar
Is set to $1$ if a variance component was estimated to be a negative value during the fitting process. Otherwise warn is set to $0$.
If ${\mathbf{warn}}=1$, the negative estimate is set to zero and the estimation process allowed to continue.
9:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$, or ${\mathbf{ncol}}<1$, or $\mathit{lddat}<{\mathbf{n}}$, or ${\mathbf{yvid}}<1$ or ${\mathbf{yvid}}>{\mathbf{ncol}}$, or ${\mathbf{cwid}}<0$ or ${\mathbf{cwid}}>{\mathbf{ncol}}$, or ${\mathbf{nfv}}<0$ or ${\mathbf{nfv}}\ge {\mathbf{ncol}}$, or ${\mathbf{fint}}\ne 0$ and ${\mathbf{fint}}\ne 1$, or ${\mathbf{nrv}}<0$ or ${\mathbf{nrv}}>{\mathbf{ncol}}$ or ${\mathbf{nrv}}+{\mathbf{rint}}<1$, or ${\mathbf{nvpr}}<0$ or ${\mathbf{nvpr}}>{\mathbf{nrv}}$, or ${\mathbf{rint}}\ne 0$ and ${\mathbf{rint}}\ne 1$, or ${\mathbf{svid}}<0$ or ${\mathbf{svid}}>{\mathbf{ncol}}$, or lb is too small.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{levels}}\left(i\right)<1$, for at least one $i$, or ${\mathbf{fvid}}\left(i\right)<1$, or ${\mathbf{fvid}}\left(i\right)>{\mathbf{ncol}}$, for at least one $i$, or ${\mathbf{rvid}}\left(i\right)<1$, or ${\mathbf{rvid}}\left(i\right)>{\mathbf{ncol}}$, for at least one $i$, or ${\mathbf{vpr}}\left(i\right)<1$ or ${\mathbf{vpr}}\left(i\right)>{\mathbf{nvpr}}$, for at least one $i$, or at least one discrete variable in array dat has a value greater than that specified in levels, or ${\mathbf{gamma}}\left(i\right)<0$, for at least one $i$, and ${\mathbf{gamma}}\left(1\right)\ne -1$.
${\mathbf{ifail}}=3$
Degrees of freedom $<1$. The number of arguments exceed the effective number of observations.
${\mathbf{ifail}}=4$
The function failed to converge to the specified tolerance in maxit iterations. See Further Comments for advice.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The accuracy of the results can be adjusted through the use of the tol argument.

Wherever possible any block structure present in the design matrix $Z$ should be modelled through a subject variable, specified via svid, rather than being explicitly entered into dat.
nag_correg_mixeff_ml (g02jb) uses an iterative process to fit the specified model and for some problems this process may fail to converge (see ${\mathbf{ifail}}={\mathbf{4}}$). If the function fails to converge then the maximum number of iterations (see maxit) or tolerance (see tol) may require increasing; try a different starting estimate in gamma. Alternatively, the model can be fit using restricted maximum likelihood (see nag_correg_mixeff_reml (g02ja)) or using the noniterative MIVQUE0.
To fit the model just using MIVQUE0, the first element of gamma should be set to $-1$ and maxit should be set to zero.
Although the quasi-Newton algorithm used in nag_correg_mixeff_ml (g02jb) tends to require more iterations before converging compared to the Newton–Raphson algorithm recommended by Wolfinger et al. (1994), it does not require the second derivatives of the likelihood function to be calculated and consequentially takes significantly less time per iteration.

## Example

The following dataset is taken from Stroup (1989) and arises from a balanced split-plot design with the whole plots arranged in a randomized complete block-design.
In this example the full design matrix for the random independent variable, $Z$, is given by:
 $Z = 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1$
 $= A 0 0 0 0 A 0 0 0 0 A 0 0 0 0 A A 0 0 0 0 A 0 0 0 0 A 0 0 0 0 A ,$ (1)
where
 $A = 1 1 0 0 1 0 1 0 1 0 0 1 .$
The block structure evident in (1) is modelled by specifying a four-level subject variable, taking the values $\left\{1,1,1,2,2,2,3,3,3,4,4,4,1,1,1,2,2,2,3,3,3,4,4,4\right\}$. The first column of $1\mathrm{s}$ is added to $A$ by setting ${\mathbf{rint}}=1$. The remaining columns of $A$ are specified by a three level factor, taking the values, $\left\{1,2,3,1,2,3,1,\dots \right\}$.
```function g02jb_example

fprintf('g02jb example results\n\n');

dat = [56, 1, 1, 1, 1;
50, 1, 2, 1, 1;
39, 1, 3, 1, 1;
30, 2, 1, 1, 1;
36, 2, 2, 1, 1;
33, 2, 3, 1, 1;
32, 3, 1, 1, 1;
31, 3, 2, 1, 1;
15, 3, 3, 1, 1;
30, 4, 1, 1, 1;
35, 4, 2, 1, 1;
17, 4, 3, 1, 1;
41, 1, 1, 2, 1;
36, 1, 2, 2, 2;
35, 1, 3, 2, 3;
25, 2, 1, 2, 1;
28, 2, 2, 2, 2;
30, 2, 3, 2, 3;
24, 3, 1, 2, 1;
27, 3, 2, 2, 2;
19, 3, 3, 2, 3;
25, 4, 1, 2, 1;
30, 4, 2, 2, 2;
18, 4, 3, 2, 3];
[n,ncol] = size(dat);

% Number of levels in each variable
levels = [int64(1);4;3;2;3];

% Model information
yvid =  int64(1);
fvid = [int64(3);  4;  5];
rvid = [int64(3)];
svid =  int64(2);
cwid =  int64(0);
fint =  int64(1);
rint =  int64(1);

% Calculate lb
lb = (rint + sum(levels(rvid)))*prod(levels(svid)) + ...
fint + sum(levels(fvid)) - numel(fvid);

% Variance component
vpr = [int64(1)];
nvpr = int64(numel(vpr));

% Initial gamma
gamma = [1; 1; 0];
% Fit the linear mixed effects regresion model
[nff, nrf, df, ml, b, se, gamma, warn, ifail] = ...
g02jb( ...
nvpr, levels, yvid, fvid, rvid, svid, cwid, vpr, ...
dat, fint, rint, lb, gamma);

% Display results
if warn
fprintf(['Warning: At least one variance component was ', ...
'estimated to be negative and then reset to zero\n\n']);
end
fprintf('Fixed effects (Estimate and Standard Deviation)\n\n');
k = 1;
if fint==1
fprintf('Intercept%15s%10.4f%10.4f\n', ' ',b(k), se(k));
k = k + 1;
end
for i = 1:numel(fvid)
for j = 1:levels(fvid(i))
if levels(fvid(i))==1 || j>1
fprintf('Variable %3d Level %3d: %10.4f%10.4f\n', i, j, b(k), se(k));
k = k + 1;
end
end
end
fprintf('\nRandom Effects (Estimate and Standard Deviation\n\n');
if svid==0
for i = 1:numel(rvid)
for j = 1:levels(rvid(i))
fprintf('Variable %4d Level %4d: %10.4f %10.4f\n', i, j, b(k), se(k));
k = k + 1;
end
end
else
for l = 1:levels(svid)
if (rint==1)
fprintf('Intercept for Subject Level %4d:%12s%10.4f%10.4f\n', ...
l, ' ', b(k), se(k));
k = k + 1;
end
for i = 1:numel(rvid)
for j = 1:levels(rvid(i))
fprintf('Subject Level %4d Variable %4d Level %4d: %10.4f%10.4f\n', ...
l, i, j, b(k), se(k));
k = k + 1;
end
end
end
end
fprintf('\n Variance Components\n');
for i = 1:nvpr+rint
fprintf('%4d%10.4f\n',i,gamma(i));
end
fprintf('\nsigma^2           = %10.4f\n', gamma(nvpr+rint+1));
fprintf('-2log likelihood  = %10.4f\n', ml);
fprintf('DF                = %16d\n', df);

```
```g02jb example results

Fixed effects (Estimate and Standard Deviation)

Intercept                  37.0000    4.0421
Variable   1 Level   2:     1.0000    3.0461
Variable   1 Level   3:   -11.0000    3.0461
Variable   2 Level   2:    -8.2500    1.8736
Variable   3 Level   2:     0.5000    2.6497
Variable   3 Level   3:     7.7500    2.6497

Random Effects (Estimate and Standard Deviation

Intercept for Subject Level    1:               10.7631    3.8855
Subject Level    1 Variable    1 Level    1:     3.7276    2.6268
Subject Level    1 Variable    1 Level    2:    -1.4476    2.6268
Subject Level    1 Variable    1 Level    3:     0.3733    2.6268
Intercept for Subject Level    2:               -0.5269    3.8855
Subject Level    2 Variable    1 Level    1:    -3.7171    2.6268
Subject Level    2 Variable    1 Level    2:    -1.2253    2.6268
Subject Level    2 Variable    1 Level    3:     4.8125    2.6268
Intercept for Subject Level    3:               -5.6450    3.8855
Subject Level    3 Variable    1 Level    1:     0.5903    2.6268
Subject Level    3 Variable    1 Level    2:     0.3987    2.6268
Subject Level    3 Variable    1 Level    3:    -2.3806    2.6268
Intercept for Subject Level    4:               -4.5912    3.8855
Subject Level    4 Variable    1 Level    1:    -0.6009    2.6268
Subject Level    4 Variable    1 Level    2:     2.2742    2.6268
Subject Level    4 Variable    1 Level    3:    -2.8052    2.6268

Variance Components
1   46.7969
2   11.5365

sigma^2           =     7.0208
-2log likelihood  =   141.6877
DF                =               16
```