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

NAG Library Routine Document

G02GCF

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

1  Purpose

G02GCF fits a generalized linear model with Poisson errors.

2  Specification

SUBROUTINE G02GCF ( LINK, MEAN, OFFSET, WEIGHT, N, X, LDX, M, ISX, IP, Y, WT, A, DEV, IDF, B, IRANK, SE, COV, V, LDV, TOL, MAXIT, IPRINT, EPS, WK, IFAIL)
INTEGER  N, LDX, M, ISX(M), IP, IDF, IRANK, LDV, MAXIT, IPRINT, IFAIL
REAL (KIND=nag_wp)  X(LDX,M), Y(N), WT(*), A, DEV, B(IP), SE(IP), COV(IP*(IP+1)/2), V(LDV,IP+7), TOL, EPS, WK((IP*IP+3*IP+22)/2)
CHARACTER(1)  LINK, MEAN, OFFSET, WEIGHT

3  Description

A generalized linear model with Poisson errors consists of the following elements:
(a) a set of n observations, yi, from a Poisson distribution:
μye-μ y! .  
(b) X, a set of p independent variables for each observation, x1,x2,,xp.
(c) a linear model:
η=βjxj.  
(d) a link between the linear predictor, η, and the mean of the distribution, μ, η=gμ. The possible link functions are:
(i) exponent link: η=μa, for a constant a,
(ii) identity link: η=μ,
(iii) log link: η=logμ,
(iv) square root link: η=μ,
(v) reciprocal link: η=1μ .
(e) a measure of fit, the deviance:
i=1ndevyi,μ^i=i=1n2 yilogyiμ^i-yi-μ^i .  
The linear parameters are estimated by iterative weighted least squares. An adjusted dependent variable, z, is formed:
z=η+y-μdη dμ  
and a working weight, w,
w= τddη dμ 2,  
where τ=μ.
At each iteration an approximation to the estimate of β, β^, is found by the weighted least squares regression of z on X with weights w.
G02GCF finds a QR decomposition of w1/2X, i.e., w1/2X=QR where R is a p by p triangular matrix and Q is an n by p column orthogonal matrix.
If R is of full rank, then β^ is the solution to:
Rβ^=QTw1/2z.  
If R is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of R.
R=Q* D 0 0 0 PT,  
where D is a k by k diagonal matrix with nonzero diagonal elements, k being the rank of R and w1/2X.
This gives the solution
β^=P1D-1 Q* 0 0 I QTw1/2z,  
P1 being the first k columns of P, i.e., P=P1P0.
The iterations are continued until there is only a small change in the deviance.
The initial values for the algorithm are obtained by taking
η^=gy.  
The fit of the model can be assessed by examining and testing the deviance, in particular by comparing the difference in deviance between nested models, i.e., when one model is a sub-model of the other. The difference in deviance between two nested models has, asymptotically, a χ2-distribution with degrees of freedom given by the difference in the degrees of freedom associated with the two deviances.
The parameters estimates, β^, are asymptotically Normally distributed with variance-covariance matrix
The residuals and influence statistics can also be examined.
The estimated linear predictor η^=Xβ^, can be written as Hw1/2z for an n by n matrix H. The ith diagonal elements of H, hi, give a measure of the influence of the ith values of the independent variables on the fitted regression model. These are known as leverages.
The fitted values are given by μ^=g-1η^.
G02GCF also computes the deviance residuals, r:
ri=signyi-μ^idevyi,μ^i.  
An option allows prior weights to be used with the model.
In many linear regression models the first term is taken as a mean term or an intercept, i.e., xi,1=1, for i=1,2,,n. This is provided as an option.
Often only some of the possible independent variables are included in a model; the facility to select variables to be included in the model is provided.
If part of the linear predictor can be represented by a variables with a known coefficient then this can be included in the model by using an offset, o:
η=o+βjxj.  
If the model is not of full rank the solution given will be only one of the possible solutions. Other estimates may be obtained by applying constraints to the parameters. These solutions can be obtained by using G02GKF after using G02GCF. Only certain linear combinations of the parameters will have unique estimates, these are known as estimable functions, these can be estimated and tested using G02GNF.
Details of the SVD are made available in the form of the matrix P*:
P*= D-1 P1T P0T .  
The generalized linear model with Poisson errors can be used to model contingency table data; see Cook and Weisberg (1982) and McCullagh and Nelder (1983).

4  References

Cook R D and Weisberg S (1982) Residuals and Influence in Regression Chapman and Hall
McCullagh P and Nelder J A (1983) Generalized Linear Models Chapman and Hall
Plackett R L (1974) The Analysis of Categorical Data Griffin

5  Parameters

On entry: indicates which link function is to be used.
LINK='E'
An exponent link is used.
LINK='I'
An identity link is used.
LINK='L'
A log link is used;
LINK='S'
A square root link is used.
LINK='R'
A reciprocal link is used.
Constraint: LINK='E', 'I', 'L', 'S' or 'R'.
2:     MEAN – CHARACTER(1)Input
On entry: indicates if a mean term is to be included.
MEAN='M'
A mean term, intercept, will be included in the model.
MEAN='Z'
The model will pass through the origin, zero-point.
Constraint: MEAN='M' or 'Z'.
3:     OFFSET – CHARACTER(1)Input
On entry: indicates if an offset is required.
OFFSET='Y'
An offset is required and the offsets must be supplied in the seventh column of V.
OFFSET='N'
No offset is required.
Constraint: OFFSET='N' or 'Y'.
4:     WEIGHT – CHARACTER(1)Input
On entry: indicates if prior weights are to be used.
WEIGHT='U'
No prior weights are used.
WEIGHT='W'
Prior weights are used and weights must be supplied in WT.
Constraint: WEIGHT='U' or 'W'.
5:     N – INTEGERInput
On entry: n, the number of observations.
Constraint: N2.
6:     XLDXM – REAL (KIND=nag_wp) arrayInput
On entry: the matrix of all possible independent variables. Xij must contain the ijth element of X, for i=1,2,,n and j=1,2,,M.
7:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G02GCF is called.
Constraint: LDXN.
8:     M – INTEGERInput
On entry: m, the total number of independent variables.
Constraint: M1.
9:     ISXM – INTEGER arrayInput
On entry: indicates which independent variables are to be included in the model.
ISXj>0
The variable contained in the jth column of X is included in the regression model.
Constraints:
  • ISXj0, for j=1,2,,M;
  • if MEAN='M', exactly IP-1 values of ISX must be >0;
  • if MEAN='Z', exactly IP values of ISX must be >0.
10:   IP – INTEGERInput
On entry: the number of independent variables in the model, including the mean or intercept if present.
Constraint: IP>0.
11:   YN – REAL (KIND=nag_wp) arrayInput
On entry: y, observations on the dependent variable.
Constraint: Yi0.0, for i=1,2,,n.
12:   WT* – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least N if WEIGHT='W', and at least 1 otherwise.
On entry: if WEIGHT='W' >, WT must contain the weights to be used in the weighted regression.
If WTi=0.0, the ith observation is not included in the model, in which case 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.
13:   A – REAL (KIND=nag_wp)Input
On entry: if LINK='E', A must contain the power of the exponential.
If LINK'E', A is not referenced.
Constraint: if A0.0, LINK='E'.
14:   DEV – REAL (KIND=nag_wp)Output
On exit: the deviance for the fitted model.
15:   IDF – INTEGEROutput
On exit: the degrees of freedom asociated with the deviance for the fitted model.
16:   BIP – REAL (KIND=nag_wp) arrayOutput
On exit: the estimates of the parameters of the generalized linear model, β^.
If MEAN='M', the first element of B will contain the estimate of the mean parameter and Bi+1 will contain the coefficient of the variable contained in column j of X, where ISXj is the ith positive value in the array ISX.
If MEAN='Z', Bi will contain the coefficient of the variable contained in column j of X, where ISXj is the ith positive value in the array ISX.
17:   IRANK – INTEGEROutput
On exit: the rank of the independent variables.
If the model is of full rank, IRANK=IP.
If the model is not of full rank, IRANK is an estimate of the rank of the independent variables. IRANK is calculated as the number of singular values greater that EPS×(largest singular value). It is possible for the SVD to be carried out but for IRANK to be returned as IP.
18:   SEIP – REAL (KIND=nag_wp) arrayOutput
On exit: the standard errors of the linear parameters.
SEi contains the standard error of the parameter estimate in Bi, for i=1,2,,IP.
19:   COVIP×IP+1/2 – REAL (KIND=nag_wp) arrayOutput
On exit: the upper triangular part of the variance-covariance matrix of the IP parameter estimates given in B. They are stored packed by column, i.e., the covariance between the parameter estimate given in Bi and the parameter estimate given in Bj, ji, is stored in COVj×j-1/2+i.
20:   VLDVIP+7 – REAL (KIND=nag_wp) arrayInput/Output
On entry: if OFFSET='N', V need not be set.
If OFFSET='Y', Vi7, for i=1,2,,n must contain the offset values oi. All other values need not be set.
On exit: auxiliary information on the fitted model.
Vi1 contains the linear predictor value, ηi, for i=1,2,,n.
Vi2 contains the fitted value, μ^i, for i=1,2,,n.
Vi3 contains the variance standardization, 1τi, for i=1,2,,n.
Vi4 contains the square root of the working weight, wi12, for i=1,2,,n.
Vi5 contains the deviance residual, ri, for i=1,2,,n.
Vi6 contains the leverage, hi, for i=1,2,,n.
Vi7 contains the offset, oi, for i=1,2,,n. If OFFSET='N', all values will be zero.
Vij for j=8,,IP+7, contains the results of the QR decomposition or the singular value decomposition.
If the model is not of full rank, i.e., IRANK<IP, the first IP rows of columns 8 to IP+7 contain the P* matrix.
21:   LDV – INTEGERInput
On entry: the first dimension of the array V as declared in the (sub)program from which G02GCF is called.
Constraint: LDVN.
22:   TOL – REAL (KIND=nag_wp)Input
On entry: indicates the accuracy required for the fit of the model.
The iterative weighted least squares procedure is deemed to have converged if the absolute change in deviance between iterations is less than TOL×1.0+Current Deviance. This is approximately an absolute precision if the deviance is small and a relative precision if the deviance is large.
If 0.0TOL<machine precision, the routine will use 10×machine precision instead.
Constraint: TOL0.0.
23:   MAXIT – INTEGERInput
On entry: the maximum number of iterations for the iterative weighted least squares.
If MAXIT=0, a default value of 10 is used.
Constraint: MAXIT0.
24:   IPRINT – INTEGERInput
On entry: indicates if the printing of information on the iterations is required.
IPRINT0
There is no printing.
IPRINT>0
Every IPRINT iteration, the following are printed:
  • the deviance;
  • the current estimates;
  • and if the weighted least squares equations are singular then this is indicated.
When printing occurs the output is directed to the current advisory message unit (see X04ABF).
25:   EPS – REAL (KIND=nag_wp)Input
On entry: the value of EPS is used to decide if the independent variables are of full rank and, if not, what is the rank of the independent variables. The smaller the value of EPS the stricter the criterion for selecting the singular value decomposition.
If 0.0EPS<machine precision, the routine will use machine precision instead.
Constraint: EPS0.0.
26:   WKIP×IP+3×IP+22/2 – REAL (KIND=nag_wp) arrayWorkspace
27:   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, because for this routine the values of the output parameters may be useful even if IFAIL0 on exit, the recommended value is -1. 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).
Note: G02GCF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
IFAIL=1
On entry,N<2,
orM<1,
orLDX<N,
orLDV<N,
orIP<1,
orLINK'E', 'I', 'L', 'S' or 'R',
orLINK='E' and A=0.0,
orMEAN'M' or 'Z',
orWEIGHT'U' or 'W',
orOFFSET'N' or 'Y',
orMAXIT<0,
orTOL<0.0,
orEPS<0.0.
IFAIL=2
On entry,WEIGHT='W' and a value of WT<0.0.
IFAIL=3
On entry,a value of ISX<0,
orthe value of IP is incompatible with the values of MEAN and ISX,
orIP is greater than the effective number of observations.
IFAIL=4
On entry,Yi<0.0 for some i=1,2,,n.
IFAIL=5
A fitted value is at the boundary, i.e., μ^=0.0. This may occur if there are y values of 0.0 and the model is too complex for the data. The model should be reformulated with, perhaps, some observations dropped.
IFAIL=6
The singular value decomposition has failed to converge. This is an unlikely error exit.
IFAIL=7
The iterative weighted least squares has failed to converge in MAXIT (or default 10) iterations. The value of MAXIT could be increased but it may be advantageous to examine the convergence using the IPRINT option. This may indicate that the convergence is slow because the solution is at a boundary in which case it may be better to reformulate the model.
IFAIL=8
The rank of the model has changed during the weighted least squares iterations. The estimate for β returned may be reasonable, but you should check how the deviance has changed during iterations.
IFAIL=9
The degrees of freedom for error are 0. A saturated model has been fitted.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

The accuracy depends on the value of TOL as described in Section 5. As the deviance is a function of logμ the accuracy of the β^ will only be a function of TOL. TOL should therefore be set smaller than the accuracy required for β^.

8  Parallelism and Performance

G02GCF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
G02GCF makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

9  Further Comments

None.

10  Example

A 3 by 5 contingency table given by Plackett (1974) is analysed by fitting terms for rows and columns. The table is:
141 67 114 79 39 131 66 143 72 35 36 14 38 28 16 .  

10.1  Program Text

Program Text (g02gcfe.f90)

10.2  Program Data

Program Data (g02gcfe.d)

10.3  Program Results

Program Results (g02gcfe.r)


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

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