G02JCF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G02JCF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy
    9  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

SUBROUTINE G02JCF ( WEIGHT, N, NCOL, DAT, LDDAT, LEVELS, Y, WT, FIXED, LFIXED, NRNDM, RNDM, LDRNDM, NFF, NLSV, NRF, RCOMM, LRCOMM, ICOMM, LICOMM, IFAIL)
INTEGER  N, NCOL, LDDAT, LEVELS(NCOL), FIXED(LFIXED), LFIXED, NRNDM, RNDM(LDRNDM,NRNDM), LDRNDM, NFF, NLSV, NRF, LRCOMM, ICOMM(LICOMM), LICOMM, IFAIL
REAL (KIND=nag_wp)  DAT(LDDAT,NCOL), Y(N), WT(*), RCOMM(LRCOMM)
CHARACTER(1)  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  Parameters

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:     DAT(LDDAT,NCOL) – 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:     LEVELS(NCOL) – 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:     Y(N) – 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:     FIXED(LFIXED) – 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 8.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:   RNDM(LDRNDM,NRNDM) – 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 8.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 8.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:   RCOMM(LRCOMM) – 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:   ICOMM(LICOMM) – 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 parameter you should refer to Section 3.3 in the Essential Introduction 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 parameter, 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'W' or 'U'.
IFAIL=2
On entry, N<1.
IFAIL=3
On entry, NCOL<0.
IFAIL=4
On entry, at least one data point for variable j has data taking a value less than one or greater than LEVELSj.
IFAIL=5
On entry, LDDAT<N.
IFAIL=6
On entry, LEVELSi is less than one for at least one i.
IFAIL=8
On entry, there is at least one negative weight.
IFAIL=9
On entry, number of fixed parameters is less than zero.
IFAIL=10
On entry, LFIXED is too small.
IFAIL=11
On entry, NRNDM<1.
IFAIL=12
On entry, number of random parameters for at least one random statement is less than zero.
IFAIL=13
On entry, LDRNDM is too small.
IFAIL=18
On entry, LRCOMM is too small.
IFAIL=20
On entry, LICOMM is too small.
IFAIL=102
On entry, more fixed factors than observations.
IFAIL=108
On entry, no observations due to zero weights.
IFAIL=109
On entry, invalid value for fixed intercept flag.
IFAIL=112
On entry, invalid value for random intercept flag in at least one random statement.
IFAIL=209
On entry, the identifier associated with at least one fixed variable, as supplied in FIXED is less than one or greater than NCOL.
IFAIL=212
On entry, must be at least one parameter, or an intercept in each random statement.
IFAIL=312
On entry, the identifier associated with random variable j in random statement i, is less than one or greater than NCOL.
IFAIL=412
On entry, number of subject parameters for random statement i<0.
IFAIL=512
On entry, nesting variable j in random statement i has less than two levels.

7  Accuracy

Not applicable.

8  Further Comments

8.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.

8.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.

8.3  The RNDM parameter

To illustrate some additional points about the RNDM parameter, 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..

9  Example

See Section 9 in G02JDF and G02JEF.

G02JCF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012