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

NAG Library Routine Document

G02GAF

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

G02GAF fits a generalized linear model with normal errors.

2  Specification

SUBROUTINE G02GAF ( LINK, MEAN, OFFSET, WEIGHT, N, X, LDX, M, ISX, IP, Y, WT, S, A, RSS, 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, RSS, 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 Normal errors consists of the following elements:
(a) a set of n observations, yi, from a Normal distribution with probability density function:
12πσ exp- y-μ 22σ2 ,
where μ is the mean and σ2 is the variance.
(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, μ, i.e., η=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 residual sum of squares = yi-μ^i 2.
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.
At each iteration an approximation to the estimate of β, β^, is found by the weighted least squares regression of z on X with weights w.
G02GAF finds a QR decomposition of w12X, i.e., w12X=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β^=QTw12z.
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
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 residual sum of squares.
The initial values for the algorithm are obtained by taking
η^=gy.
The fit of the model can be assessed by examining and testing the residual sum of squares, in particular comparing the difference in residual sums of squares between nested models, i.e., when one model is a sub-model of the other.
Let RSSf be the residual sum of squares for the full model with degrees of freedom νf and let RSSs be the residual sum of squares for the sub-model with degrees of freedom νs then:
F=RSSs-RSSf/νs-νf RSSf/νf ,
has, approximately, an F-distribution with (νs-νf), νf degrees of freedom.
The parameter 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 sometimes known as leverages.
The fitted values are given by μ^=g-1η^.
G02GAF also computes the residuals, r:
ri=yi-μ^i.
An option allows prior weights ωi to be used; this gives a model with:
σi2=σ2ω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 variable 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 G02GAF. 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 exponent link is used.
LINK='I'
An identity link is used. You are advised not to use G02GAF with an identity link as G02DAF provides a more efficient way of fitting such a model.
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:     X(LDX,M) – 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 G02GAF is called.
Constraint: LDXN.
8:     M – INTEGERInput
On entry: m, the total number of independent variables.
Constraint: M1.
9:     ISX(M) – 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:   Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: the observations on the dependent variable, yi, 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 with the model, ωi. 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 model, σ2.
If S=0.0, the scale parameter is estimated with the routine using the residual mean square.
On exit: if on input S=0.0, S contains the estimated value of the scale parameter, σ^2.
If on input S0.0, S is unchanged on exit.
Constraint: S0.0.
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:   RSS – REAL (KIND=nag_wp)Output
On exit: the residual sum of squares for the fitted model.
16:   IDF – INTEGEROutput
On exit: the degrees of freedom associated with the residual sum of squares for the fitted model.
17:   B(IP) – REAL (KIND=nag_wp) arrayOutput
On exit: the estimates of the parameters of the generalized linear model, β^.
If MEAN='M', B1 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, 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 than EPS× (largest singular value). It is possible for the SVD to be carried out but for IRANK to be returned as IP.
19:   SE(IP) – 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:   COV(IP×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.
21:   V(LDV,IP+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 is only included for consistency with other routines. Vi3=1.0, for i=1,2,,n.
Vi4 contains the square root of the working weight, wi12, for i=1,2,,n.
Vi5 contains the residual, ri, for i=1,2,,n.
Vi6 contains the leverage, hi, for i=1,2,,n.
Vi7 contains the offset, 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 G02GAF 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 interactions is less than TOL×1.0+current residual sum of squares. This is approximately an absolute precision if the residual sum of squares is small and a relative precision if the residual sum of squares is large.
If 0.0TOL<machine precision, G02GAF will use 10×machine precision.
Constraint: TOL0.0.
24:   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.
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 is 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, the routine will use machine precision instead.
Constraint: EPS0.0.
27:   WK(IP×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: G02GAF 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
A fitted value is at a boundary. This will only occur with LINK='L', 'R' or 'E'. 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=5
The singular value decomposition has failed to converge. This is an unlikely error exit, see F02WUF.
IFAIL=6
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=7
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=8
The degrees of freedom for error are 0. A saturated model has been fitted.

7  Accuracy

The accuracy is determined by TOL as described in Section 5. As the residual sum of squares is a function of μ2 the accuracy of the β^ will depend on the link used and may be of the order TOL.

8  Further Comments

None.

9  Example

The model:
y=1β1+β2x +ε
for a sample of five observations.

9.1  Program Text

Program Text (g02gafe.f90)

9.2  Program Data

Program Data (g02gafe.d)

9.3  Program Results

Program Results (g02gafe.r)


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

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