nag_ml_hier_mixed_regsn (g02jec) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_ml_hier_mixed_regsn (g02jec)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_ml_hier_mixed_regsn (g02jec) fits a multi-level linear mixed effects regression model using maximum likelihood (ML). Prior to calling nag_ml_hier_mixed_regsn (g02jec) the initialization function nag_hier_mixed_init (g02jcc) must be called.

2  Specification

#include <nag.h>
#include <nagg02.h>
void  nag_ml_hier_mixed_regsn (Integer lvpr, const Integer vpr[], Integer nvpr, double gamma[], Integer *effn, Integer *rnkx, Integer *ncov, double *lnlike, Integer lb, Integer id[], Integer pdid, double b[], double se[], double czz[], Integer pdczz, double cxx[], Integer pdcxx, double cxz[], Integer pdcxz, const double rcomm[], const Integer icomm[], const Integer iopt[], Integer liopt, const double ropt[], Integer lropt, NagError *fail)

3  Description

nag_ml_hier_mixed_regsn (g02jec) 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,
β is a vector of length p of unknown fixed effects,
Z is a known n by q design matrix for the random independent variables,
ν is a vector of length q of unknown random effects,
and ε is a vector of length n of unknown random errors.
Both ν  and ε  are assumed to have a Gaussian distribution 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. It is assumed that the random variables, Z , can be subdivided into g q  groups with each group being identically distributed with expectation zero and variance σi2 . The diagonal elements of matrix G  therefore take one of the values σi2 : i=1,2,,g , depending on which group the associated random variable belongs to.
The model therefore contains three sets of unknowns: the fixed effects β , the random effects ν  and a vector of g+1  variance components γ , where γ = σ12,σ22,, σ g-1 2 ,σg2,σR2 . Rather than working directly with γ , nag_ml_hier_mixed_regsn (g02jec) uses an iterative process to estimate γ* = σ12 / σR2 , σ22 / σR2 ,, σg-12 / σR2 , σg2 / σR2 ,1 . Due to the iterative nature of the estimation a set of initial values, γ0 , for γ*  is required. nag_ml_hier_mixed_regsn (g02jec) 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_ml_hier_mixed_regsn (g02jec) fits the model by maximizing the log-likelihood function:
-2 l R = log V + n log rT V-1 r + log 2 π / n
where
V = ZG ZT + R,   r=y-Xb   and   b = XT V-1 X -1 XT V-1 y .
Once the final estimates for γ *  have been obtained, the value of σR2  is given by
σR2 = rT V-1 r / n - p .
Case weights, Wc , can be incorporated into the model by replacing XTX  and ZTZ  with XTWcX  and ZTWcZ  respectively, for a diagonal weight matrix Wc .
The log-likelihood, lR, is calculated using the sweep algorithm detailed in Wolfinger et al. (1994).

4  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

5  Arguments

Note: prior to calling nag_ml_hier_mixed_regsn (g02jec) the initialization function nag_hier_mixed_init (g02jcc) must be called, therefore this documention should be read in conjunction with the document for nag_hier_mixed_init (g02jcc).
In particular some argument names and conventions described in that document are also relevant here, but their definition has not been repeated. Specifically, RNDM, wt, n, nff, nrf, nlsv, levels, fixed, DAT, licomm and lrcomm should be interpreted identically in both functions.
1:     lvprIntegerInput
On entry: the sum of the number of random parameters and the random intercept flags specified in the call to nag_hier_mixed_init (g02jcc).
Constraint: lvpr=iRNDM1,i+RNDM2,i.
2:     vpr[lvpr]const IntegerInput
On entry: a vector of flags indicating the mapping between the random variables specified in rndm and the variance components, σi2. See Section 8 for more details.
Constraint: 1vpr[i-1]nvpr, for i=1,2,,lvpr.
3:     nvprIntegerInput
On entry: g, the number of variance components being estimated (excluding the overall variance, σR2).
Constraint: 1nvpr lvpr .
4:     gamma[nvpr+1]doubleInput/Output
On entry: holds the initial values of the variance components, γ0 , with gamma[i-1] the initial value for σi2/σR2, for i=1,2,,nvpr.
If gamma[0]=-1.0, the remaining elements of gamma are ignored and the initial values for the variance components are estimated from the data using MIVQUE0.
On exit: gamma[i-1], for i=1,2,,nvpr, holds the final estimate of σi2 and gamma[nvpr] holds the final estimate for σR2.
Constraint: gamma[0]=-1.0 ​ or ​ gamma[i-1]0.0, for i=1,2,,g.
5:     effnInteger *Output
On exit: effective number of observations. If there are no weights (i.e., wt is NULL), or all weights are nonzero, then effn=n.
6:     rnkxInteger *Output
On exit: the rank of the design matrix, X, for the fixed effects.
7:     ncovInteger *Output
On exit: number of variance components not estimated to be zero. If none of the variance components are estimated to be zero, then ncov=nvpr.
8:     lnlikedouble *Output
On exit: - 2 lR γ^  where lR  is the log of the maximum likelihood calculated at γ^ , the estimated variance components returned in gamma.
9:     lbIntegerInput
On entry: the dimension of the arrays b and se.
Constraint: lbnff+nrf×nlsv.
10:   id[pdid×lb]IntegerOutput
Note: where IDi,j appears in this document, it refers to the array element id[j-1×pdid+i-1].
On exit: an array describing the parameter estimates returned in b. The first nlsv×nrf columns of ID describe the parameter estimates for the random effects and the last nff columns the parameter estimates for the fixed effects.
A print function for decoding the parameter estimates given in b using information from id is supplied with the example program for this function.
For fixed effects:
  • for l=nrf×nlsv+1 ,, nrf×nlsv+nff  
    • if b[l-1] contains the parameter estimate for the intercept then
      ID1,l = ID2,l = ID3,l = 0 ;
    • if b[l-1] contains the parameter estimate for the ith level of the jth fixed variable, that is the vector of values held in the kth column of DAT when fixed[j+1]=k then
      ID1,l=0,  ID2,l=j,  ID3,l=i;
    • if the jth variable is continuous or binary, that is levels[fixed[j+1]-1]=1, then ID3,l=0;
    • any remaining rows of the lth column of ID are set to 0.
For random effects:
  • let
    • NRb denote the number of random variables in the bth random statement, that is NRb=RNDM1,b;
    • Rjb denote the jth random variable from the bth random statement, that is the vector of values held in the kth column of DAT when RNDM2+j,b=k;
    • NSb denote the number of subject variables in the bth random statement, that is NSb=RNDM3+NRb,b;
    • Sjb denote the jth subject variable from the bth random statement, that is the vector of values held in the kth column of DAT when RNDM3+NRb+j,b=k;
    • LSjb denote the number of levels for Sjb, that is LSjb=levels[RNDM3+NRb+j,b-1];
  • then
    • for l=1,2, nrf×nlsv, if b[l-1] contains the parameter estimate for the ith level of Rjb when Skb=sk, for k=1,2,,NSb and 1skLSjb, i.e., sk is a valid value for the kth subject variable, then
      ID1,l=b,  ID2,l=j,  ID3,l=i,  ID3+k,l=sk, ​k=1,2,,NSb;
    • if the parameter being estimated is for the intercept then ID2,l=ID3,l=0;
    • if the jth variable is continuous, or binary, that is LSjb=1, then ID3,l=0;
    • the remaining rows of the lth column of ID are set to 0.
In some situations, certain combinations of variables are never observed. In such circumstances all elements of the lth row of ID are set to -999.
11:   pdidIntegerInput
On entry: the stride separating matrix row elements in the array id.
Constraint: pdid3+maxj RNDM 3+ RNDM 1,j ,j , i.e., 3+ maximum number of subject variables (see nag_hier_mixed_init (g02jcc)).
12:   b[lb]doubleOutput
On exit: the parameter estimates, with the first nrf×nlsv elements of b containing the parameter estimates for the random effects, ν, and the remaining nff elements containing the parameter estimates for the fixed effects, β. The order of these estimates are described by the id argument.
13:   se[lb]doubleOutput
On exit: the standard errors of the parameter estimates given in b.
14:   czz[dim]doubleOutput
Note: the dimension, dim, of the array czz must be at least max1,pdczz×nrf×nlsv.
Let CZZi,j refer to the array element czz[j-1×pdczz+i-1].
On exit: if nlsv=1, then CZZ holds the lower triangular portion of the matrix 1/ σ 2 ZT R^-1 Z + G^-1 , where R^ and G^ are the estimates of R and G respectively. If nlsv>1 then CZZ holds this matrix in compressed form, with the first nrf columns holding the part of the matrix corresponding to the first level of the overall subject variable, the next nrf columns the part corresponding to the second level of the overall subject variable etc.
15:   pdczzIntegerInput
On entry: the stride separating matrix row elements in the array czz.
Constraint: pdczznrf.
16:   cxx[dim]doubleOutput
Note: the dimension, dim, of the array cxx must be at least pdcxx×nff.
Let CXXi,j refer to the array element cxx[j-1×pdcxx+i-1].
On exit: CXX holds the lower triangular portion of the matrix 1/ σ2 XT V^-1 X , where V^ is the estimated value of V.
17:   pdcxxIntegerInput
On entry: the stride separating matrix row elements in the array cxx.
Constraint: pdcxxnff.
18:   cxz[dim]doubleOutput
Note: the dimension, dim, of the array cxz must be at least max1,pdcxz×nlsv×nrf.
Let CXZi,j refer to the array element cxz[j-1×pdcxz+i-1].
On exit: if nlsv=1, then CXZ holds the matrix 1/σ2 XT V^-1 Z G^ , where V^ and G^ are the estimates of V and G respectively. If nlsv>1 then CXZ holds this matrix in compressed form, with the first nrf columns holding the part of the matrix corresponding to the first level of the overall subject variable, the next nrf columns the part corresponding to the second level of the overall subject variable etc.
19:   pdcxzIntegerInput
On entry: the stride separating matrix row elements in the array cxz.
Constraint: pdcxznff.
20:   rcomm[dim]const doubleCommunication Array
Note: the dimension, dim, of the array rcomm must be at least lrcomm.
On entry: communication array initialized by a call to nag_hier_mixed_init (g02jcc).
21:   icomm[dim]const IntegerCommunication Array
Note: the dimension, dim, of the array icomm must be at least licomm.
On entry: communication array initialized by a call to nag_hier_mixed_init (g02jcc).
22:   iopt[liopt]const IntegerInput
On entry: optional arguments passed to the optimization function.
By default nag_ml_hier_mixed_regsn (g02jec) fits the specified model using a modified Newton optimization algorithm as implemented in the NAG Fortran Library routine E04LBF. In some cases, where the calculation of the derivatives is computationally expensive it may be more efficient to use a sequential QP algorithm. The sequential QP algorithm as implemented in the NAG Fortran Library routine E04UCF can be chosen by setting iopt[4]=1. If liopt<4 or iopt[4]1 then E04LBF will be used.
Different optional arguments are available depending on the optimization function used. In all cases, using a value of -1 will cause the default value to be used. In addition only the first liopt values of iopt are used, so for example, if only the first element of iopt needs changing and default values for all other optional arguments are sufficient liopt can be set to 1.
NAG Fortran Library routine E04LBF is being used


i


Description
Equivalent
E04LBF
argument


Default Value
0 Number of iterations MAXCAL 1000
1 Unit number for monitoring information. See nag_open_file (x04acc) for details on how to assign a file to a unit number. n/a Output sent to stdout
2 Print optional arguments (1= print) n/a -1 (no printing performed)
3 Frequency that monitoring information is printed IPRINT -1
4 Optimizer used n/a n/a
If requested, monitoring information is displayed in a similar format to that given by E04LBF.
NAG Fortran Library routine E04UCF is being used


i


Description
Equivalent
E04UCF
argument


Default Value
0 Number of iterations Major Iteration Limit max50,3×nvpr
1 Unit number for monitoring information. See nag_open_file (x04acc) for details on how to assign a file to a unit number. n/a Output sent to stdout
2 Print optional arguments (1= print, otherwise no print) List/NoList -1 (no printing performed)
3 Frequency that monitoring information is printed Major Print Level 0
4 Optimizer used n/a n/a
5 Number of minor iterations Minor Iteration Limit max50,3×nvpr
6 Frequency that additional monitoring information is printed Minor Print Level 0
23:   lioptIntegerInput
On entry: length of the options array iopt. If liopt0 then iopt is not referenced and default values are used for all optional arguments.
24:   ropt[lropt]const doubleInput
On entry: optional arguments passed to the optimization function.
Different optional arguments are available depending on the optimization function used. In all cases, using a value of -1.0 will cause the default value to be used. In addition only the first lropt values of ropt are used, so for example, if only the first element of ropt needs changing and default values for all other optional arguments are sufficient lropt can be set to 1.
NAG Fortran Library routine E04LBF is being used


i


Description
Equivalent
E04LBF
argument


Default Value
0 Sweep tolerance n/a maxeps,eps×maxizzii
1 Accuracy of linear minimizations ETA 0.9
2 Accuracy to which solution is required XTOL 0.0
3 Initial distance from solution STEPMX 100000.0
NAG Fortran Library routine E04UCF is being used


i


Description
Equivalent
E04UCF
argument


Default Value
0 Sweep tolerance n/a maxeps,eps×maxizzii
1 Lower bound for γ* n/a eps/100
2 Upper bound for γ* n/a 1020
3 Line search tolerance Line Search Tolerance 0.9
4 Optimality tolerance Optimality Tolerance eps0.72
where eps is the machine precision returned by nag_machine_precision (X02AJC) and zzii denotes the i diagonal element of ZTZ.
25:   lroptIntegerInput
On entry: length of the options array ropt. If lropt0 then ropt is not referenced and default values are used for all optional arguments.
26:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_BAD_PARAM
On entry, argument value had an illegal value.
NE_INT
On entry, lb=value.
Constraint: lbvalue.
On entry, lvpr=value.
Constraint: lvprvalue.
On entry, nvpr=value.
Constraint: 1nvprvalue.
On entry, pdcxx=value.
Constraint: pdcxxvalue.
On entry, pdcxz= value.
Constraint: pdcxzvalue.
On entry, pdczz=value.
Constraint: pdczzvalue.
On entry, pdid=value.
Constraint: pdidvalue.
NE_INT_ARRAY
On entry, at least one value of i, for i=1,2,,nvpr, does not appear in vpr.
On entry, icomm has not been initialized correctly.
On entry, vpr[value]=value and nvpr=value.
Constraint: 1vpr[i-1]nvpr.
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.
NE_NEG_ELEMENT
At least one negative estimate for gamma was obtained. All negative estimates have been set to zero.
NE_REAL_ARRAY
On entry, gamma[value]=value.
Constraint: gamma[i-1]0.
NW_KT_CONDITIONS
Current point cannot be improved upon.
NW_NOT_CONVERGED
Optimal solution found, but requested accuracy not achieved.
NW_TOO_MANY_ITER
Too many major iterations.

7  Accuracy

Not applicable.

8  Further Comments

The argument vpr gives the mapping between the random variables and the variance components. In most cases vpr[i-1]=i, for i=1,2,,iRNDM1,i+RNDM2,i. However, in some cases it might be necessary to associate more than one random variable with a single variance component, for example, when the columns of DAT hold dummy variables.
Consider a dataset with three variables:
DAT= 113.6 214.5 311.1 128.3 227.2 326.1
where the first column corresponds to a categorical variable with three levels, the next to a categorical variable with two levels and the last column to a continuous variable. So in a call to nag_hier_mixed_init (g02jcc)
levels=321
also assume a model with no fixed effects, no random intercept, no nesting and all three variables being included as random effects, then
fixed=00; RNDM=30123T.
Each of the three columns in DAT therefore correspond to a single variable and hence there are three variance components, one for each random variable included in the model, so
vpr=123.
This is the recommended way of supplying the data to nag_ml_hier_mixed_regsn (g02jec), however it is possible to reformat the above dataset by replacing each of the categorical variables with a series of dummy variables, one for each level. The dataset then becomes
DAT= 100103.6 010104.5 001101.1 100018.3 010017.2 001016.1
where each column only has one level
levels= 111111 .
Again a model with no fixed effects, no random intercept, no nesting and all variables being included as random effects is required, so
fixed=00 ; RNDM= 60123456T .
With the data entered in this manner, the first three columns of DAT correspond to a single variable (the first column of the original dataset) as do the next two columns (the second column of the original dataset). Therefore vpr must reflect this
vpr= 111223 .
In most situations it is more efficient to supply the data to nag_hier_mixed_init (g02jcc) in terms of categorical variables rather than transform them into dummy variables.

9  Example

This example fits a random effects model with three levels of nesting to a simulated dataset with 90 observations and 12 variables.

9.1  Program Text

Program Text (g02jece.c)

9.2  Program Data

Program Data (g02jece.d)

9.3  Program Results

Program Results (g02jece.r)


nag_ml_hier_mixed_regsn (g02jec) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

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