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_reml (g02ja)

## Purpose

nag_correg_mixeff_reml (g02ja) fits a linear mixed effects regression model using restricted maximum likelihood (REML).

## Syntax

[nff, nrf, df, reml, b, se, gamma, warn, ifail] = g02ja(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, reml, b, se, gamma, warn, ifail] = nag_correg_mixeff_reml(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:
Mark 22: n has been made optional
Mark 23: maxit, tol optional
.

## Description

nag_correg_mixeff_reml (g02ja) fits a model of the form:
 y = Xβ + Zν + ε $y=Xβ+Zν+ε$
where
• y$y$ is a vector of n$n$ observations on the dependent variable,
• X$X$ is a known n$n$ by p$p$ design matrix for the fixed independent variables,
• β$\beta$ is a vector of length p$p$ of unknown fixed effects,
• Z$Z$ is a known n$n$ by q$q$ design matrix for the random independent variables,
• ν$\nu$ is a vector of length q$q$ of unknown random effects,
and
• ε$\epsilon$ is a vector of length n$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 ]
$Var[ ν ε ] = [ G 0 0 R ]$
where R = σR2 I $R={\sigma }_{R}^{2}I$, I $I$ is the n × n $n×n$ identity matrix and G $G$ is a diagonal matrix. It is assumed that the random variables, Z $Z$, can be subdivided into g q $g\le q$ groups with each group being identically distributed with expectations zero and variance σi2 ${\sigma }_{i}^{2}$. The diagonal elements of matrix G $G$ therefore take one of the values {σi2 : i = 1,2,,g} $\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 $g+1$ variance components, γ $\gamma$, where γ = {σ12,σ22,,σg12,σg2,σR2} $\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_reml (g02ja) uses an iterative process to estimate γ* = { σ12 / σR2 , σ22 / σR2 ,, σg12 / σR2 , σg2 / σR2 ,1} ${\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, γ0 ${\gamma }_{0}$, for γ* ${\gamma }^{*}$ is required. nag_correg_mixeff_reml (g02ja) 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_reml (g02ja) fits the model using a quasi-Newton algorithm to maximize the restricted log-likelihood function:
 − 2 lR = log(|V|) + (n − p) log(r′V − 1r) + log|X′V − 1X| + (n − p) (1 + log(2π / (n − p))) $-2 l R = log( |V| ) + ( n-p ) log( r ′ V-1 r ) + log| X ′ V-1 X | + ( n-p ) ( 1+ log( 2 π / ( n-p ) ) )$
where
 V = ZG Z′ + R,   r = y − Xb   and   b = (X′V − 1X) − 1 X′ V − 1 y . $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 σR2 ${\sigma }_{R}^{2}$ is given by:
 σR2 = (r′V − 1r) / (n − p) . $σR2 = ( r′ V-1 r ) / ( n - p ) .$
Case weights, Wc ${W}_{c}$, can be incorporated into the model by replacing XX ${X}^{\prime }X$ and ZZ ${Z}^{\prime }Z$ with XWcX ${X}^{\prime }{W}_{c}X$ and ZWcZ ${Z}^{\prime }{W}_{c}Z$ respectively, for a diagonal weight matrix Wc ${W}_{c}$.
The log-likelihood, lR${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:     nvpr – int64int32nag_int scalar
If rint = 1${\mathbf{rint}}=1$ and svid0${\mathbf{svid}}\ne 0$, nvpr is the number of variance components being estimated2$\text{estimated}-2$, (g1$g-1$), else nvpr = g${\mathbf{nvpr}}=g$.
If nrv = 0${\mathbf{nrv}}=0$, nvpr${\mathbf{nvpr}}$ is not referenced.
Constraint: if nrv0${\mathbf{nrv}}\ne 0$, 1nvprnrv$1\le {\mathbf{nvpr}}\le {\mathbf{nrv}}$.
2:     levels(ncol) – int64int32nag_int array
ncol, the dimension of the array, must satisfy the constraint ncol1${\mathbf{ncol}}\ge 1$.
levels(i)${\mathbf{levels}}\left(i\right)$ contains the number of levels associated with the i$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 levels(i)${\mathbf{levels}}\left(i\right)$ should be 1$1$; if the variable is discrete then levels(i)${\mathbf{levels}}\left(i\right)$ is the number of levels associated with it and DAT(j,i)${\mathbf{DAT}}\left(\mathit{j},i\right)$ is assumed to take the values 1$1$ to levels(i)${\mathbf{levels}}\left(i\right)$, for j = 1,2,,n$\mathit{j}=1,2,\dots ,{\mathbf{n}}$.
Constraint: levels(i)1${\mathbf{levels}}\left(\mathit{i}\right)\ge 1$, for i = 1,2,,ncol$\mathit{i}=1,2,\dots ,{\mathbf{ncol}}$.
3:     yvid – int64int32nag_int scalar
The column of dat holding the dependent, y$y$, variable.
Constraint: 1yvidncol$1\le {\mathbf{yvid}}\le {\mathbf{ncol}}$.
4:     fvid(nfv) – int64int32nag_int array
nfv, the dimension of the array, must satisfy the constraint 0nfv < ncol$0\le {\mathbf{nfv}}<{\mathbf{ncol}}$.
The columns of the data matrix dat holding the fixed independent variables with fvid(i) ${\mathbf{fvid}}\left(i\right)$ holding the column number corresponding to the i $i$th fixed variable.
Constraint: 1fvid(i)ncol$1\le {\mathbf{fvid}}\left(\mathit{i}\right)\le {\mathbf{ncol}}$, for i = 1,2,,nfv$\mathit{i}=1,2,\dots ,{\mathbf{nfv}}$.
5:     rvid(nrv) – int64int32nag_int array
nrv, the dimension of the array, must satisfy the constraint
• 0nrv < ncol$0\le {\mathbf{nrv}}<{\mathbf{ncol}}$
• nrv + rint > 0${\mathbf{nrv}}+{\mathbf{rint}}>0$
• .
The columns of the data matrix DAT${\mathbf{DAT}}$ holding the random independent variables with rvid(i) ${\mathbf{rvid}}\left(i\right)$ holding the column number corresponding to the i $i$th random variable.
Constraint: 1rvid(i)ncol$1\le {\mathbf{rvid}}\left(\mathit{i}\right)\le {\mathbf{ncol}}$, for i = 1,2,,nrv$\mathit{i}=1,2,\dots ,{\mathbf{nrv}}$.
6:     svid – int64int32nag_int scalar
The column of dat holding the subject variable.
If svid = 0${\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 Z1 × ZS ${Z}_{1}×{Z}_{S}$ denote the interaction between variables Z1 ${Z}_{1}$ and ZS ${Z}_{S}$, fitting a model with rint = 0 ${\mathbf{rint}}=0$, random-effects Z1 + Z2 ${Z}_{1}+{Z}_{2}$ and subject variable ZS ${Z}_{S}$ is equivalent to fitting a model with random-effects Z1 × ZS + Z2 × ZS ${Z}_{1}×{Z}_{S}+{Z}_{2}×{Z}_{S}$ and no subject variable. If rint = 1 ${\mathbf{rint}}=1$ the model is equivalent to fitting ZS + Z1 × ZS + Z2 × ZS ${Z}_{S}+{Z}_{1}×{Z}_{S}+{Z}_{2}×{Z}_{S}$ and no subject variable.
Constraint: 0svidncol$0\le {\mathbf{svid}}\le {\mathbf{ncol}}$.
7:     cwid – int64int32nag_int scalar
The column of dat holding the case weights.
If cwid = 0${\mathbf{cwid}}=0$, no weights are used.
Constraint: 0cwidncol$0\le {\mathbf{cwid}}\le {\mathbf{ncol}}$.
8:     vpr(nrv) – int64int32nag_int array
nrv, the dimension of the array, must satisfy the constraint
• 0nrv < ncol$0\le {\mathbf{nrv}}<{\mathbf{ncol}}$
• nrv + rint > 0${\mathbf{nrv}}+{\mathbf{rint}}>0$
• .
vpr(i) ${\mathbf{vpr}}\left(i\right)$ holds a flag indicating the variance of the i $i$th random variable. The variance of the i $i$th random variable is σj2 ${\sigma }_{j}^{2}$, where j = vpr(i) + 1 $j={\mathbf{vpr}}\left(i\right)+1$ if rint = 1${\mathbf{rint}}=1$ and svid0${\mathbf{svid}}\ne 0$ and j = vpr(i) $j={\mathbf{vpr}}\left(i\right)$ otherwise. Random variables with the same value of j$j$ are assumed to be taken from the same distribution.
Constraint: 1vpr(i)nvpr$1\le {\mathbf{vpr}}\left(\mathit{i}\right)\le {\mathbf{nvpr}}$, for i = 1,2,,nrv$\mathit{i}=1,2,\dots ,{\mathbf{nrv}}$.
9:     dat(lddat,ncol) – double array
lddat, the first dimension of the array, must satisfy the constraint lddatn$\mathit{lddat}\ge {\mathbf{n}}$.
Array containing all of the data. For the i$i$th observation:
• DAT(i,yvid)${\mathbf{DAT}}\left(i,{\mathbf{yvid}}\right)$ holds the dependent variable, y$y$;
• if cwid0${\mathbf{cwid}}\ne 0$, DAT(i,cwid)${\mathbf{DAT}}\left(i,{\mathbf{cwid}}\right)$ holds the case weights;
• if svid0${\mathbf{svid}}\ne 0$, DAT(i,svid)${\mathbf{DAT}}\left(i,{\mathbf{svid}}\right)$ holds the subject variable.
The remaining columns hold the values of the independent variables.
Constraints:
• if cwid0${\mathbf{cwid}}\ne 0$, DAT(i,cwid)0.0${\mathbf{DAT}}\left(i,{\mathbf{cwid}}\right)\ge 0.0$;
• if levels(j)1${\mathbf{levels}}\left(j\right)\ne 1$, 1DAT(i,j)levels(j)$1\le {\mathbf{DAT}}\left(i,j\right)\le {\mathbf{levels}}\left(j\right)$.
10:   fint – int64int32nag_int scalar
Flag indicating whether a fixed intercept is included (fint = 1${\mathbf{fint}}=1$).
Constraint: fint = 0${\mathbf{fint}}=0$ or 1$1$.
11:   rint – int64int32nag_int scalar
Flag indicating whether a random intercept is included (rint = 1${\mathbf{rint}}=1$).
If svid = 0${\mathbf{svid}}=0$, rint is not referenced.
Constraint: rint = 0${\mathbf{rint}}=0$ or 1$1$.
12:   lb – int64int32nag_int scalar
The size of the array b.
Constraint: lb fint + i = 1nfv max (levels(fvid(i))1,1) + LS × (rint + i = 1nrvlevels(rvid(i))) ${\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 LS = ${L}_{S}={\mathbf{levels}}\left({\mathbf{svid}}\right)$ if svid0${\mathbf{svid}}\ne 0$ and 1$1$ otherwise.
13:   gamma(nvpr + 2${\mathbf{nvpr}}+2$) – double array
Holds the initial values of the variance components, γ0 ${\gamma }_{0}$, with gamma(i)${\mathbf{gamma}}\left(\mathit{i}\right)$ the initial value for σi2 / σR2${\sigma }_{\mathit{i}}^{2}/{\sigma }_{R}^{2}$, for i = 1,2,,g$\mathit{i}=1,2,\dots ,g$. If rint = 1${\mathbf{rint}}=1$ and svid0${\mathbf{svid}}\ne 0$, g = nvpr + 1$g={\mathbf{nvpr}}+1$, else g = nvpr$g={\mathbf{nvpr}}$.
If gamma(1) = 1.0${\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: gamma(1) = 1.0 ​ or ​ gamma(i)0.0${\mathbf{gamma}}\left(1\right)=-1.0\text{​ or ​}{\mathbf{gamma}}\left(\mathit{i}\right)\ge 0.0$, for i = 1,2,,g$\mathit{i}=1,2,\dots ,g$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The first dimension of the array dat.
n$n$, the number of observations.
Constraint: n1${\mathbf{n}}\ge 1$.
2:     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: ncol1${\mathbf{ncol}}\ge 1$.
3:     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: 0nfv < ncol$0\le {\mathbf{nfv}}<{\mathbf{ncol}}$.
4:     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:
• 0nrv < ncol$0\le {\mathbf{nrv}}<{\mathbf{ncol}}$;
• nrv + rint > 0${\mathbf{nrv}}+{\mathbf{rint}}>0$.
5:     maxit – int64int32nag_int scalar
The maximum number of iterations.
If maxit < 0 ${\mathbf{maxit}}<0$, the default value of 100 $100$ is used.
If maxit = 0${\mathbf{maxit}}=0$, the parameter estimates (β,ν) $\left(\beta ,\nu \right)$ and corresponding standard errors are calculated based on the value of γ0 ${\gamma }_{0}$ supplied in gamma.
Default: -1$-1$
6:     tol – double scalar
The tolerance used to assess convergence.
If tol0.0${\mathbf{tol}}\le 0.0$, the default value of ε0.7${\epsilon }^{0.7}$ is used, where ε$\epsilon$ is the machine precision.
Default: 0$0$

lddat

### Output Parameters

1:     nff – int64int32nag_int scalar
The number of fixed effects estimated (i.e., the number of columns, p$p$, in the design matrix X$X$).
2:     nrf – int64int32nag_int scalar
The number of random effects estimated (i.e., the number of columns, q$q$, in the design matrix Z$Z$).
3:     df – int64int32nag_int scalar
The degrees of freedom.
4:     reml – double scalar
2 lR (γ̂) $-2{l}_{R}\left(\stackrel{^}{\gamma }\right)$ where lR ${l}_{R}$ is the log of the restricted maximum likelihood calculated at γ̂ $\stackrel{^}{\gamma }$, the estimated variance components returned in gamma.
5:     b(lb) – 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 fint = 1${\mathbf{fint}}=1$, b(1)${\mathbf{b}}\left(1\right)$ contains the estimate of the fixed intercept. Let Li ${L}_{i}$ denote the number of levels associated with the i$i$th fixed variable, that is Li = levels(fvid(i)) ${L}_{i}={\mathbf{levels}}\left({\mathbf{fvid}}\left(i\right)\right)$. Define
• if fint = 1${\mathbf{fint}}=1$, F1 = 2 ${F}_{1}=2$ else if fint = 0${\mathbf{fint}}=0$, F1 = 1 ${F}_{1}=1$;
• Fi + 1 = Fi + max (Li1,1) ${F}_{i+1}={F}_{i}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({L}_{i}-1,1\right)$, i1 $i\ge 1$.
Then for i = 1,2,,nfv$i=1,2,\dots ,{\mathbf{nfv}}$:
• if Li > 1 ${L}_{i}>1$, b( Fi + j2 ) ${\mathbf{b}}\left({F}_{i}+\mathit{j}-2\right)$ contains the parameter estimate for the j$\mathit{j}$th level of the i$i$th fixed variable, for j = 2,3,,Li$\mathit{j}=2,3,\dots ,{L}_{i}$;
• if Li 1 ${L}_{i}\le 1$, b(Fi) ${\mathbf{b}}\left({F}_{i}\right)$ contains the parameter estimate for the i$i$th fixed variable.
Random effects
Redefining Li ${L}_{i}$ to denote the number of levels associated with the i$i$th random variable, that is Li = levels(rvid(i)) ${L}_{i}={\mathbf{levels}}\left({\mathbf{rvid}}\left(i\right)\right)$. Define
• if rint = 1${\mathbf{rint}}=1$, R1 = 2 ${R}_{1}=2$ else if rint = 0${\mathbf{rint}}=0$, R1 = 1 ${R}_{1}=1$;
Ri + 1 = Ri + Li ${R}_{i+1}={R}_{i}+{L}_{i}$, i1 $i\ge 1$.
Then for i = 1 , 2 , , nrv $i=1,2,\dots ,{\mathbf{nrv}}$:
• if svid = 0${\mathbf{svid}}=0$,
• if Li > 1 ${L}_{i}>1$, b( nff + Ri + j1 ) ${\mathbf{b}}\left({\mathbf{nff}}+{R}_{i}+\mathit{j}-1\right)$ contains the parameter estimate for the j$\mathit{j}$th level of the i$i$th random variable, for j = 1,2,,Li$\mathit{j}=1,2,\dots ,{L}_{i}$;
• if Li 1 ${L}_{i}\le 1$, b( nff + Ri ) ${\mathbf{b}}\left({\mathbf{nff}}+{R}_{i}\right)$ contains the parameter estimate for the i$i$th random variable;
• if svid 0 ${\mathbf{svid}}\ne 0$,
• let LS ${L}_{S}$ denote the number of levels associated with the subject variable, that is LS = ${L}_{S}={\mathbf{levels}}\left({\mathbf{svid}}\right)$;
• if Li > 1 ${L}_{i}>1$, b( nff + (s1) LS + Ri + j 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 s$\mathit{s}$th level of the subject variable and the j$\mathit{j}$th level of the i$i$th random variable, for s = 1,2,,LS$\mathit{s}=1,2,\dots ,{L}_{S}$ and j = 1,2,,Li$\mathit{j}=1,2,\dots ,{L}_{i}$;
• if Li 1 ${L}_{i}\le 1$, b( nff + (s1) LS + Ri ) ${\mathbf{b}}\left({\mathbf{nff}}+\left(\mathit{s}-1\right){L}_{S}+{R}_{i}\right)$ contains the parameter estimate for the interaction between the s$\mathit{s}$th level of the subject variable and the i$i$th random variable, for s = 1,2,,LS$\mathit{s}=1,2,\dots ,{L}_{S}$;
• if rint = 1${\mathbf{rint}}=1$, b (nff + 1) ${\mathbf{b}}\left({\mathbf{nff}}+1\right)$ contains the estimate of the random intercept.
6:     se(lb) – double array
The standard errors of the parameter estimates given in b.
7:     gamma(nvpr + 2${\mathbf{nvpr}}+2$) – double array
gamma(i)${\mathbf{gamma}}\left(\mathit{i}\right)$, for i = 1,2,,g$\mathit{i}=1,2,\dots ,g$, holds the final estimate of σi2${\sigma }_{\mathit{i}}^{2}$ and gamma(g + 1)${\mathbf{gamma}}\left(g+1\right)$ holds the final estimate for σR2${\sigma }_{R}^{2}$.
8:     warn – int64int32nag_int scalar
Is set to 1 $1$ if a variance component was estimated to be a negative value during the fitting process. Otherwise warn is set to 0 $0$.
If warn = 1${\mathbf{warn}}=1$, the negative estimate is set to zero and the estimation process allowed to continue.
9:     ifail – int64int32nag_int scalar
${\mathrm{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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 2${\mathbf{n}}<2$, or ncol < 1${\mathbf{ncol}}<1$, or lddat < n$\mathit{lddat}<{\mathbf{n}}$, or yvid < 1${\mathbf{yvid}}<1$ or ${\mathbf{yvid}}>{\mathbf{ncol}}$, or cwid < 0${\mathbf{cwid}}<0$ or ${\mathbf{cwid}}>{\mathbf{ncol}}$, or nfv < 0${\mathbf{nfv}}<0$ or ${\mathbf{nfv}}\ge {\mathbf{ncol}}$, or fint ≠ 0${\mathbf{fint}}\ne 0$ and fint ≠ 1${\mathbf{fint}}\ne 1$, or nrv < 0${\mathbf{nrv}}<0$ or ${\mathbf{nrv}}>{\mathbf{ncol}}$ or nrv + rint ≤ 0${\mathbf{nrv}}+{\mathbf{rint}}\le 0$, or nvpr ≤ 0${\mathbf{nvpr}}\le 0$ or ${\mathbf{nvpr}}>{\mathbf{nrv}}$, or rint ≠ 0${\mathbf{rint}}\ne 0$ and rint ≠ 1${\mathbf{rint}}\ne 1$, or svid < 0${\mathbf{svid}}<0$ or ${\mathbf{svid}}>{\mathbf{ncol}}$, or lb is too small.
ifail = 2${\mathbf{ifail}}=2$
 On entry, levels(i) < 1${\mathbf{levels}}\left(i\right)<1$, for at least one i$i$, or fvid(i) < 1${\mathbf{fvid}}\left(i\right)<1$, or fvid(i) > ncol${\mathbf{fvid}}\left(i\right)>{\mathbf{ncol}}$, for at least one i$i$, or rvid(i) < 1${\mathbf{rvid}}\left(i\right)<1$, or rvid(i) > ncol${\mathbf{rvid}}\left(i\right)>{\mathbf{ncol}}$, for at least one i$i$, or vpr(i) < 1${\mathbf{vpr}}\left(i\right)<1$ or vpr(i) > nvpr${\mathbf{vpr}}\left(i\right)>{\mathbf{nvpr}}$, for at least one i$i$, or at least one discrete variable in array dat has a value less than 1.0$1.0$ or greater than that specified in levels, or gamma(i) < 0.0${\mathbf{gamma}}\left(i\right)<0.0$, for at least one i$i$, and gamma(1) ≠ − 1.0${\mathbf{gamma}}\left(1\right)\ne -1.0$.
ifail = 3${\mathbf{ifail}}=3$
Degrees of freedom < 1$<1$. The number of parameters exceed the effective number of observations.
ifail = 4${\mathbf{ifail}}=4$
The function failed to converge to the specified tolerance in maxit iterations. See Section [Further Comments] for advice.

## Accuracy

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

Wherever possible any block structure present in the design matrix Z$Z$ should be modelled through a subject variable, specified via svid, rather than being explicitly entered into dat.
nag_correg_mixeff_reml (g02ja) 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 maximum likelihood (see nag_correg_mixeff_ml (g02jb)) or using the noniterative MIVQUE0.
To fit the model just using MIVQUE0, the first element of gamma should be set to 1.0$-1.0$ and maxit should be set to zero.
Although the quasi-Newton algorithm used in nag_correg_mixeff_reml (g02ja) 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

```function nag_correg_mixeff_reml_example
nvpr = int64(1);
levels = [int64(1);4;3;2;3];
yvid = int64(1);
fvid = [int64(3);4;5];
rvid = [int64(3)];
svid = int64(2);
cwid = int64(0);
vpr = [int64(1)];
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];
fint = int64(1);
rint = int64(1);
lb = int64(25);
gamma = [1; 1; 0];
[nff, nrf, df, reml, b, se, gammaOut, warn, ifail] = ...
nag_correg_mixeff_reml(nvpr, levels, yvid, fvid, rvid, svid, cwid, vpr, dat, fint, rint, lb, gamma)
```
```

nff =

6

nrf =

16

df =

16

reml =

119.7618

b =

37.0000
1.0000
-11.0000
-8.2500
0.5000
7.7500
10.7631
3.7276
-1.4476
0.3733
-0.5269
-3.7171
-1.2253
4.8125
-5.6450
0.5903
0.3987
-2.3806
-4.5912
-0.6009
2.2742
-2.8052
0
0
0

se =

4.6674
3.5173
3.5173
2.1635
3.0596
3.0596
4.4865
3.0331
3.0331
3.0331
4.4865
3.0331
3.0331
3.0331
4.4865
3.0331
3.0331
3.0331
4.4865
3.0331
3.0331
3.0331
0
0
0

gammaOut =

62.3958
15.3819
9.3611

warn =

0

ifail =

0

```
```function g02ja_example
nvpr = int64(1);
levels = [int64(1);4;3;2;3];
yvid = int64(1);
fvid = [int64(3);4;5];
rvid = [int64(3)];
svid = int64(2);
cwid = int64(0);
vpr = [int64(1)];
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];
fint = int64(1);
rint = int64(1);
lb = int64(25);
gamma = [1; 1; 0];
[nff, nrf, df, reml, b, se, gammaOut, warn, ifail] = ...
g02ja(nvpr, levels, yvid, fvid, rvid, svid, cwid, vpr, dat, fint, rint, lb, gamma)
```
```

nff =

6

nrf =

16

df =

16

reml =

119.7618

b =

37.0000
1.0000
-11.0000
-8.2500
0.5000
7.7500
10.7631
3.7276
-1.4476
0.3733
-0.5269
-3.7171
-1.2253
4.8125
-5.6450
0.5903
0.3987
-2.3806
-4.5912
-0.6009
2.2742
-2.8052
0
0
0

se =

4.6674
3.5173
3.5173
2.1635
3.0596
3.0596
4.4865
3.0331
3.0331
3.0331
4.4865
3.0331
3.0331
3.0331
4.4865
3.0331
3.0331
3.0331
4.4865
3.0331
3.0331
3.0331
0
0
0

gammaOut =

62.3958
15.3819
9.3611

warn =

0

ifail =

0

```