# NAG CL Interfaceg02jcc (mixeff_​hier_​init)

Note: this function is deprecated. Replaced by g02jfc.

Settings help

CL Name Style:

## 1Purpose

g02jcc preprocesses a dataset prior to fitting a linear mixed effects regression model of the following form via either g02jdc or g02jec.

## 2Specification

 #include
 void g02jcc (Nag_OrderType order, Integer n, Integer ncol, const double dat[], Integer pddat, const Integer levels[], const double y[], const double wt[], const Integer fixed[], Integer lfixed, Integer nrndm, const Integer rndm[], Integer lrndm, Integer *nff, Integer *nlsv, Integer *nrf, double rcomm[], Integer lrcomm, Integer icomm[], Integer licomm, NagError *fail)
The function may be called by the names: g02jcc, nag_correg_mixeff_hier_init or nag_hier_mixed_init.

## 3Description

g02jcc must be called prior to fitting a linear mixed effects regression model with either g02jdc or g02jec.
The model fitting functions g02jdc and g02jec fit a model of the following form:
 $y=Xβ+Zν+ε$
 where $y$ is a vector of $n$ observations on the dependent variable, $X$ is an $n×p$ design matrix of fixed independent variables, $\beta$ is a vector of $p$ unknown fixed effects, $Z$ is an $n×q$ design matrix of random independent variables, $\nu$ is a vector of length $q$ of unknown random effects, $\epsilon$ is a vector of length $n$ of unknown random errors,
and $\nu$ and $\epsilon$ are Normally distributed with expectation zero and variance/covariance matrix defined by
 $Var[ ν ε ] = [ G 0 0 R ]$
where $R={\sigma }_{R}^{2}I$, $I$ is the $n×n$ identity matrix and $G$ is a diagonal matrix.
Case weights can be incorporated into the model by replacing $X$ and $Z$ with ${W}_{c}^{1/2}X$ and ${W}_{c}^{1/2}Z$ respectively where ${W}_{c}$ is a diagonal weight matrix.

None.

## 5Arguments

1: $\mathbf{order}$Nag_OrderType Input
On entry: the order argument specifies the two-dimensional storage scheme being used, i.e., row-major ordering or column-major ordering. C language defined storage is specified by ${\mathbf{order}}=\mathrm{Nag_RowMajor}$. See Section 3.1.3 in the Introduction to the NAG Library CL Interface for a more detailed explanation of the use of this argument.
Constraint: ${\mathbf{order}}=\mathrm{Nag_RowMajor}$ or $\mathrm{Nag_ColMajor}$.
2: $\mathbf{n}$Integer Input
On entry: $n$, the number of observations.
The effective number of observations, that is the number of observations with nonzero weight (see wt for more detail), must be greater than the number of fixed effects in the model (as returned in nff).
Constraint: ${\mathbf{n}}\ge 1$.
3: $\mathbf{ncol}$Integer Input
On entry: the number of columns in the data matrix, dat.
Constraint: ${\mathbf{ncol}}\ge 0$.
4: $\mathbf{dat}\left[\mathit{dim}\right]$const double Input
Note: the dimension, dim, of the array dat must be at least
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{pddat}}×{\mathbf{ncol}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}×{\mathbf{pddat}}\right)$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
where ${\mathbf{DAT}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{dat}}\left[\left(j-1\right)×{\mathbf{pddat}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{dat}}\left[\left(i-1\right)×{\mathbf{pddat}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: a matrix of data, with ${\mathbf{DAT}}\left(i,j\right)$ holding the $i$th observation on the $j$th variable. The two design matrices $X$ and $Z$ are constructed from dat and the information given in fixed (for $X$) and rndm (for $Z$).
Constraint: if ${\mathbf{levels}}\left[j-1\right]\ne 1,1\le {\mathbf{DAT}}\left(i,j\right)\le {\mathbf{levels}}\left[j-1\right]$.
5: $\mathbf{pddat}$Integer Input
On entry: the stride separating row or column elements (depending on the value of order) in the array dat.
Constraints:
• if ${\mathbf{order}}=\mathrm{Nag_ColMajor}$, ${\mathbf{pddat}}\ge {\mathbf{n}}$;
• if ${\mathbf{order}}=\mathrm{Nag_RowMajor}$, ${\mathbf{pddat}}\ge {\mathbf{ncol}}$.
6: $\mathbf{levels}\left[{\mathbf{ncol}}\right]$const Integer Input
On entry: ${\mathbf{levels}}\left[i-1\right]$ contains the number of levels associated with the $i$th variable held in dat.
If the $i$th variable is continuous or binary (i.e., only takes the values zero or one), then ${\mathbf{levels}}\left[i-1\right]$ must be set to $1$. Otherwise the $i$th variable is assumed to take an integer value between $1$ and ${\mathbf{levels}}\left[i-1\right]$, (i.e., the $i$th variable is discrete with ${\mathbf{levels}}\left[i-1\right]$ levels).
Constraint: ${\mathbf{levels}}\left[\mathit{i}-1\right]\ge 1$, for $\mathit{i}=1,2,\dots ,{\mathbf{ncol}}$.
7: $\mathbf{y}\left[{\mathbf{n}}\right]$const double Input
On entry: $y$, the vector of observations on the dependent variable.
8: $\mathbf{wt}\left[\mathit{dim}\right]$const double Input
Note: the dimension, dim, of the array wt must be at least
• ${\mathbf{n}}$, when ${\mathbf{wt}}\phantom{\rule{0.25em}{0ex}}\text{is not}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$.
On entry: optionally, the weights to be used in the weighted regression.
If ${\mathbf{wt}}\left[i-1\right]=0.0$, the $i$th observation is not included in the model, in which case the effective number of observations is the number of observations with nonzero weights.
If weights are not provided then wt must be set to NULL and the effective number of observations is n.
Constraint: if ${\mathbf{wt}}\phantom{\rule{0.25em}{0ex}}\text{is not}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$, ${\mathbf{wt}}\left[\mathit{i}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
9: $\mathbf{fixed}\left[{\mathbf{lfixed}}\right]$const Integer Input
On entry: defines the structure of the fixed effects design matrix, $X$.
${\mathbf{fixed}}\left[0\right]$
The number of variables, ${N}_{F}$, to include as fixed effects (not including the intercept if present).
${\mathbf{fixed}}\left[1\right]$
The fixed intercept flag which must contain $1$ if a fixed intercept is to be included and $0$ otherwise.
${\mathbf{fixed}}\left[2+i-1\right]$
The column of DAT holding the $\mathit{i}$th fixed variable, for $\mathit{i}=1,2,\dots ,{\mathbf{fixed}}\left[0\right]$.
See Section 9.1 for more details on the construction of $X$.
Constraints:
• ${\mathbf{fixed}}\left[0\right]\ge 0$;
• ${\mathbf{fixed}}\left[1\right]=0$ or $1$;
• $1\le {\mathbf{fixed}}\left[2+\mathit{i}-1\right]\le {\mathbf{ncol}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{fixed}}\left[0\right]$.
10: $\mathbf{lfixed}$Integer Input
On entry: length of the vector fixed.
Constraint: ${\mathbf{lfixed}}\ge 2+{\mathbf{fixed}}\left[0\right]$.
11: $\mathbf{nrndm}$Integer Input
On entry: the number of columns in RNDM.
Constraint: ${\mathbf{nrndm}}>0$.
12: $\mathbf{rndm}\left[{\mathbf{lrndm}}×{\mathbf{nrndm}}\right]$const Integer Input
Note: where ${\mathbf{RNDM}}\left(i,j\right)$ appears in this document, it refers to the array element
• ${\mathbf{rndm}}\left[\left(j-1\right)×{\mathbf{lrndm}}+i-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_ColMajor}$;
• ${\mathbf{rndm}}\left[\left(i-1\right)×{\mathbf{nrndm}}+j-1\right]$ when ${\mathbf{order}}=\mathrm{Nag_RowMajor}$.
On entry: ${\mathbf{RNDM}}\left(i,j\right)$ defines the structure of the random effects design matrix, $Z$. The $b$th column of RNDM defines a block of columns in the design matrix $Z$.
${\mathbf{RNDM}}\left(1,b\right)$
The number of variables, ${N}_{{R}_{b}}$, to include as random effects in the $b$th block (not including the random intercept if present).
${\mathbf{RNDM}}\left(2,b\right)$
The random intercept flag which must contain $1$ if block $b$ includes a random intercept and $0$ otherwise.
${\mathbf{RNDM}}\left(2+i,b\right)$
The column of DAT holding the $\mathit{i}$th random variable in the $b$th block, for $\mathit{i}=1,2,\dots ,{\mathbf{RNDM}}\left(1,b\right)$.
${\mathbf{RNDM}}\left(3+{N}_{{R}_{b}},b\right)$
The number of subject variables, ${N}_{{S}_{b}}$, for the $b$th block. The subject variables define the nesting structure for this block.
${\mathbf{RNDM}}\left(3+{N}_{{R}_{b}}+i,b\right)$
The column of DAT holding the $\mathit{i}$th subject variable in the $b$th block, for $\mathit{i}=1,2,\dots ,{\mathbf{RNDM}}\left(3+{N}_{{R}_{b}},b\right)$.
See Section 9.2 for more details on the construction of $Z$.
Constraints:
• ${\mathbf{RNDM}}\left(1,b\right)\ge 0$;
• ${\mathbf{RNDM}}\left(2,b\right)=0$ or $1$;
• at least one random variable or random intercept must be specified in each block, i.e., ${\mathbf{RNDM}}\left(1,b\right)+{\mathbf{RNDM}}\left(2,b\right)>0$;
• the column identifiers associated with the random variables must be in the range $1$ to ncol, i.e., $1\le {\mathbf{RNDM}}\left(2+\mathit{i},b\right)\le {\mathbf{ncol}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{RNDM}}\left(1,b\right)$;
• ${\mathbf{RNDM}}\left(3+{N}_{{R}_{b}},b\right)\ge 0$;
• the column identifiers associated with the subject variables must be in the range $1$ to ncol, i.e., $1\le {\mathbf{RNDM}}\left(3+{N}_{{R}_{b}}+\mathit{i},b\right)\le {\mathbf{ncol}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{RNDM}}\left(3+{N}_{{R}_{b}},b\right)$.
13: $\mathbf{lrndm}$Integer Input
On entry: maximum number of entries in any column of RNDM.
Constraint: ${\mathbf{lrndm}}\ge \underset{b}{max}\phantom{\rule{0.25em}{0ex}}\left(3+{N}_{{R}_{b}}+{N}_{{S}_{b}}\right)$.
14: $\mathbf{nff}$Integer * Output
On exit: $p$, the number of fixed effects estimated, i.e., the number of columns in the design matrix $X$.
15: $\mathbf{nlsv}$Integer * Output
On exit: the number of levels for the overall subject variable (see Section 9.2 for a description of what this means). If there is no overall subject variable, ${\mathbf{nlsv}}=1$.
16: $\mathbf{nrf}$Integer * Output
On exit: the number of random effects estimated in each of the overall subject blocks. The number of columns in the design matrix $Z$ is given by $q={\mathbf{nrf}}×{\mathbf{nlsv}}$.
17: $\mathbf{rcomm}\left[{\mathbf{lrcomm}}\right]$double Communication Array
On exit: communication array as required by the analysis functions g02jdc and g02jec.
18: $\mathbf{lrcomm}$Integer Input
On entry: the dimension of the array rcomm.
Constraint: ${\mathbf{lrcomm}}\ge \left({\mathbf{nff}}×{\mathbf{nrf}}+{\mathbf{nrf}}×{\mathbf{nrf}}+{\mathbf{nrf}}\right)×{\mathbf{nlsv}}+{\mathbf{nff}}×{\mathbf{nff}}+{\mathbf{nff}}+2$.
19: $\mathbf{icomm}\left[{\mathbf{licomm}}\right]$Integer Communication Array
On exit: if ${\mathbf{licomm}}=2$, ${\mathbf{icomm}}\left[0\right]$ holds the minimum required value for licomm and ${\mathbf{icomm}}\left[1\right]$ holds the minimum required value for lrcomm, otherwise icomm is a communication array as required by the analysis functions g02jdc and g02jec.
20: $\mathbf{licomm}$Integer Input
On entry: the dimension of the array icomm.
Constraint: ${\mathbf{licomm}}=2$ or ${\mathbf{licomm}}\ge 34+{N}_{F}×\left(\text{MFL}+1\right)+{\mathbf{nrndm}}×\text{MNR}×\text{MRL}+\left(\text{LRNDM}+2\right)×{\mathbf{nrndm}}+{\mathbf{ncol}}+\text{LDID}×\text{LB,}$
where
• $\text{MNR}=\underset{b}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}\left({N}_{{R}_{b}}\right)$,
• $\text{MFL}=\underset{i}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}\left({\mathbf{levels}}\left[{\mathbf{fixed}}\left[2+i-1\right]-1\right]\right)$,
• $\text{MRL}=\underset{b,i}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}\left({\mathbf{levels}}\left[{\mathbf{RNDM}}\left(2+i,b\right)-1\right]\right)$,
• $\text{LDID}=\underset{b}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}{N}_{{S}_{b}}$,
• $\text{LB}={\mathbf{nff}}+{\mathbf{nrf}}×{\mathbf{nlsv}}$, and
• $\text{LRNDM}=\underset{b}{max}\phantom{\rule{0.25em}{0ex}}\left(3+{N}_{{R}_{b}}+{N}_{{S}_{b}}\right)$
21: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_INT
On entry, ${\mathbf{lfixed}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lfixed}}\ge ⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{licomm}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{licomm}}\ge ⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{lrcomm}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lrcomm}}\ge ⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{lrndm}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lrndm}}\ge ⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{ncol}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ncol}}\ge 0$.
On entry, ${\mathbf{nrndm}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{nrndm}}>0$.
NE_INT_2
On entry, ${\mathbf{pddat}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{pddat}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{pddat}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ncol}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{pddat}}\ge {\mathbf{ncol}}$.
NE_INT_ARRAY
On entry, index of fixed variable $j$ is less than $1$ or greater than ${\mathbf{ncol}}$: $j=⟨\mathit{\text{value}}⟩$, index $\text{}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ncol}}=⟨\mathit{\text{value}}⟩$.
On entry, index of random variable $j$ in random statement $i$ is less than $1$ or greater than ${\mathbf{ncol}}$: $i=⟨\mathit{\text{value}}⟩$, $j=⟨\mathit{\text{value}}⟩$, index $\text{}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{ncol}}=⟨\mathit{\text{value}}⟩$.
On entry, invalid value for fixed intercept flag: value $\text{}=⟨\mathit{\text{value}}⟩$.
On entry, invalid value for random intercept flag for random statement $i$: $i=⟨\mathit{\text{value}}⟩$, value $\text{}=⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{levels}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{levels}}\left[i-1\right]\ge 1$.
On entry, must be at least one parameter, or an intercept in each random statement $i$: $i=⟨\mathit{\text{value}}⟩$.
On entry, nesting variable $j$ in random statement $i$ has one level: $j=⟨\mathit{\text{value}}⟩$, $i=⟨\mathit{\text{value}}⟩$.
On entry, number of fixed parameters, $⟨\mathit{\text{value}}⟩$ is less than zero.
On entry, number of random parameters for random statement $i$ is less than $0$: $i=⟨\mathit{\text{value}}⟩$, number of parameters $\text{}=⟨\mathit{\text{value}}⟩$.
On entry, number of subject parameters for random statement $i$ is less than $0$: $i=⟨\mathit{\text{value}}⟩$, number of parameters $\text{}=⟨\mathit{\text{value}}⟩$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_REAL_ARRAY
On entry, no observations due to zero weights.
On entry, variable $j$ of observation $i$ is less than $1$ or greater than ${\mathbf{levels}}\left[j-1\right]$: $i=⟨\mathit{\text{value}}⟩$, $j=⟨\mathit{\text{value}}⟩$, value $=⟨\mathit{\text{value}}⟩$, ${\mathbf{levels}}\left[j-1\right]=⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{wt}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{wt}}\left[i-1\right]\ge 0.0$.
NE_TOO_MANY
On entry, more fixed factors than observations, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge ⟨\mathit{\text{value}}⟩$.

Not applicable.

## 8Parallelism and Performance

g02jcc is not threaded in any implementation.

### 9.1Construction of the fixed effects design matrix, $\mathbit{X}$

Let
• ${N}_{F}$ denote the number of fixed variables, that is ${\mathbf{fixed}}\left[0\right]={N}_{F}$;
• ${F}_{j}$ denote the $j$th fixed variable, that is the vector of values held in the $k$th column of DAT when ${\mathbf{fixed}}\left[2+j-1\right]=k$;
• ${F}_{ij}$ denote the $i$th element of ${F}_{j}$;
• $L\left({F}_{j}\right)$ denote the number of levels for ${F}_{j}$, that is $L\left({F}_{j}\right)={\mathbf{levels}}\left[{\mathbf{fixed}}\left[2+j-1\right]-1\right]$;
• ${D}_{v}\left({F}_{j}\right)$ denoted an indicator function that returns a vector of values whose $i$th element is $1$ if ${F}_{ij}=v$ and $0$ otherwise.
The design matrix for the fixed effects, $X$, is constructed as follows:
• set $k$ to zero and the flag $\text{done_first}$ to false;
• if a fixed intercept is included, that is ${\mathbf{fixed}}\left[1\right]=1$,
• set the first column of $X$ to a vector of $1$s;
• set $k=k+1$;
• set $\text{done_first}$ to true;
• loop over each fixed variable, so for each $j=1,2,\dots ,{N}_{F}$,
• if $L\left({F}_{j}\right)=1$,
• set the $k$th column of $X$ to be ${F}_{j}$;
• set $k=k+1$;
• else
• if $\text{done_first}$ is false then
• set the $L\left({F}_{j}\right)$ columns, $k$ to $k+L\left({F}_{j}\right)-1$, of $X$ to ${D}_{\mathit{v}}\left({F}_{j}\right)$, for $\mathit{v}=1,2,\dots ,L\left({F}_{j}\right)$;
• set $k=k+L\left({F}_{j}\right)$;
• set $\text{done_first}$ to true;
• else
• set the $L\left({F}_{j}\right)-1$ columns, $k$ to $k+L\left({F}_{j}\right)-2$, of $X$ to ${D}_{\mathit{v}}\left({F}_{j}\right)$, for $\mathit{v}=2,3,\dots ,L\left({F}_{j}\right)$;
• set $k=k+L\left({F}_{j}\right)-1$.
The number of columns in the design matrix, $X$, is, therefore, given by
 $p= 1+ ∑ j=1 N F (levels[fixed[2+j-1]-1]-1) .$
This quantity is returned in nff.
In summary, g02jcc converts all non-binary categorical variables (i.e., where $L\left({F}_{j}\right)>1$) to dummy variables. If a fixed intercept is included in the model then the first level of all such variables is dropped. If a fixed intercept is not included in the model then the first level of all such variables, other than the first, is dropped. The variables are added into the model in the order they are specified in fixed.

### 9.2Construction of random effects design matrix, $\mathbit{Z}$

Let
• ${N}_{{R}_{b}}$ denote the number of random variables in the $b$th random statement, that is ${N}_{{R}_{b}}={\mathbf{RNDM}}\left(1,b\right)$;
• ${R}_{jb}$ denote the $j$th random variable from the $b$th random statement, that is the vector of values held in the $k$th column of DAT when ${\mathbf{RNDM}}\left(2+j,b\right)=k$;
• ${R}_{ijb}$ denote the $i$th element of ${R}_{jb}$;
• $L\left({R}_{jb}\right)$ denote the number of levels for ${R}_{jb}$, that is $L\left({R}_{jb}\right)={\mathbf{levels}}\left[{\mathbf{RNDM}}\left(2+j,b\right)-1\right]$;
• ${D}_{v}\left({R}_{jb}\right)$ denoted an indicator function that returns a vector of values whose $i$th element is $1$ if ${R}_{ijb}=v$ and $0$ otherwise;
• ${N}_{{S}_{b}}$ denote the number of subject variables in the $b$th random statement, that is ${N}_{{S}_{b}}={\mathbf{RNDM}}\left(3+{N}_{{R}_{b}},b\right)$;
• ${S}_{jb}$ denote the $j$th subject variable from the $b$th random statement, that is the vector of values held in the $k$th column of DAT when ${\mathbf{RNDM}}\left(3+{N}_{{R}_{b}}+j,b\right)=k$;
• ${S}_{ijb}$ denote the $i$th element of ${S}_{jb}$;
• $L\left({S}_{jb}\right)$ denote the number of levels for ${S}_{jb}$, that is $L\left({S}_{jb}\right)={\mathbf{levels}}\left[{\mathbf{RNDM}}\left(3+{N}_{{R}_{b}}+j,b\right)-1\right]$;
• ${I}_{b}\left({s}_{1},{s}_{2},\dots ,{s}_{{N}_{{S}_{b}}}\right)$ denoted an indicator function that returns a vector of values whose $i$th element is $1$ if ${S}_{ijb}={s}_{j}$ for all $j=1,2,\dots ,{N}_{{S}_{b}}$ and $0$ otherwise.
The design matrix for the random effects, $Z$, is constructed as follows:
• set $k$ to zero;
• loop over each random statement, so for each $b=1,2,\dots ,{\mathbf{nrndm}}$,
• loop over each level of the last subject variable, so for each ${s}_{{N}_{{S}_{b}}}=1,2,\dots ,L\left({R}_{{N}_{{S}_{b}}b}\right)$,
• $⋮$
• loop over each level of the second subject variable, so for each ${s}_{2}=1,2,\dots ,L\left({R}_{2b}\right)$,
• loop over each level of the first subject variable, so for each ${s}_{1}=1,2,\dots ,L\left({R}_{1b}\right)$,
• if a random intercept is included, that is ${\mathbf{RNDM}}\left(2,b\right)=1$,
• set the $k$th column of $Z$ to ${I}_{b}\left({s}_{1},{s}_{2},\dots ,{s}_{{N}_{{S}_{b}}}\right)$;
• set $k=k+1$;
• loop over each random variable in the $b$th random statement, so for each $j=1,2,\dots ,{N}_{{R}_{b}}$,
• if $L\left({R}_{jb}\right)=1$,
• set the $k$th column of $Z$ to ${R}_{jb}×{I}_{b}\left({s}_{1},{s}_{2},\dots ,{s}_{{N}_{{S}_{b}}}\right)$ where $×$ indicates an element-wise multiplication between the two vectors, ${R}_{jb}$ and ${I}_{b}\left(\dots \right)$;
• set $k=k+1$;
• else
• set the $L\left({R}_{bj}\right)$ columns, $k$ to $k+L\left({R}_{bj}\right)$, of $Z$ to ${D}_{\mathit{v}}\left({R}_{jb}\right)×{I}_{b}\left({s}_{1},{s}_{2},\dots ,{s}_{{N}_{{S}_{b}}}\right)$, for $\mathit{v}=1,2,\dots ,L\left({R}_{jb}\right)$. As before, $×$ indicates an element-wise multiplication between the two vectors, ${D}_{v}\left(\dots \right)$ and ${I}_{b}\left(\dots \right)$;
• set $k=k+L\left({R}_{jb}\right)$.
In summary, each column of RNDM defines a block of consecutive columns in $Z$. g02jcc converts all non-binary categorical variables (i.e., where $L\left({R}_{jb}\right)$ or $L\left({S}_{jb}\right)>1$) to dummy variables. All random variables defined within a column of RNDM are nested within all subject variables defined in the same column of RNDM. In addition each of the subject variables are nested within each other, starting with the first (i.e., each of the ${R}_{jb},j=1,2,\dots ,{N}_{{R}_{b}}$ are nested within ${S}_{1b}$ which in turn is nested within ${S}_{2b}$, which in turn is nested within ${S}_{3b}$, etc.).
If the last subject variable in each column of RNDM are the same (i.e., ${S}_{{N}_{{S}_{1}}1}={S}_{{N}_{{S}_{2}}2}=\cdots ={S}_{{N}_{{S}_{b}}b}$) then all random effects in the model are nested within this variable. In such instances the last subject variable (${S}_{{N}_{{S}_{1}}1}$) is called the overall subject variable. The fact that all of the random effects in the model are nested within the overall subject variable means that ${Z}^{\mathrm{T}}Z$ is block diagonal in structure. This fact can be utilised to improve the efficiency of the underlying computation and reduce the amount of internal storage required. The number of levels in the overall subject variable is returned in ${\mathbf{nlsv}}=L\left({S}_{{N}_{{S}_{1}}1}\right)$.
If the last $k$ subject variables in each column of RNDM are the same, for $k>1$ then the overall subject variable is defined as the interaction of these $k$ variables and
 $nlsv= ∏ j=NS1-k+1 NS1 L(Sj1) .$
If there is no overall subject variable then ${\mathbf{nlsv}}=1$.
The number of columns in the design matrix $Z$ is given by $q={\mathbf{nrf}}×{\mathbf{nlsv}}$.

## 10Example

See Section 10 in g02jdc and g02jec.