g02gd fits a generalized linear model with gamma errors.

# Syntax

C#
```public static void g02gd(
string mean,
string offset,
string weight,
int n,
double[,] x,
int m,
int[] isx,
int ip,
double[] y,
double[] wt,
ref double s,
double a,
out double dev,
out int idf,
double[] b,
out int irank,
double[] se,
double[] cov,
double[,] v,
double tol,
int maxit,
int iprint,
double eps,
out int ifail
)```
Visual Basic
```Public Shared Sub g02gd ( _
mean As String, _
offset As String, _
weight As String, _
n As Integer, _
x As Double(,), _
m As Integer, _
isx As Integer(), _
ip As Integer, _
y As Double(), _
wt As Double(), _
ByRef s As Double, _
a As Double, _
<OutAttribute> ByRef dev As Double, _
<OutAttribute> ByRef idf As Integer, _
b As Double(), _
<OutAttribute> ByRef irank As Integer, _
se As Double(), _
cov As Double(), _
v As Double(,), _
tol As Double, _
maxit As Integer, _
iprint As Integer, _
eps As Double, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g02gd(
String^ mean,
String^ offset,
String^ weight,
int n,
array<double,2>^ x,
int m,
array<int>^ isx,
int ip,
array<double>^ y,
array<double>^ wt,
double% s,
double a,
[OutAttribute] double% dev,
[OutAttribute] int% idf,
array<double>^ b,
[OutAttribute] int% irank,
array<double>^ se,
array<double>^ cov,
array<double,2>^ v,
double tol,
int maxit,
int iprint,
double eps,
[OutAttribute] int% ifail
)```
F#
```static member g02gd :
mean : string *
offset : string *
weight : string *
n : int *
x : float[,] *
m : int *
isx : int[] *
ip : int *
y : float[] *
wt : float[] *
s : float byref *
a : float *
dev : float byref *
idf : int byref *
b : float[] *
irank : int byref *
se : float[] *
cov : float[] *
v : float[,] *
tol : float *
maxit : int *
iprint : int *
eps : float *
ifail : int byref -> unit
```

#### Parameters

mean
Type: System..::..String
On entry: indicates if a mean term is to be included.
${\mathbf{mean}}=\text{"M"}$
A mean term, intercept, will be included in the model.
${\mathbf{mean}}=\text{"Z"}$
The model will pass through the origin, zero-point.
Constraint: ${\mathbf{mean}}=\text{"M"}$ or $\text{"Z"}$.
offset
Type: System..::..String
On entry: indicates if an offset is required.
${\mathbf{offset}}=\text{"Y"}$
An offset is required and the offsets must be supplied in the seventh column of v.
${\mathbf{offset}}=\text{"N"}$
No offset is required.
Constraint: ${\mathbf{offset}}=\text{"N"}$ or $\text{"Y"}$.
weight
Type: System..::..String
On entry: indicates if prior weights are to be used.
${\mathbf{weight}}=\text{"U"}$
No prior weights are used.
${\mathbf{weight}}=\text{"W"}$
Prior weights are used and weights must be supplied in wt.
Constraint: ${\mathbf{weight}}=\text{"U"}$ or $\text{"W"}$.
n
Type: System..::..Int32
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
x
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: ${\mathbf{x}}\left[\mathit{i}-1,\mathit{j}-1\right]$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th independent variable, for $\mathit{i}=1,2,\dots ,{\mathbf{n}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
m
Type: System..::..Int32
On entry: $m$, the total number of independent variables.
Constraint: ${\mathbf{m}}\ge 1$.
isx
Type: array<System..::..Int32>[]()[][]
An array of size [m]
On entry: indicates which independent variables are to be included in the model.
If ${\mathbf{isx}}\left[j-1\right]>0$, the variable contained in the $j$th column of x is included in the regression model.
Constraints:
• ${\mathbf{isx}}\left[j-1\right]\ge 0$, for $\mathit{i}=0,1,\dots ,{\mathbf{m}}-1$;
• if ${\mathbf{mean}}=\text{"M"}$, exactly ${\mathbf{ip}}-1$ values of isx must be $\text{}>0$;
• if ${\mathbf{mean}}=\text{"Z"}$, exactly ip values of isx must be $\text{}>0$.
ip
Type: System..::..Int32
On entry: the number of independent variables in the model, including the mean or intercept if present.
Constraint: ${\mathbf{ip}}>0$.
y
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: $y$, the dependent variable.
Constraint: ${\mathbf{y}}\left[\mathit{i}\right]\ge 0.0$, for $\mathit{i}=0,1,\dots ,n-1$.
wt
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
Note: the dimension of the array wt must be at least ${\mathbf{n}}$ if ${\mathbf{weight}}=\text{"W"}$, and at least $1$ otherwise.
On entry: if ${\mathbf{weight}}=\text{"W"}$, wt must contain the weights to be used in the weighted regression. If ${\mathbf{wt}}\left[i-1\right]=0.0$, the $i$th observation is not included in the model, in which case the effective number of observations is the number of observations with nonzero weights.
If ${\mathbf{weight}}=\text{"U"}$, wt is not referenced and the effective number of observations is $n$.
Constraint: if ${\mathbf{weight}}=\text{"W"}$, ${\mathbf{wt}}\left[\mathit{i}\right]\ge 0.0$, for $\mathit{i}=0,1,\dots ,n-1$.
s
Type: System..::..Double%
On entry: the scale parameter for the gamma model, ${\nu }^{-1}$.
${\mathbf{s}}=0.0$
The scale parameter is estimated with the method using the formula described in [Description].
Constraint: ${\mathbf{s}}\ge 0.0$.
On exit: if on input ${\mathbf{s}}=0.0$, s contains the estimated value of the scale parameter, ${\stackrel{^}{\nu }}^{-1}$.
If on input ${\mathbf{s}}\ne 0.0$, s is unchanged on exit.
a
Type: System..::..Double
On entry: if ${\mathbf{link}}=\text{"E"}$, a must contain the power of the exponential.
If ${\mathbf{link}}\ne \text{"E"}$, a is not referenced.
Constraint: if ${\mathbf{link}}=\text{"E"}$, ${\mathbf{a}}\ne 0.0$.
dev
Type: System..::..Double%
On exit: the adjusted deviance for the fitted model.
idf
Type: System..::..Int32%
On exit: the degrees of freedom asociated with the deviance for the fitted model.
b
Type: array<System..::..Double>[]()[][]
An array of size [ip]
On exit: the estimates of the parameters of the generalized linear model, $\stackrel{^}{\beta }$.
If ${\mathbf{mean}}=\text{"M"}$, the first element of b will contain the estimate of the mean parameter and ${\mathbf{b}}\left[i\right]$ will contain the coefficient of the variable contained in column $j$ of ${\mathbf{x}}$, where ${\mathbf{isx}}\left[j-1\right]$ is the $i$th positive value in the array isx.
If ${\mathbf{mean}}=\text{"Z"}$, ${\mathbf{b}}\left[i-1\right]$ will contain the coefficient of the variable contained in column $j$ of ${\mathbf{x}}$, where ${\mathbf{isx}}\left[j-1\right]$ is the $i$th positive value in the array isx.
irank
Type: System..::..Int32%
On exit: the rank of the independent variables.
If the model is of full rank then ${\mathbf{irank}}={\mathbf{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 ${\mathbf{eps}}×\text{}$(largest singular value). It is possible for the SVD to be carried out but for irank to be returned as ip.
se
Type: array<System..::..Double>[]()[][]
An array of size [ip]
On exit: the standard errors of the linear parameters.
${\mathbf{se}}\left[\mathit{i}-1\right]$ contains the standard error of the parameter estimate in ${\mathbf{b}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{ip}}$.
cov
Type: array<System..::..Double>[]()[][]
An array of size [${\mathbf{ip}}×\left({\mathbf{ip}}+1\right)/2$]
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 ${\mathbf{b}}\left[i-1\right]$ and the parameter estimate given in ${\mathbf{b}}\left[j-1\right]$, $j\ge i$, is stored in ${\mathbf{cov}}\left[\left(j×\left(j-1\right)/2+i\right)-1\right]$.
v
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, ${\mathbf{ip}}+7$]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: if ${\mathbf{offset}}=\text{"N"}$, v need not be set.
If ${\mathbf{offset}}=\text{"Y"}$, ${\mathbf{v}}\left[\mathit{i}-1,6\right]$, for $\mathit{i}=1,2,\dots ,n$, must contain the offset values ${o}_{\mathit{i}}$. All other values need not be set.
On exit: auxiliary information on the fitted model.
 ${\mathbf{v}}\left[i-1,0\right]$ contains the linear predictor value, ${\eta }_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$. ${\mathbf{v}}\left[i-1,1\right]$ contains the fitted value, ${\stackrel{^}{\mu }}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$. ${\mathbf{v}}\left[i-1,2\right]$ contains the variance standardization, $\frac{1}{{\tau }_{\mathit{i}}}$, for $\mathit{i}=1,2,\dots ,n$. ${\mathbf{v}}\left[i-1,3\right]$ contains the square root of the working weight, ${w}_{\mathit{i}}^{\frac{1}{2}}$, for $\mathit{i}=1,2,\dots ,n$. ${\mathbf{v}}\left[i-1,4\right]$ contains the Anscombe residual, ${r}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$. ${\mathbf{v}}\left[i-1,5\right]$ contains the leverage, ${h}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$. ${\mathbf{v}}\left[i-1,6\right]$ contains the offset, ${o}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$. If ${\mathbf{offset}}=\text{"N"}$, all values will be zero. ${\mathbf{v}}\left[i-1,j-1\right]$, for $j=8,\dots ,{\mathbf{ip}}+7$, contains the results of the $QR$ decomposition or the singular value decomposition.
If the model is not of full rank, i.e., ${\mathbf{irank}}<{\mathbf{ip}}$, the first ip rows of columns $8$ to ${\mathbf{ip}}+7$ contain the ${P}^{*}$ matrix.
tol
Type: System..::..Double
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 ${\mathbf{tol}}×\left(1.0+\text{Current Deviance}\right)$. This is approximately an absolute precision if the deviance is small and a relative precision if the deviance is large.
If  then the method will use  instead.
Constraint: ${\mathbf{tol}}\ge 0.0$.
maxit
Type: System..::..Int32
On entry: the maximum number of iterations for the iterative weighted least squares.
${\mathbf{maxit}}=0$
A default value of $10$ is used.
Constraint: ${\mathbf{maxit}}\ge 0$.
iprint
Type: System..::..Int32
On entry: indicates if the printing of information on the iterations is required.
${\mathbf{iprint}}\le 0$
There is no printing.
${\mathbf{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 not in this release)).
eps
Type: System..::..Double
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  then the method will use machine precision instead.
Constraint: ${\mathbf{eps}}\ge 0.0$.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# 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μ1y$
$\nu$ being constant for the sample.
(b) $X$, a set of $p$ independent variables for each observation, ${x}_{1},{x}_{2},\dots ,{x}_{p}$.
(c) a linear model:
 $η=∑βjxj.$
(d) a link between the linear predictor, $\eta$, and the mean of the distribution, $\mu$, $\eta =g\left(\mu \right)$. The possible link functions are:
 (i) exponent link: $\eta ={\mu }^{a}$, for a constant $a$, (ii) identity link: $\eta =\mu$, (iii) log link: $\eta =\mathrm{log} \mu$, (iv) square root link: $\eta =\sqrt{\mu }$, (v) reciprocal link: $\eta =\frac{1}{\mu }$.
(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=1n2logμ^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 $\beta$, $\stackrel{^}{\beta }$ is found by the weighted least squares regression of $z$ on $X$ with weights $w$.
g02gd finds a $QR$ decomposition of ${w}^{\frac{1}{2}}X$, i.e.,
• ${w}^{\frac{1}{2}}X=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 $\stackrel{^}{\beta }$ is the solution to:
• $R\stackrel{^}{\beta }={Q}^{\mathrm{T}}{w}^{\frac{1}{2}}z$
If $R$ is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of $R$.
 $R=Q*D000PT.$
where $D$ is a $k$ by $k$ diagonal matrix with nonzero diagonal elements, $k$ being the rank of $R$ and ${w}^{\frac{1}{2}}X$.
This gives the solution
 $β^=P1D-1Q*00IQTw12z,$
where ${P}_{1}$ is the first $k$ columns of $P$, i.e., $P=\left({P}_{1}{P}_{0}\right)$.
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, ${\nu }^{-1}$ is estimated by a moment estimator:
 $ν^-1=∑i=1nyi-μ^i/μ^2n-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 $\nu$ has, asymptotically, a ${\chi }^{2}$-distribution with degrees of freedom given by the difference in the degrees of freedom associated with the two deviances.
The parameters estimates, $\stackrel{^}{\beta }$, are asymptotically Normally distributed with variance-covariance matrix:
• $C={R}^{-1}{{R}^{-1}}^{\mathrm{T}}{\nu }^{-1}$ in the full rank case, otherwise
• $C={P}_{1}{D}^{-2}{P}_{1}^{\mathrm{T}}{\nu }^{-1}$.
The residuals and influence statistics can also be examined.
The estimated linear predictor $\stackrel{^}{\eta }=X\stackrel{^}{\beta }$, can be written as $H{w}^{\frac{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 $\stackrel{^}{\mu }={g}^{-1}\left(\stackrel{^}{\eta }\right)$.
g02gd also computes the Anscombe residuals, $r$:
 $ri=3yi13-μ^i13μ^i13.$
An option allows the use of prior weights, ${\omega }_{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,\dots ,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 g02gk after using g02gd. Only certain linear combinations of the parameters will have unique estimates, these are known as estimable functions, and can be estimated and tested using g02gn.
Details of the SVD are made available in the form of the matrix ${P}^{*}$:
 $P*=D-1P1TP0T.$

# 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

# Error Indicators and Warnings

Note: g02gd may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDX, LDV) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$, or ${\mathbf{m}}<1$, or ${\mathbf{ip}}<1$, or ${\mathbf{link}}\ne \text{"E"},\text{"I"},\text{"L"},\text{"S"}$ or "R", or ${\mathbf{s}}<0.0$, or ${\mathbf{link}}=\text{"E"}$ and ${\mathbf{a}}=0.0$, or ${\mathbf{mean}}\ne \text{"M"}$ or $\text{"Z"}$, or ${\mathbf{weight}}\ne \text{"U"}$ or $\text{"W"}$, or ${\mathbf{offset}}\ne \text{"N"}$ or "Y", or ${\mathbf{maxit}}<0$, or ${\mathbf{tol}}<0.0$, or ${\mathbf{eps}}<0.0$.
${\mathbf{ifail}}=3$
 On entry, a value of ${\mathbf{isx}}<0$, or the value of ip is incompatible with the values of mean and isx, or ip is greater than the effective number of observations.
${\mathbf{ifail}}=4$
 On entry, ${\mathbf{y}}\left[i-1\right]<0.0$ for some $i=1,2,\dots ,n$.
${\mathbf{ifail}}=5$
A fitted value is at the boundary, i.e., $\stackrel{^}{\mu }=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.
${\mathbf{ifail}}=6$
The singular value decomposition has failed to converge. This is an unlikely error exit.
${\mathbf{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.
${\mathbf{ifail}}=8$
The rank of the model has changed during the weighted least squares iterations. The estimate for $\beta$ returned may be reasonable, but you should check how the deviance has changed during iterations.
${\mathbf{ifail}}=9$
The degrees of freedom for error are $0$. A saturated model has been fitted.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

The accuracy depends on tol as described in [Parameters]. As the adjusted deviance is a function of $\mathrm{log} \mu$, the accuracy of the $\stackrel{^}{\beta }$s will be a function of tol, so tol should be set to a smaller value than the accuracy required for $\stackrel{^}{\beta }$.

None.

None.

# Example

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

Example program (C#): g02gde.cs

Example program data: g02gde.d

Example program results: g02gde.r