NAG Library Routine Document

g02eaf (linregm_rssq)


    1  Purpose
    7  Accuracy


g02eaf calculates the residual sums of squares for all possible linear regressions for a given set of independent variables.


Fortran Interface
Subroutine g02eaf ( mean, weight, n, m, x, ldx, vname, isx, y, wt, nmod, modl, ldmodl, rss, nterms, mrank, wk, ifail)
Integer, Intent (In):: n, m, ldx, isx(m), ldmodl
Integer, Intent (Inout):: ifail
Integer, Intent (Out):: nmod, nterms(ldmodl), mrank(ldmodl)
Real (Kind=nag_wp), Intent (In):: x(ldx,m), y(n), wt(*)
Real (Kind=nag_wp), Intent (Out):: rss(ldmodl), wk(n*(m+1))
Character (*), Intent (In):: vname(m)
Character (*), Intent (Inout):: modl(ldmodl,m)
Character (1), Intent (In):: mean, weight
C Header Interface
#include nagmk26.h
void  g02eaf_ (const char *mean, const char *weight, const Integer *n, const Integer *m, const double x[], const Integer *ldx, const char vname[], const Integer isx[], const double y[], const double wt[], Integer *nmod, char modl[], const Integer *ldmodl, double rss[], Integer nterms[], Integer mrank[], double wk[], Integer *ifail, const Charlen length_mean, const Charlen length_weight, const Charlen length_vname, const Charlen length_modl)


For a set of k possible independent variables there are 2k linear regression models with from zero to k independent variables in each model. For example if k=3 and the variables are A, B and C then the possible models are:
(i) null model
(ii) A
(iii) B
(iv) C
(v) A and B
(vi) A and C
(vii) B and C
(viii) A, B and C.
g02eaf calculates the residual sums of squares from each of the 2k possible models. The method used involves a QR decomposition of the matrix of possible independent variables. Independent variables are then moved into and out of the model by a series of Givens rotations and the residual sums of squares computed for each model; see Clark (1981) and Smith and Bremner (1989).
The computed residual sums of squares are then ordered first by increasing number of terms in the model, then by decreasing size of residual sums of squares. So the first model will always have the largest residual sum of squares and the 2kth will always have the smallest. This aids you in selecting the best possible model from the given set of independent variables.
g02eaf allows you to specify some independent variables that must be in the model, the forced variables. The other independent variables from which the possible models are to be formed are the free variables.


Clark M R B (1981) A Givens algorithm for moving from one linear model to another without going back to the data Appl. Statist. 30 198–203
Smith D M and Bremner J M (1989) All possible subset regressions using the QR decomposition Comput. Statist. Data Anal. 7 217–236
Weisberg S (1985) Applied Linear Regression 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.
  • n2;
  • nm, is the number of independent variables to be considered (forced plus free plus mean if included), as specified by mean and isx.
4:     m – IntegerInput
On entry: the number of variables contained in x.
Constraint: m2.
5:     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.
6:     ldx – IntegerInput
On entry: the first dimension of the array x as declared in the (sub)program from which g02eaf is called.
Constraint: ldxn.
7:     vnamem – Character(*) arrayInput
On entry: vnamej must contain the name of the variable in column j of x, for j=1,2,,m.
8:     isxm – Integer arrayInput
On entry: indicates which independent variables are to be considered in the model.
The variable contained in the jth column of x is included in all regression models, i.e., is a forced variable.
The variable contained in the jth column of x is included in the set from which the regression models are chosen, i.e., is a free variable.
The variable contained in the jth column of x is not included in the models.
  • isxj0, for j=1,2,,m;
  • at least one value of isx=1.
9:     yn – Real (Kind=nag_wp) arrayInput
On entry: yi must contain the ith observation on the dependent variable, yi, for i=1,2,,n.
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.
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:   nmod – IntegerOutput
On exit: the total number of models for which residual sums of squares have been calculated.
12:   modlldmodlm – Character(*) arrayOutput
On exit: the first ntermsi elements of the ith row of modl contain the names of the independent variables, as given in vname, that are included in the ith model.
Constraint: the length of modl should be greater or equal to the length of vname.
13:   ldmodl – IntegerInput
On entry: the first dimension of the array modl and the dimension of the arrays rss, nterms and mrank as declared in the (sub)program from which g02eaf is called.
  • ldmodlm;
  • ldmodl2k, k is the number of free variables in the model as specified in isx, and hence 2k is the total number of models to be generated.
14:   rssldmodl – Real (Kind=nag_wp) arrayOutput
On exit: rssi contains the residual sum of squares for the ith model, for i=1,2,,nmod.
15:   ntermsldmodl – Integer arrayOutput
On exit: ntermsi contains the number of independent variables in the ith model, not including the mean if one is fitted, for i=1,2,,nmod.
16:   mrankldmodl – Integer arrayOutput
On exit: mranki contains the rank of the residual sum of squares for the ith model.
17:   wkn×m+1 – Real (Kind=nag_wp) arrayWorkspace
18:   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, ldmodl=value and m=value.
Constraint: ldmodlm.
On entry, ldx=value and n=value.
Constraint: ldxn.
On entry, m=value.
Constraint: m2.
On entry, mean=value.
Constraint: mean='M' or 'Z'.
On entry, n=value.
Constraint: n2.
On entry, weight=value.
Constraint: weight='W' or 'U'.
On entry, wtvalue<0.0.
Constraint: wti0.0, for i=1,2,,n.
On entry, isxvalue<0.
Constraint: isxi0, for i=1,2,,m.
There are no free variables, i.e., no element of isx=1.
On entry, ldmodl=value and number of possible models is value.
Constraint: ldmodl the number of possible models.
On entry, the number of independent variables to be considered (forced plus free plus mean if included) is greater or equal to the effective number of observations.
The full model is not of full rank, i.e., some of the independent variables may be linear combinations of other independent variables. Variables must be excluded from the model in order to give full rank.
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.


For a discussion of the improved accuracy obtained by using a method based on the QR decomposition see Smith and Bremner (1989).

Parallelism and Performance

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

g02ecf may be used to compute R2 and Cp-values from the results of g02eaf.
If a mean has been included in the model and no variables are forced in then rss1 contains the total sum of squares and in many situations a reasonable estimate of the variance of the errors is given by rssnmod/n-1-ntermsnmod.


The data for this example is given in Weisberg (1985). The independent variables and the dependent variable are read, as are the names of the variables. These names are as given in Weisberg (1985). The residual sums of squares computed and printed with the names of the variables in the model.

Program Text

Program Text (g02eafe.f90)

Program Data

Program Data (g02eafe.d)

Program Results

Program Results (g02eafe.r)

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