hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_correg_glm_normal (g02ga)

Purpose

nag_correg_glm_normal (g02ga) fits a generalized linear model with normal errors.

Syntax

[s, rss, idf, b, irank, se, cov, v, ifail] = g02ga(link, mean, x, isx, ip, y, s, 'n', n, 'm', m, 'wt', wt, 'a', a, 'v', v, 'tol', tol, 'maxit', maxit, 'iprint', iprint, 'eps', eps)
[s, rss, idf, b, irank, se, cov, v, ifail] = nag_correg_glm_normal(link, mean, x, isx, ip, y, s, 'n', n, 'm', m, 'wt', wt, 'a', a, 'v', v, 'tol', tol, 'maxit', maxit, 'iprint', iprint, 'eps', eps)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: offset & weight omitted; v, wt, tol, maxit, iprint, eps, a optional
.

Description

A generalized linear model with Normal errors consists of the following elements:
(a) a set of nn observations, yiyi, from a Normal distribution with probability density function:
1/(sqrt(2π)σ)exp(((yμ)2)/(2σ2)) ,
12πσ exp(- (y-μ) 22σ2 ) ,
where μμ is the mean and σ2σ2 is the variance.
(b) XX, a set of pp independent variables for each observation, x1,x2,,xpx1,x2,,xp.
(c) a linear model:
η = βjxj.
η=βjxj.
(d) a link between the linear predictor, ηη, and the mean of the distribution, μμ, i.e., η = g(μ)η=g(μ). The possible link functions are:
(i) exponent link: η = μaη=μa, for a constant aa,
(ii) identity link: η = μη=μ,
(iii) log link: η = logμη=logμ,
(iv) square root link: η = sqrt(μ)η=μ,
(v) reciprocal link: η = 1/μ η=1μ .
(e) a measure of fit, the residual sum of squares = (yiμ̂i)2= (yi-μ^i) 2.
The linear parameters are estimated by iterative weighted least squares. An adjusted dependent variable, zz, is formed:
z = η + (yμ)(dη)/(dμ)
z=η+(y-μ)dη dμ
and a working weight, ww,
w = ((dη)/(dμ))2.
w= (dη dμ ) 2.
At each iteration an approximation to the estimate of ββ, β̂β^, is found by the weighted least squares regression of zz on XX with weights ww.
nag_correg_glm_normal (g02ga) finds a QRQR decomposition of w(1/2)Xw12X, i.e., w(1/2)X = QRw12X=QR where RR is a pp by pp triangular matrix and QQ is an nn by pp column orthogonal matrix.
If RR is of full rank, then β̂β^ is the solution to
Rβ̂ = QTw(1/2)z.
Rβ^=QTw12z.
If RR is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of RR.
R = Q*
(D0)
0 0
PT,
R=Q* D 0 0 0 PT,
where DD is a kk by kk diagonal matrix with nonzero diagonal elements, kk being the rank of RR and w(1/2)Xw12X.
This gives the solution
β̂ = P1D1
(Q*0)
0 I
QTw(1/2)z
β^=P1D-1 Q* 0 0 I QTw12z
P1P1 being the first kk columns of PP, i.e., P = (P1P0)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
η̂ = g(y).
η^=g(y).
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 RSSfRSSf be the residual sum of squares for the full model with degrees of freedom νfνf and let RSSsRSSs be the residual sum of squares for the sub-model with degrees of freedom νsνs then:
F = ((RSSsRSSf) / (νsνf))/(RSSf / νf),
F=(RSSs-RSSf)/(νs-νf) RSSf/νf ,
has, approximately, an FF-distribution with (νsνfνs-νf), ν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β̂η^=Xβ^, can be written as Hw(1/2)zHw12z for an nn by nn matrix HH. The iith diagonal elements of HH, hihi, give a measure of the influence of the iith values of the independent variables on the fitted regression model. These are sometimes known as leverages.
The fitted values are given by μ̂ = g1(η̂)μ^=g-1(η^).
nag_correg_glm_normal (g02ga) also computes the residuals, rr:
ri = yiμ̂i.
ri=yi-μ^i.
An option allows prior weights ωiωi to be used; this gives a model with:
σi2 = (σ2)/(ωi).
σi2=σ2ωi.
In many linear regression models the first term is taken as a mean term or an intercept, i.e., xi,1 = 1xi,1=1, for i = 1,2,,ni=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, oo:
η = o + βjxj.
η=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 nag_correg_glm_constrain (g02gk) after using nag_correg_glm_normal (g02ga). Only certain linear combinations of the parameters will have unique estimates; these are known as estimable functions and can be estimated and tested using nag_correg_glm_estfunc (g02gn).
Details of the SVD are made available, in the form of the matrix P*P*:
P* =
(D1 P1T )
P0T
.
P*= D-1 P1T P0T .

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

Parameters

Compulsory Input Parameters

Indicates which link function is to be used.
link = 'E'link='E'
An exponent link is used.
link = 'I'link='I'
An identity link is used. You are advised not to use nag_correg_glm_normal (g02ga) with an identity link as nag_correg_linregm_fit (g02da) provides a more efficient way of fitting such a model.
link = 'L'link='L'
A log link is used.
link = 'S'link='S'
A square root link is used.
link = 'R'link='R'
A reciprocal link is used.
Constraint: link = 'E'link='E', 'I''I', 'L''L', 'S''S' or 'R''R'.
2:     mean – string (length ≥ 1)
Indicates if a mean term is to be included.
mean = 'M'mean='M'
A mean term, intercept, will be included in the model.
mean = 'Z'mean='Z'
The model will pass through the origin, zero-point.
Constraint: mean = 'M'mean='M' or 'Z''Z'.
3:     x(ldx,m) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxnldxn.
x(i,j)xij must contain the iith observation for the jjth independent variable, for i = 1,2,,ni=1,2,,n and j = 1,2,,mj=1,2,,m.
4:     isx(m) – int64int32nag_int array
m, the dimension of the array, must satisfy the constraint m1m1.
Indicates which independent variables are to be included in the model.
If isx(j) > 0isxj>0, the variable contained in the jjth column of x is included in the regression model.
Constraints:
  • isx(j)0isxj0, for i = 1,2,,mi=1,2,,m;
  • if mean = 'M'mean='M', exactly ip1ip-1 values of isx must be > 0>0;
  • if mean = 'Z'mean='Z', exactly ip values of isx must be > 0>0.
5:     ip – int64int32nag_int scalar
The number of independent variables in the model, including the mean or intercept if present.
Constraint: ip > 0ip>0.
6:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n2n2.
The observations on the dependent variable, yiyi, for i = 1,2,,ni=1,2,,n.
7:     s – double scalar
The scale parameter for the model, σ2σ2.
If s = 0.0s=0.0, the scale parameter is estimated with the function using the residual mean square.
Constraint: s0.0s0.0.

Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array y and the first dimension of the arrays x, v. (An error is raised if these dimensions are not equal.)
nn, the number of observations.
Constraint: n2n2.
2:     m – int64int32nag_int scalar
Default: The dimension of the array isx and the second dimension of the array x. (An error is raised if these dimensions are not equal.)
mm, the total number of independent variables.
Constraint: m1m1.
3:     wt( : :) – double array
Note: the dimension of the array wt must be at least nn if weight = 'W'weight='W', and at least 11 otherwise.
If weight = 'W'weight='W', wt must contain the weights to be used with the model, ωiωi. If wt(i) = 0.0wti=0.0, the iith 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'weight='U', wt is not referenced and the effective number of observations is nn.
Constraint: if weight = 'W'weight='W', wt(i)0.0wti0.0, for i = 1,2,,ni=1,2,,n.
4:     a – double scalar
If link = 'E'link='E', a must contain the power of the exponential.
If link'E'link'E', a is not referenced.
Default: 00
Constraint: if link = 'E'link='E', a0.0a0.0.
5:     v(n,ip + 7ip+7) – double array
If offset = 'N'offset='N', v need not be set.
If offset = 'Y'offset='Y', v(i,7)vi7, for i = 1,2,,ni=1,2,,n, must contain the offset values oioi. All other values need not be set.
6:     tol – double scalar
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)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 precision0.0tol<machine precision, nag_correg_glm_normal (g02ga) will use 10 × machine precision10×machine precision.
Default: 00
Constraint: tol0.0tol0.0.
7:     maxit – int64int32nag_int scalar
The maximum number of iterations for the iterative weighted least squares.
If maxit = 0maxit=0, a default value of 1010 is used.
Default: 1010
Constraint: maxit0maxit0.
8:     iprint – int64int32nag_int scalar
Indicates if the printing of information on the iterations is required.
iprint0iprint0
There is no printing.
iprint > 0iprint>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 nag_file_set_unit_advisory (x04ab)).
Default: 00
9:     eps – double scalar
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 precision0.0eps<machine precision, the function will use machine precision instead.
Default: 00
Constraint: eps0.0eps0.0.

Input Parameters Omitted from the MATLAB Interface

offset weight ldx ldv wk

Output Parameters

1:     s – double scalar
If on input s = 0.0s=0.0, s contains the estimated value of the scale parameter, σ̂2σ^2.
If on input s0.0s0.0, s is unchanged on exit.
2:     rss – double scalar
The residual sum of squares for the fitted model.
3:     idf – int64int32nag_int scalar
The degrees of freedom associated with the residual sum of squares for the fitted model.
4:     b(ip) – double array
The estimates of the parameters of the generalized linear model, β̂β^.
If mean = 'M'mean='M', b(1)b1 will contain the estimate of the mean parameter and b(i + 1)bi+1 will contain the coefficient of the variable contained in column jj of xx, where isx(j)isxj is the iith positive value in the array isx.
If mean = 'Z'mean='Z', b(i)bi will contain the coefficient of the variable contained in column jj of xx, where isx(j)isxj is the iith positive value in the array isx.
5:     irank – int64int32nag_int scalar
The rank of the independent variables.
If the model is of full rank, irank = ipirank=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 × eps× (largest singular value). It is possible for the SVD to be carried out but for irank to be returned as ip.
6:     se(ip) – double array
The standard errors of the linear parameters.
se(i)sei contains the standard error of the parameter estimate in b(i)bi, for i = 1,2,,ipi=1,2,,ip.
7:     cov(ip × (ip + 1) / 2ip×(ip+1)/2) – double array
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)bi and the parameter estimate given in b(j)bj, jiji, is stored in cov((j × (j1) / 2 + i))cov(j×(j-1)/2+i).
8:     v(n,ip + 7ip+7) – double array
Auxiliary information on the fitted model.
v(i,1)vi1 contains the linear predictor value, ηiηi, for i = 1,2,,ni=1,2,,n.
v(i,2)vi2 contains the fitted value, μ̂iμ^i, for i = 1,2,,ni=1,2,,n.
v(i,3)vi3 is only included for consistency with other functions. v(i,3) = 1.0vi3=1.0, for i = 1,2,,ni=1,2,,n.
v(i,4)vi4 contains the square root of the working weight, wi(1/2)wi12, for i = 1,2,,ni=1,2,,n.
v(i,5)vi5 contains the residual, riri, for i = 1,2,,ni=1,2,,n.
v(i,6)vi6 contains the leverage, hihi, for i = 1,2,,ni=1,2,,n.
v(i,7)vi7 contains the offset, for i = 1,2,,ni=1,2,,n. If offset = 'N'offset='N', all values will be zero.
v(i,j)vij, for j = 8,,ip + 7j=8,,ip+7, contains the results of the QRQR decomposition or the singular value decomposition.
If the model is not of full rank, i.e., irank < ipirank<ip, the first ip rows of columns 88 to ip + 7ip+7 contain the P*P* matrix.
9:     ifail – int64int32nag_int scalar
ifail = 0ifail=0 unless the function detects an error (see [Error Indicators and Warnings]).

Error Indicators and Warnings

Note: nag_correg_glm_normal (g02ga) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

  ifail = 1ifail=1
On entry,n < 2n<2,
orm < 1m<1,
orldx < nldx<n,
orldv < nldv<n,
orip < 1ip<1,
orlink'E','I','L','S'link'E','I','L','S' or 'R',
ors < 0.0s<0.0,
orlink = 'E'link='E' and a = 0.0a=0.0,
ormean'M'mean'M' or 'Z''Z',
orweight'U'weight'U' or 'W''W',
oroffset'N'offset'N' or 'Y',
ormaxit < 0maxit<0,
ortol < 0.0tol<0.0,
oreps < 0.0eps<0.0.
  ifail = 2ifail=2
On entry, weight = 'W'weight='W' and a value of wt < 0.0wt<0.0.
  ifail = 3ifail=3
On entry,a value of isx < 0isx<0,
orthe value of ip is incompatible with the values of mean and isx,
orip is greater than the effective number of observations.
  ifail = 4ifail=4
A fitted value is at a boundary. This will only occur with link = 'L'link='L', 'R''R' or 'E''E'. This may occur if there are small values of yy and the model is not suitable for the data. The model should be reformulated with, perhaps, some observations dropped.
  ifail = 5ifail=5
The singular value decomposition has failed to converge. This is an unlikely error exit, see nag_eigen_real_triang_svd (f02wu).
  ifail = 6ifail=6
The iterative weighted least squares has failed to converge in maxit (or default 1010) 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.
W ifail = 7ifail=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.
W ifail = 8ifail=8
The degrees of freedom for error are 00. A saturated model has been fitted.

Accuracy

The accuracy is determined by tol as described in Section [Parameters]. As the residual sum of squares is a function of μ2μ2 the accuracy of the β̂β^ will depend on the link used and may be of the order sqrt(tol)tol.

Further Comments

None.

Example

function nag_correg_glm_normal_example
link = 'R';
mean_p = 'M';
x = [1;
     2;
     3;
     4;
     5];
isx = [int64(1)];
ip = int64(2);
y = [25;
     10;
     6;
     4;
     3];
s = 0;
tol = 5e-05;
[sOut, rss, idf, b, irank, se, covar, vOut, ifail] = ...
     nag_correg_glm_normal(link, mean_p, x, isx, ip, y, s, 'tol', 5e-5)
 

sOut =

    0.1291


rss =

    0.3872


idf =

                    3


b =

   -0.0239
    0.0638


irank =

                    2


se =

    0.0028
    0.0026


covar =

   1.0e-05 *

    0.7723
   -0.7177
    0.6957


vOut =

    0.0399   25.0387    1.0000 -626.9347   -0.0387    0.9954         0  635.1594  655.2205
    0.1037    9.6387    1.0000  -92.9036    0.3613    0.4577         0    0.1038  136.2024
    0.1676    5.9680    1.0000  -35.6173    0.0320    0.2681         0    0.0398    0.4013
    0.2314    4.3221    1.0000  -18.6803   -0.3221    0.1666         0    0.0209    0.3166
    0.2952    3.3878    1.0000  -11.4769   -0.3878    0.1121         0    0.0128    0.2597


ifail =

                    0


function g02ga_example
link = 'R';
mean_p = 'M';
x = [1;
     2;
     3;
     4;
     5];
isx = [int64(1)];
ip = int64(2);
y = [25;
     10;
     6;
     4;
     3];
s = 0;
tol = 5e-05;
[sOut, rss, idf, b, irank, se, covar, vOut, ifail] = ...
     g02ga(link, mean_p, x, isx, ip, y, s, 'tol', 5e-5)
 

sOut =

    0.1291


rss =

    0.3872


idf =

                    3


b =

   -0.0239
    0.0638


irank =

                    2


se =

    0.0028
    0.0026


covar =

   1.0e-05 *

    0.7723
   -0.7177
    0.6957


vOut =

    0.0399   25.0387    1.0000 -626.9347   -0.0387    0.9954         0  635.1594  655.2205
    0.1037    9.6387    1.0000  -92.9036    0.3613    0.4577         0    0.1038  136.2024
    0.1676    5.9680    1.0000  -35.6173    0.0320    0.2681         0    0.0398    0.4013
    0.2314    4.3221    1.0000  -18.6803   -0.3221    0.1666         0    0.0209    0.3166
    0.2952    3.3878    1.0000  -11.4769   -0.3878    0.1121         0    0.0128    0.2597


ifail =

                    0



PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013