NAG Library Routine Document

g02jcf  (mixeff_hier_init)

 Contents

    1  Purpose
    7  Accuracy
    10  Example

1
Purpose

g02jcf preprocesses a dataset prior to fitting a linear mixed effects regression model of the following form via either g02jdf or g02jef.

2
Specification

Fortran Interface
Subroutine g02jcf ( weight, n, ncol, dat, lddat, levels, y, wt, fixed, lfixed, nrndm, rndm, ldrndm, nff, nlsv, nrf, rcomm, lrcomm, icomm, licomm, ifail)
Integer, Intent (In):: n, ncol, lddat, levels(ncol), fixed(lfixed), lfixed, nrndm, rndm(ldrndm,nrndm), ldrndm, lrcomm, licomm
Integer, Intent (Inout):: ifail
Integer, Intent (Out):: nff, nlsv, nrf, icomm(licomm)
Real (Kind=nag_wp), Intent (In):: dat(lddat,ncol), y(n), wt(*)
Real (Kind=nag_wp), Intent (Out):: rcomm(lrcomm)
Character (1), Intent (In):: weight
C Header Interface
#include nagmk26.h
void  g02jcf_ ( const char *weight, const Integer *n, const Integer *ncol, const double dat[], const Integer *lddat, const Integer levels[], const double y[], const double wt[], const Integer fixed[], const Integer *lfixed, const Integer *nrndm, const Integer rndm[], const Integer *ldrndm, Integer *nff, Integer *nlsv, Integer *nrf, double rcomm[], const Integer *lrcomm, Integer icomm[], const Integer *licomm, Integer *ifail, const Charlen length_weight)

3
Description

g02jcf must be called prior to fitting a linear mixed effects regression model with either g02jdf or g02jef.
The model fitting routines g02jdf and g02jef 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 by p design matrix of fixed independent variables,
β is a vector of p unknown fixed effects,
Z is an n by q design matrix of random independent variables,
ν is a vector of length q of unknown random effects,
ε is a vector of length n of unknown random errors,
and ν and ε are Normally distributed with expectation zero and variance/covariance matrix defined by
Var ν ε = G 0 0 R  
where R= σ 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 Wc1/2X and Wc1/2Z respectively where Wc is a diagonal weight matrix.

4
References

None.

5
Arguments

1:     weight – Character(1)Input
On entry: indicates if weights are to be used.
weight='U'
No weights are used.
weight='W'
Case weights are used and must be supplied in array wt.
Constraint: weight='U' or 'W'.
2:     n – IntegerInput
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: n1.
3:     ncol – IntegerInput
On entry: the number of columns in the data matrix, dat.
Constraint: ncol0.
4:     datlddatncol – Real (Kind=nag_wp) arrayInput
On entry: a matrix of data, with datij holding the ith observation on the jth 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 levelsj1,1datijlevelsj.
5:     lddat – IntegerInput
On entry: the first dimension of the array dat as declared in the (sub)program from which g02jcf is called.
Constraint: lddatn.
6:     levelsncol – Integer arrayInput
On entry: levelsi contains the number of levels associated with the ith variable held in dat.
If the ith variable is continuous or binary (i.e., only takes the values zero or one) then levelsi must be set to 1. Otherwise the ith variable is assumed to take an integer value between 1 and levelsi, (i.e., the ith variable is discrete with levelsi levels).
Constraint: levelsi1, for i=1,2,,ncol.
7:     yn – Real (Kind=nag_wp) arrayInput
On entry: y, the vector of observations on the dependent variable.
8:     wt* – Real (Kind=nag_wp) arrayInput
Note: the dimension of the array wt must be at least n if weight='W'.
On entry: if weight='W', wt must contain the diagonal elements of the weight matrix Wc.
If wti=0.0, the ith observation is not included in the model and the effective number of observations is the number of observations with nonzero weights.
If weight='U', wt is not referenced and the effective number of observations is n.
Constraint: if weight='W', wti0.0, for i=1,2,,n.
9:     fixedlfixed – Integer arrayInput
On entry: defines the structure of the fixed effects design matrix, X.
fixed1
The number of variables, NF, to include as fixed effects (not including the intercept if present).
fixed2
The fixed intercept flag which must contain 1 if a fixed intercept is to be included and 0 otherwise.
fixed2+i
The column of dat holding the ith fixed variable, for i=1,2,,fixed1.
See Section 9.1 for more details on the construction of X.
Constraints:
  • fixed10;
  • fixed2=0​ or ​1;
  • 1fixed2+incol, for i=1,2,,fixed1.
10:   lfixed – IntegerInput
On entry: length of the vector fixed.
Constraint: lfixed2+fixed1.
11:   nrndm – IntegerInput
On entry: the second dimension of the array rndm as declared in the (sub)program from which g02jcf is called.
Constraint: nrndm>0.
12:   rndmldrndmnrndm – Integer arrayInput
On entry: rndmij defines the structure of the random effects design matrix, Z. The bth column of rndm defines a block of columns in the design matrix Z.
rndm1b
The number of variables, NRb, to include as random effects in the bth block (not including the random intercept if present).
rndm2b
The random intercept flag which must contain 1 if block b includes a random intercept and 0 otherwise.
rndm2+ib
The column of dat holding the ith random variable in the bth block, for i=1,2,,rndm1b.
rndm3+NRbb
The number of subject variables, NSb, for the bth block. The subject variables define the nesting structure for this block.
rndm3+NRb+ib
The column of dat holding the ith subject variable in the bth block, for i=1,2,,rndm3+NRbb.
See Section 9.2 for more details on the construction of Z.
Constraints:
  • rndm1b0;
  • rndm2b=0​ or ​1;
  • at least one random variable or random intercept must be specified in each block, i.e., rndm1b + rndm2b > 0 ;
  • the column identifiers associated with the random variables must be in the range 1 to ncol, i.e., 1 rndm2+ib ncol , for i=1,2,,rndm1b;
  • rndm3+NRbb 0 ;
  • the column identifiers associated with the subject variables must be in the range 1 to ncol, i.e., 1 rndm3+ N R b +i b ncol , for i=1,2,,rndm3+NRbb.
13:   ldrndm – IntegerInput
On entry: the first dimension of the array rndm as declared in the (sub)program from which g02jcf is called.
Constraint: ldrndm max b 3+NRb+NSb .
14:   nff – IntegerOutput
On exit: p, the number of fixed effects estimated, i.e., the number of columns in the design matrix X.
15:   nlsv – IntegerOutput
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, nlsv=1.
16:   nrf – IntegerOutput
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=nrf×nlsv.
17:   rcommlrcomm – Real (Kind=nag_wp) arrayCommunication Array
On exit: communication array as required by the analysis routines g02jdf and g02jef.
18:   lrcomm – IntegerInput
On entry: the dimension of the array rcomm as declared in the (sub)program from which g02jcf is called.
Constraint: lrcommnrf×nlsv+nff+nff×nlsv+nrf×nlsv+nff+2.
19:   icommlicomm – Integer arrayCommunication Array
On exit: if licomm=2, icomm1 holds the minimum required value for licomm and icomm2 holds the minimum required value for lrcomm, otherwise icomm is a communication array as required by the analysis routines g02jdf and g02jef.
20:   licomm – IntegerInput
On entry: the dimension of the array icomm as declared in the (sub)program from which g02jcf is called.
Constraint: licomm=2 or licomm34+ NF×MFL+1+ nrndm×MNR×MRL+LRNDM+2×nrndm+ ncol+LDID×LB,
where
  • MNR = maxb N R b ,
  • MFL=maxi levels fixed2+i ,
  • MRL=maxb,i levels rndm2+ib ,
  • LDID=maxb NSb ,
  • LB=nff+nrf×nlsv, and
  • LRNDM= max b 3+NRb+NSb  
21:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this argument, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6
Error Indicators and Warnings

If on entry ifail=0 or -1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
ifail=1
On entry, weight had an illegal value.
ifail=2
On entry, n=value.
Constraint: n1.
ifail=3
On entry, ncol=value.
Constraint: ncol0.
ifail=4
On entry, variable j of observation i is less than 1 or greater than levelsj: i=value, j=value, value =value, levelsj=value.
ifail=5
On entry, lddat=value and n=value.
Constraint: lddatn.
ifail=6
On entry, levelsvalue=value.
Constraint: levelsi1.
ifail=8
On entry, wtvalue=value.
Constraint: wti0.0.
ifail=9
On entry, number of fixed parameters, value is less than zero.
ifail=10
On entry, lfixed=value.
Constraint: lfixedvalue.
ifail=11
On entry, nrndm=value.
Constraint: nrndm>0.
ifail=12
On entry, number of random parameters for random statement i is less than 0: i=value, number of parameters =value.
ifail=13
On entry, ldrndm=value.
Constraint: ldrndmvalue.
ifail=18
On entry, lrcomm=value.
Constraint: lrcommvalue.
ifail=20
On entry, licomm=value.
Constraint: licommvalue.
ifail=102
On entry, more fixed factors than observations, n=value.
Constraint: nvalue.
ifail=108
On entry, no observations due to zero weights.
ifail=109
On entry, invalid value for fixed intercept flag: value =value.
ifail=112
On entry, invalid value for random intercept flag for random statement i: i=value, value =value.
ifail=209
On entry, index of fixed variable j is less than 1 or greater than ncol: j=value, index =value and ncol=value.
ifail=212
On entry, must be at least one parameter, or an intercept in each random statement i: i=value.
ifail=312
On entry, index of random variable j in random statement i is less than 1 or greater than ncol: i=value, j=value, index =value and ncol=value.
ifail=412
On entry, number of subject parameters for random statement i is less than 0: i=value, number of parameters =value.
ifail=512
On entry, nesting variable j in random statement i has one level: j=value, i=value.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7
Accuracy

Not applicable.

8
Parallelism and Performance

g02jcf is not threaded in any implementation.

9
Further Comments

9.1
Construction of the fixed effects design matrix, X

Let
The design matrix for the fixed effects, X, is constructed as follows:
The number of columns in the design matrix, X, is therefore given by
p= 1+ j=1 N F levels fixed 2+j -1 .  
This quantity is returned in nff.
In summary, g02jcf converts all non-binary categorical variables (i.e., where LFj>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.2
Construction of random effects design matrix, Z

Let
The design matrix for the random effects, Z, is constructed as follows:
In summary, each column of rndm defines a block of consecutive columns in Z. g02jcf converts all non-binary categorical variables (i.e., where LRjb or LSjb>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 Rjb,j=1,2,,NRb are nested within S1b which in turn is nested within S2b, which in turn is nested within S3b, etc.).
If the last subject variable in each column of rndm are the same (i.e., SNS11 = SNS22 = = SNSbb ) then all random effects in the model are nested within this variable. In such instances the last subject variable ( SNS11 ) 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 ZTZ 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 nlsv=LSNS11.
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 LSj1 .  
If there is no overall subject variable then nlsv=1.
The number of columns in the design matrix Z is given by q=nrf×nlsv.

9.3
The rndm argument

To illustrate some additional points about the rndm argument, we assume that we have a dataset with three discrete variables, V1, V2 and V3, with 2,4 and 3 levels respectively, and that V1 is in the first column of dat, V2 in the second and V3 the third. Also assume that we wish to fit a model containing V1 along with V2 nested within V3, as random effects. In order to do this the rndm matrix requires two columns:
rndm= 1 1 0 0 1 2 0 1 0 3  
The first column, 1,0,1,0,0, indicates one random variable (rndm11=1), no intercept (rndm21=0), the random variable is in the first column of dat (rndm31=1), there are no subject variables; as no nesting is required for V1 (rndm41=0). The last element in this column is ignored.
The second column, 1,0,2,1,3, indicates one random variable (rndm12=1), no intercept (rndm22=0), the random variable is in the second column of dat rndm32=2, there is one subject variable (rndm42=1), and the subject variable is in the third column of dat rndm52=3.
The corresponding Z matrix would have 14 columns, with 2 coming from V1 and 12 (4×3) from V2 nested within V3. The, symmetric, ZTZ matrix has the form
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - 0 0 0 0 - - - - 0 0 0 0 - - 0 0 0 0 - - - - 0 0 0 0 - - 0 0 0 0 - - - - 0 0 0 0 - - 0 0 0 0 - - - - 0 0 0 0 - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - -  
where 0 indicates a structural zero, i.e., it always takes the value 0, irrespective of the data, and - a value that is not a structural zero. The first two rows and columns of ZTZ correspond to V1. The block diagonal matrix in the 12 rows and columns in the bottom right correspond to V2 nested within V3. With the 4×4 blocks corresponding to the levels of V2. There are three blocks as the subject variable (V3) has three levels.
The model fitting routines, g02jdf and g02jef, use the sweep algorithm to calculate the log-likelihood function for a given set of variance components. This algorithm consists of moving down the diagonal elements (called pivots) of a matrix which is similar in structure to ZTZ, and updating each element in that matrix. When using the k diagonal element of a matrix A, an element a i j ,ik,jk , is adjusted by an amount equal to a i k a i j / a k k . This process can be referred to as sweeping on the kth pivot. As there are no structural zeros in the first row or column of the above ZTZ, sweeping on the first pivot of ZTZ would alter each element of the matrix and therefore destroy the structural zeros, i.e., we could no longer guarantee they would be zero.
Reordering the rndm matrix to
rndm= 1 1 0 0 2 1 1 0 3 0  
i.e., the swapping the two columns, results in a ZTZ matrix of the form
- - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - 0 0 0 0 - - - - 0 0 0 0 - - 0 0 0 0 - - - - 0 0 0 0 - - 0 0 0 0 - - - - 0 0 0 0 - - 0 0 0 0 - - - - 0 0 0 0 - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - 0 0 0 0 0 0 0 0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
This matrix is identical to the previous one, except the first two rows and columns have become the last two rows and columns. Sweeping a matrix, A=aij, of this form on the first pivot will only affect those elements aij, where ai10​ and ​a1j0, which is only the 13th and 14th row and columns, and the top left hand block of 4 rows and columns. The block diagonal nature of the first 12 rows and columns therefore greatly reduces the amount of work the algorithm needs to perform.
g02jcf constructs the ZTZ as specified by the rndm matrix, and does not attempt to reorder it to improve performance. Therefore for best performance some thought is required on what ordering to use. In general it is more efficient to structure rndm in such a way that the first row relates to the deepest level of nesting, the second to the next level, etc..

10
Example

See Section 10 in g02jdf and g02jef.
© The Numerical Algorithms Group Ltd, Oxford, UK. 2017