nag_glm_gamma (g02gdc) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

NAG Library Function Document

nag_glm_gamma (g02gdc)

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

nag_glm_gamma (g02gdc) fits a generalized linear model with gamma errors.

2  Specification

#include <nag.h>
#include <nagg02.h>
void  nag_glm_gamma (Nag_Link link, Nag_IncludeMean mean, Integer n, const double x[], Integer tdx, Integer m, const Integer sx[], Integer ip, const double y[], const double wt[], double offset[], double *scale, double ex_power, double *dev, double *df, double b[], Integer *rank, double se[], double cov[], double v[], Integer tdv, double tol, Integer max_iter, Integer print_iter, const char *outfile, double eps, NagError *fail)

3  Description

A generalized linear model with gamma errors consists of the following elements:
(a) a set of n  observations, y i , from a gamma distribution with probability density function:
1 Γ ν νy μ ν exp - νy μ 1 y .
ν  being constant for the sample.
(b) X , a set of p  independent variables for each observation, x 1 , x 2 , , x p .
(c) a linear model:
η = β j x j .
(d) a link between the linear predictor, η , and the mean of the distribution, μ , η = g μ . The possible link functions are:
(i) power link: η = μ a , for a constant a ,
(ii) identity link: η=μ ,
(iii) log link: η = logμ ,
(iv) square root link: η = μ ,
(e) reciprocal link: η = 1 μ .
(f) a measure of fit, an adjusted deviance. This is a function related to the deviance, but defined for y=0 :
i=1 n dev * y i , μ ^ i = i=1 n 2 log μ ^ i + y i μ ^ i
The linear arguments 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μ , 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 .
nag_glm_gamma (g02gdc) finds a QR  decomposition of w 1 2 X , 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 w 1 2 X .
This gives the solution
β ^ = P 1 D -1 Q * 0 0 I QT w 1 2 z
The iterations are continued until there is only a small change in the deviance.
The initial values for the algorithm are obtained by taking
η ^ = g y
The scale argument, ν -1  is estimated by a moment estimator:
ν ^ -1 = i=1 n y i - μ ^ 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 arguments 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 Hw 1 2 z  for an n  by n  matrix H . The i th diagonal elements of H , h i , give a measure of the influence of the i th values of the independent variables on the fitted regression model. These are known as leverages.
The fitted values are given by μ ^ = g -1 η ^ .
nag_glm_gamma (g02gdc) also computes the Anscombe residuals, r :
r i = 3 y i 1 3 - μ ^ i 1 3 μ ^ i 1 3
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., x i,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 + β j x j .
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 arguments. These solutions can be obtained by using nag_glm_tran_model (g02gkc) after using nag_glm_gamma (g02gdc).
Only certain linear combinations of the arguments will have unique estimates, these are known as estimable functions, these can be estimated and tested using nag_glm_est_func (g02gnc).
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  Arguments

On entry: indicates which link function is to be used.
link=Nag_Expo
An exponent link is used.
link=Nag_Iden
An identity link is used.
link=Nag_Log
A log link is used.
link=Nag_Sqrt
A square root link is used.
link=Nag_Reci
A reciprocal link is used.
Constraint: link=Nag_Expo, Nag_Iden, Nag_Log, Nag_Sqrt or Nag_Reci.
2:     meanNag_IncludeMeanInput
On entry: indicates if a mean term is to be included.
mean=Nag_MeanInclude
A mean term, (intercept), will be included in the model.
mean=Nag_MeanZero
The model will pass through the origin, zero point.
Constraint: mean=Nag_MeanInclude or Nag_MeanZero.
3:     nIntegerInput
On entry: the number of observations, n .
Constraint: n2 .
4:     x[n×tdx]const doubleInput
On entry: x[i-1×tdx+j-1]  must contain the i th observation for the j th independent variable, for i=1,2,,n and j=1,2,,m.
5:     tdxIntegerInput
On entry: the stride separating matrix column elements in the array x.
Constraint: tdxm .
6:     mIntegerInput
On entry: the total number of independent variables.
Constraint: m1 .
7:     sx[m]const IntegerInput
On entry: indicates which independent variables are to be included in the model.
If sx[j-1] > 0 , then the variable contained in the j th column of x is included in the regression model.
Constraints:
  • sx[j-1] 0 , for j=1,2,,m;
  • if mean=Nag_MeanInclude, then exactly ip-1  values of sx must be > 0 ;
  • if mean=Nag_MeanZero, then exactly ip values of sx must be > 0 .
8:     ipIntegerInput
On entry: the number p  of independent variables in the model, including the mean or intercept if present.
Constraint: ip must be > 0 .
9:     y[n]const doubleInput
On entry: observations on the dependent variable, y i , for i=1,2,,n.
Constraint: y[i-1] 0 , for i=1,2,,n.
10:   wt[n]const doubleInput
On entry: if weighted estimates are required, then wt must contain the weights to be used. Otherwise wt need not be defined and may be set to the null pointer NULL, i.e., (double *)0.
If wt[i-1] = 0.0 , then the i th observation is not included in the model, in which case the effective number of observations is the number of observations with positive weights.
If wt is NULL, then the effective number of observations is n .
Constraint: wt is NULL or wt[i-1] 0.0, for i=1,2,,n.
11:   offset[n]doubleInput
On entry: if an offset is required then offset must contain the values of the offset o . Otherwise offset must be supplied as the null pointer, (double *)0.
12:   scaledouble *Input/Output
On entry: the scale argument for the gamma model, ν -1 .
If scale=0.0 , then the scale argument is estimated with the function using the formula described in Section 3.
On exit: if on input scale=0.0 , then scale contains the estimated value of the scale argument, ν ^ -1 . If on input scale0.0 , then scale is unchanged on exit.
Constraint: scale0.0 .
13:   ex_powerdoubleInput
On entry: if link=Nag_Expo then ex_power must contain the power a  of the exponential.
If linkNag_Expo, ex_power is not referenced.
Constraint: if link=Nag_Expo, ex_power0.0 .
14:   devdouble *Output
On exit: the adjusted deviance for the fitted model.
15:   dfdouble *Output
On exit: the degrees of freedom associated with the deviance for the fitted model.
16:   b[ip]doubleOutput
On exit: the estimates of the arguments of the generalized linear model, β ^ .
If mean=Nag_MeanInclude, then b[0]  will contain the estimate of the mean argument and b[i]  will contain the coefficient of the variable contained in column j  of x, where sx[j-1]  is the i th positive value in the array sx.
If mean=Nag_MeanZero, then b[i-1]  will contain the coefficient of the variable contained in column j  of x, where sx[j-1]  is the i th positive value in the array sx.
17:   rankInteger *Output
On exit: the rank of the independent variables.
If the model is of full rank, then rank=ip .
If the model is not of full rank, then rank is an estimate of the rank of the independent variables. rank 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 rank to be returned as ip.
18:   se[ip]doubleOutput
On exit: the standard errors of the linear arguments.
se[i-1]  contains the standard error of the parameter estimate in b[i-1] , for i=1,2,,ip.
19:   cov[ip×ip+1/2]doubleOutput
On exit: the ip × ip+1 / 2  elements of cov contain 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 b[i]  and the parameter estimate given in b[j] , ji , is stored in cov[ j j+1 / 2 + i ] , for i=0,1,,ip - 1 and j=i,,ip - 1.
20:   v[n×tdv]doubleOutput
On exit: auxiliary information on the fitted model.
v[i-1×tdv+0] , contains the linear predictor value, η i , for i=1,2,,n.
v[i-1×tdv+1] , contains the fitted value, μ ^ i , for i=1,2,,n.
v[i-1×tdv+2] , contains the variance standardization, τ i , for i=1,2,,n.
v[i-1×tdv+3] , contains the working weight, w i , for i=1,2,,n.
v[i-1×tdv+4] , contains the Anscombe residual, r i , for i=1,2,,n.
v[i-1×tdv+5] , contains the leverage, h i , for i=1,2,,n.
v[i-1×tdv+j-1] , for j=7,8,,ip + 6, contains the results of the QR  decomposition or the singular value decomposition.
If the model is not of full rank, i.e., rank<ip , then the first ip rows of columns 7  to ip+6  contain the P *  matrix.
21:   tdvIntegerInput
On entry: the stride separating matrix column elements in the array v.
Constraint: tdv ip + 6 .
22:   toldoubleInput
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 Deviance). This is approximately an absolute precision if the deviance is small and a relative precision if the deviance is large.
If 0.0 tol <  machine precision, then the function will use 10 ×  machine precision.
Constraint: tol0.0 .
23:   max_iterIntegerInput
On entry: the maximum number of iterations for the iterative weighted least squares.
If max_iter=0 , then a default value of 10 is used.
Constraint: max_iter0 .
On entry: indicates if the printing of information on the iterations is required and the rate at which printing is produced.
print_iter0
There is no printing.
print_iter>0
The following items are printed every print_iter iterations:
(i) the deviance,
(ii) the current estimates, and
(iii) if the weighted least squares equations are singular then this is indicated.
25:   outfileconst char *Input
On entry: a null terminated character string giving the name of the file to which results should be printed. If outfile is NULL or an empty string then the stdout stream is used. Note that the file will be opened in the append mode.
26:   epsdoubleInput
On entry: the value of eps is used to decide if the independent variables are of full rank and, if not, what the rank of the independent variables is. The smaller the value of eps the stricter the criterion for selecting the singular value decomposition.
If 0.0 eps <  machine precision, then the function will use machine precision instead.
Constraint: eps0.0 .
27:   failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

6  Error Indicators and Warnings

NE_2_INT_ARG_LT
On entry, tdx=value  while m=value . These arguments must satisfy tdxm .
NE_ALLOC_FAIL
Dynamic memory allocation failed.
NE_BAD_PARAM
On entry, argument link had an illegal value.
On entry, argument mean had an illegal value.
NE_INT_ARG_LT
On entry, ip=value.
Constraint: ip1.
On entry, m=value.
Constraint: m1.
On entry, max_iter must not be less than 0: max_iter=value .
On entry, n=value.
Constraint: n2.
On entry, sx[value]  must not be less than 0: sx[value] = value.
On entry, tdv=value  while ip=value . These arguments must satisfy tdv ip + 6 .
NE_IP_GT_OBSERV
ip is greater than the effective number of observations.
NE_IP_INCOMP_SX
ip is incompatible with mean and sx.
NE_LSQ_ITER_NOT_CONV
The iterative weighted least squares has failed to converge in max_iter=value  iterations. The value of max_iter could be increased but it may be advantageous to examine the convergence using the print_iter 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.
NE_NOT_APPEND_FILE
Cannot open file string  for appending.
NE_NOT_CLOSE_FILE
Cannot close file string .
NE_RANK_CHANGED
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.
NE_REAL_ARG_LT
On entry, eps must not be less than 0.0: eps=value .
On entry, scale must not be less than 0.0: scale=value .
On entry, tol must not be less than 0.0: tol=value .
On entry, wt[value]  must not be less than 0.0: wt[value] = value.
On entry, y[value]  must not be less than 0.0: y[value] = value.
NE_REAL_ENUM_ARG_CONS
On entry, ex_power=0.0 , link=Nag_Expo. These arguments must satisfy link=Nag_Expo and ex_power0.0 .
NE_SVD_NOT_CONV
The singular value decomposition has failed to converge.
NE_VALUE_AT_BOUNDARY_D
A fitted value is at a 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.
NE_ZERO_DOF_ERROR
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 adjusted deviance is a function of logμ  the accuracy of the β ^ 's will be a function of tol. tol should therefore be set to a smaller value than the accuracy required for β ^ .

8  Further Comments

None.

9  Example

A set of 10 observations from two groups is input and a model for the two groups is fitted.

9.1  Program Text

Program Text (g02gdce.c)

9.2  Program Data

Program Data (g02gdce.d)

9.3  Program Results

Program Results (g02gdce.r)


nag_glm_gamma (g02gdc) (PDF version)
g02 Chapter Contents
g02 Chapter Introduction
NAG C Library Manual

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