NAG FL Interface
g02gpf (glm_​predict)

1 Purpose

g02gpf allows prediction from a generalized linear model fit via g02gaf, g02gbf, g02gcf or g02gdf.

2 Specification

Fortran Interface
Subroutine g02gpf ( errfn, link, mean, offset, weight, n, x, ldx, m, isx, ip, t, off, wt, s, a, b, cov, vfobs, eta, seeta, pred, sepred, ifail)
Integer, Intent (In) :: n, ldx, m, isx(m), ip
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: x(ldx,*), t(*), off(*), wt(*), s, a, b(ip), cov(ip*(ip+1)/2)
Real (Kind=nag_wp), Intent (Out) :: eta(n), seeta(n), pred(n), sepred(n)
Logical, Intent (In) :: vfobs
Character (1), Intent (In) :: errfn, link, mean, offset, weight
C Header Interface
#include <nag.h>
void  g02gpf_ (const char *errfn, const char *link, const char *mean, const char *offset, const char *weight, const Integer *n, const double x[], const Integer *ldx, const Integer *m, const Integer isx[], const Integer *ip, const double t[], const double off[], const double wt[], const double *s, const double *a, const double b[], const double cov[], const logical *vfobs, double eta[], double seeta[], double pred[], double sepred[], Integer *ifail, const Charlen length_errfn, const Charlen length_link, const Charlen length_mean, const Charlen length_offset, const Charlen length_weight)
The routine may be called by the names g02gpf or nagf_correg_glm_predict.

3 Description

A generalized linear model consists of the following elements:
  1. (i)A suitable distribution for the dependent variable y.
  2. (ii)A linear model, with linear predictor η=Xβ, where X is a matrix of independent variables and β a column vector of p parameters.
  3. (iii)A link function g. between the expected value of y and the linear predictor, that is Ey=μ=gη.
In order to predict from a generalized linear model, that is estimate a value for the dependent variable, y, given a set of independent variables X, the matrix X must be supplied, along with values for the parameters β and their associated variance-covariance matrix, C. Suitable values for β and C are usually estimated by first fitting the prediction model to a training dataset with known responses, using for example g02gaf, g02gbf, g02gcf or g02gdf. The predicted variable, and its standard error can then be obtained from:
y^ = g-1η ,   se y^ = δg-1x δx η seη + Ifobs Vary  
where
η=o+Xβ ,   seη = diagXCXT ,  
o is a vector of offsets and Ifobs=0, if the variance of future observations is not taken into account, and 1 otherwise. Here diagA indicates the diagonal elements of matrix A.
If required, the variance for the ith future observation, Varyi, can be calculated as:
Varyi = ϕ Vθ wi  
where wi is a weight, ϕ is the scale (or dispersion) parameter, and Vθ is the variance function. Both the scale parameter and the variance function depend on the distribution used for the y, with:
Poisson Vθ=μi, ϕ=1
binomial Vθ=μiti-μiti, ϕ=1
Normal Vθ=1
gamma Vθ=μi2
In the cases of a Normal and gamma error structure, the scale parameter (ϕ), is supplied by you. This value is usually obtained from the routine used to fit the prediction model. In many cases, for a Normal error structure, ϕ=σ^2, i.e., the estimated variance.

4 References

McCullagh P and Nelder J A (1983) Generalized Linear Models Chapman and Hall

5 Arguments

1: errfn Character(1) Input
On entry: indicates the distribution used to model the dependent variable, y.
errfn='B'
The binomial distribution is used.
errfn='G'
The gamma distribution is used.
errfn='N'
The Normal (Gaussian) distribution is used.
errfn='P'
The Poisson distribution is used.
Constraint: errfn='B', 'G', 'N' or 'P'.
On entry: indicates which link function is to be used.
link='C'
A complementary log-log link is used.
link='E'
An exponent link is used.
link='G'
A logistic link is used.
link='I'
An identity link is used.
link='L'
A log link is used.
link='P'
A probit link is used.
link='R'
A reciprocal link is used.
link='S'
A square root link is used.
Details on the functional form of the different links can be found in the G02 Chapter Introduction.
Constraints:
  • if errfn='B', link='C', 'G' or 'P';
  • otherwise link='E', 'I', 'L', 'R' or 'S'.
3: 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'.
4: offset Character(1) Input
On entry: indicates if an offset is required.
offset='Y'
An offset must be supplied in off.
offset='N'
off is not referenced.
Constraint: offset='Y' or 'N'.
5: weight Character(1) Input
On entry: if vfobs=.TRUE. indicates if weights are used, otherwise weight is not referenced.
weight='U'
No weights are used.
weight='W'
Weights are used and must be supplied in wt.
Constraint: if vfobs=.TRUE., weight='U' or 'W'.
6: n Integer Input
On entry: n, the number of observations.
Constraint: n1.
7: xldx* Real (Kind=nag_wp) array Input
Note: the second dimension of the array x must be at least m.
On entry: xij must contain the ith observation for the jth independent variable, for i=1,2,,n and j=1,2,,m.
8: ldx Integer Input
On entry: the first dimension of the array x as declared in the (sub)program from which g02gpf is called.
Constraint: ldxn.
9: m Integer Input
On entry: m, the total number of independent variables.
Constraint: m1.
10: isxm Integer array Input
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 j=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.
11: ip Integer Input
On entry: the number of independent variables in the model, including the mean or intercept if present.
Constraint: ip>0.
12: t* Real (Kind=nag_wp) array Input
Note: the dimension of the array t must be at least n if errfn='B'.
On entry: if errfn='B', ti must contain the binomial denominator, ti, for the ith observation.
Otherwise t is not referenced.
Constraint: if errfn='B', ti0.0, for i=1,2,,n.
13: off* Real (Kind=nag_wp) array Input
Note: the dimension of the array off must be at least n if offset='Y'.
On entry: if offset='Y', offi must contain the offset oi, for the ith observation.
Otherwise off is not referenced.
14: wt* Real (Kind=nag_wp) array Input
Note: the dimension of the array wt must be at least n if weight='W' and vfobs=.TRUE..
On entry: if weight='W' and vfobs=.TRUE., wti must contain the weight, wi, for the ith observation.
If the variance of future observations is not included in the standard error of the predicted variable, wt is not referenced.
Constraint: if vfobs=.TRUE. and weight='W', wti0., for i=1,2,,i.
15: s Real (Kind=nag_wp) Input
On entry: if errfn='N' or 'G' and vfobs=.TRUE., the scale parameter, ϕ.
Otherwise s is not referenced and ϕ=1.
Constraint: if errfn='N' or 'G' and vfobs=.TRUE., s>0.0.
16: 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.
17: bip Real (Kind=nag_wp) array Input
On entry: the model parameters, β.
If mean='M', b1 must contain the mean parameter and bi+1 the coefficient of the variable contained in the jth independent x, where isxj is the ith positive value in the array isx.
If mean='Z', bi must contain the coefficient of the variable contained in the jth independent x, where isxj is the ith positive value in the array isx.
18: covip×ip+1/2 Real (Kind=nag_wp) array Input
On entry: the upper triangular part of the variance-covariance matrix, C, of the model parameters. This matrix should be supplied packed by column, i.e., the covariance between parameters βi and βj, that is the values stored in bi and bj, should be supplied in covj×j-1/2+i, for i=1,2,,ip and j=i,,ip.
Constraint: the matrix represented in cov must be a valid variance-covariance matrix.
19: vfobs Logical Input
On entry: if vfobs=.TRUE., the variance of future observations is included in the standard error of the predicted variable (i.e., Ifobs=1), otherwise Ifobs=0.
20: etan Real (Kind=nag_wp) array Output
On exit: the linear predictor, η.
21: seetan Real (Kind=nag_wp) array Output
On exit: the standard error of the linear predictor, seη.
22: predn Real (Kind=nag_wp) array Output
On exit: the predicted value, y^.
23: sepredn Real (Kind=nag_wp) array Output
On exit: the standard error of the predicted value, sey^. If predi could not be calculated, g02gpf returns ifail=22, and sepredi is set to -99.0.
24: ifail Integer Input/Output
On entry: ifail must be set to 0, -1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of -1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value -1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value -1 is recommended since useful values can be provided in some output arguments even when ifail0 on exit. 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).
Errors or warnings detected by the routine:
Note: in some cases g02gpf may return useful information.
ifail=1
On entry, errfn=value.
Constraint: errfn='B', 'G', 'N' or 'P'.
ifail=2
On entry, errfn=value and link=value.
Constraint: if errfn='B', link='C', 'G' or 'P',
otherwise, link='E', 'I', 'L', 'R' or 'S'.
ifail=3
On entry, mean=value.
Constraint: mean='M' or 'Z'.
ifail=4
On entry, offset=value.
Constraint: offset='Y' or 'N'.
ifail=5
On entry, weight=value.
Constraint: if vfobs=.TRUE., weight='U' or 'W'.
ifail=6
On entry, n=value.
Constraint: n1.
ifail=8
On entry, ldx=value and n=value.
Constraint: ldxn.
ifail=9
On entry, m=value.
Constraint: m1.
ifail=10
On entry, isxvalue<0.
Constraint: isxj0.0, for j=1,2,,m.
ifail=11
On entry, ip=value.
Constraint: ip>0.
ifail=12
On entry, tvalue=value.
Constraint: ti0.0, for all i.
ifail=14
On entry, wtvalue=value.
Constraint: wti0.0, for all i.
ifail=15
On entry, s=value.
Constraint: s>0.0.
ifail=16
On entry, a=0.0.
Constraint: if link='E', a0.0.
ifail=18
On entry, covvalue=value.
Constraint: covi0.0 for at least one diagonal element.
ifail=22
At least one predicted value could not be calculated as required. sepred is set to -99.0 for affected predicted values.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

Not applicable.

8 Parallelism and Performance

g02gpf 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

The model
y = 1 β1 + β2 x + ε  
is fitted to a training dataset with five observations. The resulting model is then used to predict the response for two new observations.

10.1 Program Text

Program Text (g02gpfe.f90)

10.2 Program Data

Program Data (g02gpfe.d)

10.3 Program Results

Program Results (g02gpfe.r)