NAG Library Function Document
nag_glm_normal (g02gac)
1 Purpose
nag_glm_normal (g02gac) fits a generalized linear model with normal errors.
2 Specification
#include <nag.h> |
#include <nagg02.h> |
void |
nag_glm_normal (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[],
const double offset[],
double *scale,
double ex_power,
double *rss,
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 Normal errors consists of the following elements:
(a) |
a set of observations, , from a Normal distribution with probability density function:
where is the mean and is the variance. |
(b) |
, a set of independent variables for each observation, . |
(c) |
a linear model:
|
(d) |
a link between the linear predictor, , and the mean of the distribution, , i.e., . The possible link functions are:
(i) |
exponent link: , for a constant , |
(ii) |
identity link: , |
(iii) |
log link: , |
(iv) |
square root link: , |
(v) |
reciprocal link: . |
|
(e) |
a measure of fit, the residual sum of squares |
The linear arguments are estimated by iterative weighted least squares. An adjusted dependent variable,
, is formed:
and a working weight,
,
At each iteration an approximation to the estimate of , , is found by the weighted least squares regression of on with weights .
nag_glm_normal (g02gac) finds a decomposition of , i.e.,
- where is a by triangular matrix and is a by column orthogonal matrix.
If
is of full rank, then
is the solution to:
If
is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of
.
where D is a
by
diagonal matrix with nonzero diagonal elements,
being the rank of
and
.
This gives the solution
being the first
columns of
, i.e.,
.
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
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
be the residual sum of squares for the full model with degrees of freedom
and let
be the residual sum of squares for the sub-model with degrees of freedom
then:
has, approximately, a
-distribution with
,
degrees of freedom.
The parameter estimates, , are asymptotically Normally distributed with variance-covariance matrix:
- in the full rank case, otherwise
The residuals and influence statistics can also be examined.
The estimated linear predictor , can be written as for an by matrix . The th diagonal elements of , , give a measure of the influence of the th values of the independent variables on the fitted regression model. These are sometimes known as leverages.
The fitted values are given by .
nag_glm_normal (g02gac) also computes the residuals,
:
An option allows prior weights,
to be used, this gives a model with:
In many linear regression models the first term is taken as a mean term or an intercept, i.e., , for ; 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,
:
If the model is not of full rank the solution given will be only one of the possible solutions. Other estimates be 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_normal (g02gac). Only certain linear combinations of the arguments will have unique estimates; these are known as estimable functions and can be estimated and tested using
nag_glm_est_func (g02gnc).
Details of the SVD, are made available, in the form of the matrix :
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
- 1:
– Nag_LinkInput
-
On entry: indicates which link function is to be used.
- An exponent link is used.
- An identity link is used. You are advised not to use nag_glm_normal (g02gac) with an identity link as nag_regsn_mult_linear (g02dac) provides a more efficient way of fitting such a model.
- A log link is used.
- A square root link is used.
- A reciprocal link is used.
Constraint:
, , , or .
- 2:
– Nag_IncludeMeanInput
-
On entry: indicates if a mean term is to be included.
- A mean term, (intercept), will be included in the model.
- The model will pass through the origin, zero point.
Constraint:
or .
- 3:
– IntegerInput
-
On entry: the number of observations, .
Constraint:
.
- 4:
– const doubleInput
-
On entry: must contain the th observation for the th independent variable, for and .
- 5:
– IntegerInput
-
On entry: the stride separating matrix column elements in the array
x.
Constraint:
.
- 6:
– IntegerInput
-
On entry: the total number of independent variables.
Constraint:
.
- 7:
– const IntegerInput
-
On entry: indicates which independent variables are to be included in the model. If
, then the variable contained in the
th column of
x is included in the regression model.
Constraints:
- , for ;
- if , then exactly values of sx must be ;
- if , then exactly ip values of sx must be .
- 8:
– IntegerInput
-
On entry: the number of independent variables in the model, including the mean or intercept if present.
Constraint:
.
- 9:
– const doubleInput
-
On entry: observations on the dependent variable, , for .
- 10:
– 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
NULL.
If , then the 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
.
Constraint:
or , for .
- 11:
– const doubleInput
-
On entry: if an offset is required then
offset must contain the values of the offset
. Otherwise
offset must be supplied as
NULL.
- 12:
– double *Input/Output
-
On entry: indicates the scale argument for the model, . If , then the scale argument is estimated using the residual mean square.
On exit: if on input
, then
scale contains the estimated value of the scale argument,
. If on input
, then
scale is unchanged on exit.
Constraint:
.
- 13:
– doubleInput
-
On entry: if
then
ex_power must contain the power
of the exponential.
If
,
ex_power is not referenced.
Constraint:
If , .
-
On exit: the residual sum of squares for the fitted model.
- 15:
– double *Output
-
On exit: the degrees of freedom associated with the residual sum of squares for the fitted model.
- 16:
– doubleOutput
-
On exit:
,
contains the estimates of the arguments of the generalized linear model,
.
If
, then
will contain the estimate of the mean argument and
will contain the coefficient of the variable contained in column
of
x, where
is the
th positive value in the array
sx.
If
, then
will contain the coefficient of the variable contained in column
of
x, where
is the
th positive value in the array
sx.
- 17:
– Integer *Output
-
On exit: the rank of the independent variables.
If the model is of full rank, then .
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
(largest singular value). It is possible for the SVD to be carried out but
rank to be returned as
ip.
- 18:
– doubleOutput
-
On exit: the standard errors of the linear arguments.
contains the standard error of the parameter estimate in , for .
- 19:
– doubleOutput
-
On exit: the
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
and the parameter estimate given in
,
, is stored in
, for
and
.
- 20:
– doubleOutput
-
On exit: auxiliary information on the fitted model.
, contains the linear predictor value, , for .
, contains the fitted value, , for .
, is only included for consistency with other functions. , for .
, contains the working weight, , for .
, contains the standardized residual, , for .
, contains the leverage, , for .
, for , contains the results of the decomposition or the singular value decomposition.
If the model is not of full rank, i.e.,
, then the first
ip rows of columns
to
contain the
matrix.
- 21:
– IntegerInput
-
On entry: the stride separating matrix column elements in the array
v.
Constraint:
.
- 22:
– doubleInput
-
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 (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 machine precision, then the function will use machine precision.
Constraint:
.
- 23:
– IntegerInput
-
On entry: the maximum number of iterations for the iterative weighted least squares. If , then a default value of 10 is used.
Constraint:
.
- 24:
– IntegerInput
-
On entry: indicates if the printing of information on the iterations is required and the rate at which printing is produced. The following values are available:
- There is no printing.
- 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:
– const 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:
– doubleInput
-
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 machine precision, then the function will use machine precision instead.
Constraint:
.
- 27:
– NagError *Input/Output
-
The NAG error argument (see
Section 2.7 in How to Use the NAG Library and its Documentation).
6 Error Indicators and Warnings
- NE_2_INT_ARG_LT
-
On entry, while . These arguments must satisfy .
On entry, while . These arguments must satisfy .
- 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, .
Constraint: .
On entry, .
Constraint: .
On entry,
max_iter must not be less than 0:
.
On entry, .
Constraint: .
On entry, must not be less than 0: .
- NE_IP_GT_OBSERV
-
Parameter
ip is greater than the effective number of observations.
- NE_IP_INCOMP_SX
-
Parameter
ip is incompatible with arguments
mean and
sx.
- NE_LSQ_ITER_NOT_CONV
-
The iterative weighted least squares has failed to converge in
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 for appending.
- NE_NOT_CLOSE_FILE
-
Cannot close file .
- 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:
.
On entry,
scale must not be less than 0.0:
.
On entry,
tol must not be less than 0.0:
.
On entry, must not be less than 0.0: .
- NE_REAL_ENUM_ARG_CONS
-
On entry, , . These arguments must satisfy and .
- NE_SVD_NOT_CONV
-
The singular value decomposition has failed to converge.
- NE_VALUE_AT_BOUNDARY_A
-
A fitted value is at a boundary. This will only occur with , or . This may occur if there are small values of 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 residual sum of squares is a function of
the accuracy of the
's will depend on the link used and may be of the order
.
8 Parallelism and Performance
nag_glm_normal (g02gac) is not threaded in any implementation.
None.
10 Example
The model:
for a sample of five observations.
10.1 Program Text
Program Text (g02gace.c)
10.2 Program Data
Program Data (g02gace.d)
10.3 Program Results
Program Results (g02gace.r)