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

NAG Library Routine Document

G02GDF

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

G02GDF fits a generalized linear model with gamma errors.

2  Specification

SUBROUTINE G02GDF ( LINK, MEAN, OFFSET, WEIGHT, N, X, LDX, M, ISX, IP, Y, WT, S, 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(*), S, 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 gamma errors consists of the following elements:
(a) a set of n observations, yi, from a gamma distribution with probability density function:
1Γν νy μ νexp-νy μ 1y  
ν being constant for the sample.
(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, an adjusted deviance. This is a function related to the deviance, but defined for y=0:
i=1ndev*yi,μ^i=i=1n2 logμ^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= τdη dμ 2 ,   where  τ=1μ.  
At each iteration an approximation to the estimate of β, β^ is found by the weighted least squares regression of z on X with weights w.
G02GDF finds a QR decomposition of w12X, i.e.,
If R is of full rank then β^ is the solution to:
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 w12X.
This gives the solution
β^=P1D-1 Q* 0 0 I QTw12z,  
where P1 is 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 scale parameter, ν-1 is estimated by a moment estimator:
ν^ -1 = i=1 n yi - μ^i / μ^ 2 n-k .  
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 or adjusted deviance between two nested models with known ν 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 Hw12z 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η^.
G02GDF also computes the Anscombe residuals, r:
ri = 3 y i 13 - μ^ i 13 μ^ i 13 .  
An option allows the use of prior weights, ωi. This gives a model with:
νi = νωi .  
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 + βj xj .  
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 G02GDF. Only certain linear combinations of the parameters will have unique estimates, these are known as estimable functions, and 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 .  

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

5  Parameters

On entry: indicates which link function is to be used.
LINK='E'
An exponential 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: Xij must contain the ith observation for the jth independent variable, 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 G02GDF 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.
If ISXj>0, the variable contained in the jth column of X is included in the regression model.
Constraints:
  • ISXj0, for i=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, 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:   S – REAL (KIND=nag_wp)Input/Output
On entry: the scale parameter for the gamma model, ν-1.
S=0.0
The scale parameter is estimated with the routine using the formula described in Section 3.
Constraint: S0.0.
On exit: if on input S=0.0, S contains the estimated value of the scale parameter, ν^-1.
If on input S0.0, S is unchanged on exit.
14:   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 LINK='E', A0.0.
15:   DEV – REAL (KIND=nag_wp)Output
On exit: the adjusted deviance for the fitted model.
16:   IDF – INTEGEROutput
On exit: the degrees of freedom asociated with the deviance for the fitted model.
17:   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.
18:   IRANK – INTEGEROutput
On exit: the rank of the independent variables.
If the model is of full rank then IRANK=IP.
If the model is not of full rank then 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.
19:   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.
20:   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 in packed form 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.
21:   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 Anscombe 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.
22:   LDV – INTEGERInput
On entry: the first dimension of the array V as declared in the (sub)program from which G02GDF is called.
Constraint: LDVN.
23:   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 then the routine will use 10×machine precision instead.
Constraint: TOL0.0.
24:   MAXIT – INTEGERInput
On entry: the maximum number of iterations for the iterative weighted least squares.
MAXIT=0
A default value of 10 is used.
Constraint: MAXIT0.
25:   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).
26:   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 then the routine will use machine precision instead.
Constraint: EPS0.0.
27:   WKIP×IP+3×IP+22/2 – REAL (KIND=nag_wp) arrayWorkspace
28:   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: G02GDF 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',
orS<0.0,
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 small values of y and the model is not suitable 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 TOL as described in Section 5. As the adjusted deviance is a function of logμ, the accuracy of the β^s will be a function of TOL, so TOL should be set to a smaller value than the accuracy required for β^.

8  Parallelism and Performance

G02GDF is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
G02GDF 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 set of 10 observations from two groups is input and a model for the two groups is fitted.

10.1  Program Text

Program Text (g02gdfe.f90)

10.2  Program Data

Program Data (g02gdfe.d)

10.3  Program Results

Program Results (g02gdfe.r)


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

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