NAG Library Routine Document

g02daf (linregm_fit)


    1  Purpose
    7  Accuracy


g02daf performs a general multiple linear regression when the independent variables may be linearly dependent. Parameter estimates, standard errors, residuals and influence statistics are computed. g02daf may be used to perform a weighted regression.


Fortran Interface
Subroutine g02daf ( mean, weight, n, x, ldx, m, isx, ip, y, wt, rss, idf, b, se, cov, res, h, q, ldq, svd, irank, p, tol, wk, ifail)
Integer, Intent (In):: n, ldx, m, isx(m), ip, ldq
Integer, Intent (Inout):: ifail
Integer, Intent (Out):: idf, irank
Real (Kind=nag_wp), Intent (In):: x(ldx,m), y(n), wt(*), tol
Real (Kind=nag_wp), Intent (Inout):: q(ldq,ip+1)
Real (Kind=nag_wp), Intent (Out):: rss, b(ip), se(ip), cov(ip*(ip+1)/2), res(n), h(n), p(2*ip+ip*ip), wk(max(2,5*(ip-1)+ip*ip))
Logical, Intent (Out):: svd
Character (1), Intent (In):: mean, weight
C Header Interface
#include nagmk26.h
void  g02daf_ (const char *mean, const char *weight, const Integer *n, const double x[], const Integer *ldx, const Integer *m, const Integer isx[], const Integer *ip, const double y[], const double wt[], double *rss, Integer *idf, double b[], double se[], double cov[], double res[], double h[], double q[], const Integer *ldq, logical *svd, Integer *irank, double p[], const double *tol, double wk[], Integer *ifail, const Charlen length_mean, const Charlen length_weight)


The general linear regression model is defined by
If V=I, the identity matrix, then least squares estimation is used. If VI, then for a given weight matrix WV-1, weighted least squares estimation is used.
The least squares estimates β^ of the arguments β minimize y-XβT y-Xβ  while the weighted least squares estimates minimize y-XβT Wy-Xβ .
g02daf finds a QR decomposition of X (or W1/2X in weighted case), i.e.,
X=QR*or  W1/2X=QR*,  
where R*= R 0  and R is a p by p upper triangular matrix and Q is an n by n orthogonal matrix. If R is of full rank, then β^ is the solution to
where c=QTy (or QTW1/2y) and c1 is the first p elements of c. 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 Q* and P are p by p orthogonal matrices. This gives the solution
P1 being the first k columns of P, i.e., P=P1P0, and Q*1 being the first k columns of Q*.
Details of the SVD, are made available, in the form of the matrix P*:
P*= D-1 P1T P0T .  
This 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 g02dkf after using g02daf. Only certain linear combinations of the arguments will have unique estimates; these are known as estimable functions.
The fit of the model can be examined by considering the residuals, ri=yi-y^, where y^=Xβ^ are the fitted values. The fitted values can be written as Hy for an n by n matrix H. The ith diagonal elements of H, hi, give a measure of the influence of the ith values of the independent variables on the fitted regression model. The values hi are sometimes known as leverages. Both ri and hi are provided by g02daf.
The output of g02daf also includes β^, the residual sum of squares and associated degrees of freedom, n-k, the standard errors of the parameter estimates and the variance-covariance matrix of the parameter estimates.
In many linear regression models the first term is taken as a mean term or an intercept, i.e., Xi,1=1, for i=1,2,,n. This is provided as an option. Also only some of the possible independent variables are required to be included in a model, a facility to select variables to be included in the model is provided.
Details of the QR decomposition and, if used, the SVD, are made available. These allow the regression to be updated by adding or deleting an observation using g02dcf, adding or deleting a variable using g02def and g02dff or estimating and testing an estimable function using g02dnf.


Cook R D and Weisberg S (1982) Residuals and Influence in Regression Chapman and Hall
Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley
Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore
Hammarling S (1985) The singular value decomposition in multivariate statistics SIGNUM Newsl. 20(3) 2–25
McCullagh P and Nelder J A (1983) Generalized Linear Models Chapman and Hall
Searle S R (1971) Linear Models Wiley


1:     mean – Character(1)Input
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: mean='M' or 'Z'.
2:     weight – Character(1)Input
On entry: indicates if weights are to be used.
Least squares estimation is used.
Weighted least squares is used and weights must be supplied in array wt.
Constraint: weight='U' or 'W'.
3:     n – IntegerInput
On entry: n, the number of observations.
Constraint: n2.
4:     xldxm – Real (Kind=nag_wp) arrayInput
On entry: xij must contain the ith observation for the jth independent variable, for i=1,2,,n and j=1,2,,m.
5:     ldx – IntegerInput
On entry: the first dimension of the array x as declared in the (sub)program from which g02daf is called.
Constraint: ldxn.
6:     m – IntegerInput
On entry: m, the total number of independent variables in the dataset.
Constraint: m1.
7:     isxm – Integer arrayInput
On entry: indicates which independent variables are to be included in the model.
The variable contained in the jth column of x is included in the regression model.
  • 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.
8:     ip – IntegerInput
On entry: the number of independent variables in the model, including the mean or intercept if present.
  • if mean='M', 1ipm+1;
  • if mean='Z', 1ipm;
  • otherwise 1ipn.
9:     yn – Real (Kind=nag_wp) arrayInput
On entry: y, the observations on the dependent variable.
10:   wt* – Real (Kind=nag_wp) arrayInput
Note: the dimension of the array wt must be at least n if weight='W'.
On entry: if weight='W', wt must contain the weights to be used in the weighted regression.
If wti=0.0, the ith observation is not included in the model, in which case the effective number of observations is the number of observations with nonzero weights. The values of res and h will be set to zero for observations with zero weights.
If weight='U', wt is not referenced and the effective number of observations is n.
Constraint: if weight='W', wti0.0, for i=1,2,,n.
11:   rss – Real (Kind=nag_wp)Output
On exit: the residual sum of squares for the regression.
12:   idf – IntegerOutput
On exit: the degrees of freedom associated with the residual sum of squares.
13:   bip – Real (Kind=nag_wp) arrayOutput
On exit: bi, i=1,2,,ip contains the least squares estimates of the parameters of the regression model, β^.
If mean='M', b1 will contain the estimate of the mean parameter and bi+1 will contain the coefficient of the variable contained in column j of x, where isxj is the ith positive value in the array isx.
If mean='Z', bi will contain the coefficient of the variable contained in column j of x, where isxj is the ith positive value in the array isx.
14:   seip – Real (Kind=nag_wp) arrayOutput
On exit: sei, i=1,2,,ip contains the standard errors of the ip parameter estimates given in b.
15:   covip×ip+1/2 – Real (Kind=nag_wp) arrayOutput
On exit: the first 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 bi and the parameter estimate given in bj, ji, is stored in covj×j-1/2+i.
16:   resn – Real (Kind=nag_wp) arrayOutput
On exit: the (weighted) residuals, ri, for i=1,2,,n.
17:   hn – Real (Kind=nag_wp) arrayOutput
On exit: the diagonal elements of H, hi, for i=1,2,,n.
18:   qldqip+1 – Real (Kind=nag_wp) arrayOutput
On exit: the results of the QR decomposition:
  • the first column of q contains c;
  • the upper triangular part of columns 2 to ip+1 contain the R matrix;
  • the strictly lower triangular part of columns 2 to ip+1 contain details of the Q matrix.
19:   ldq – IntegerInput
On entry: the first dimension of the array q as declared in the (sub)program from which g02daf is called.
Constraint: ldqn.
20:   svd – LogicalOutput
On exit: if a singular value decomposition has been performed then svd will be .TRUE., otherwise svd will be .FALSE..
21:   irank – IntegerOutput
On exit: the rank of the independent variables.
If svd=.FALSE., irank=ip.
If svd=.TRUE., irank is an estimate of the rank of the independent variables.
irank is calculated as the number of singular values greater that tol× (largest singular value). It is possible for the SVD to be carried out but irank to be returned as ip.
22:   p2×ip+ip×ip – Real (Kind=nag_wp) arrayOutput
On exit: details of the QR decomposition and SVD if used.
If svd=.FALSE., only the first ip elements of p are used these will contain the zeta values for the QR decomposition (see f08aef (dgeqrf) for details).
If svd=.TRUE., the first ip elements of p will contain the zeta values for the QR decomposition (see f08aef (dgeqrf) for details) and the next ip elements of p contain singular values. The following ip by ip elements contain the matrix P* stored by columns.
23:   tol – Real (Kind=nag_wp)Input
On entry: the value of tol 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 tol the stricter the criterion for selecting the singular value decomposition. If tol=0.0, the singular value decomposition will never be used; this may cause run time errors or inaccurate results if the independent variables are not of full rank.
Suggested value: tol=0.000001.
Constraint: tol0.0.
24:   wkmax2,5×ip-1+ip×ip – Real (Kind=nag_wp) arrayOutput
On exit: if on exit svd=.TRUE., wk contains information which is needed by g02dgf; otherwise wk is used as workspace.
25:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this argument, the recommended value is 0. 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).

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:
On entry, ip=value.
Constraint: ip1.
On entry, ip=value and n=value.
Constraint: ipn.
On entry, ldq=value and n=value.
Constraint: ldqn.
On entry, ldx=value and n=value.
Constraint: ldxn.
On entry, m=value.
Constraint: m1.
On entry, n=value.
Constraint: n2.
On entry, tol=value.
Constraint: tol0.0.
On entry, mean=value.
Constraint: mean='M' or 'Z'.
On entry, weight=value.
Constraint: weight='U' or 'W'.
On entry, wtvalue<0.0.
Constraint: wti0.0, for i=1,2,,n.
On entry, ip=value.
Constraint: ip must be compatible with the number of nonzero elements in isx.
On entry, isxvalue<0.
Constraint: isxi0.0, for i=1,2,,m.
The degrees of freedom for the residuals are zero, i.e., the designated number of arguments is equal to the effective number of observations. In this case the parameter estimates will be returned along with the diagonal elements of H, but neither standard errors nor the variance-covariance matrix will be calculated.
SVD solution failed to converge.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.


The accuracy of g02daf is closely related to the accuracy of f02wuf and f08aef (dgeqrf). These routine documents should be consulted.

Parallelism and Performance

g02daf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g02daf 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.

Further Comments

Standardized residuals and further measures of influence can be computed using g02faf. g02faf requires, in particular, the results stored in res and h.


Data from an experiment with four treatments and three observations per treatment are read in. The treatments are represented by dummy (0-1) variables. An unweighted model is fitted with a mean included in the model. g02buf is then called to calculate the total sums of squares and the coefficient of determination (R2), adjusted R2 and Akaike's information criteria (AIC) are calculated.

Program Text

Program Text (g02dafe.f90)

Program Data

Program Data (g02dafe.d)

Program Results

Program Results (g02dafe.r)

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