G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02DAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

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.

## 2  Specification

 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 N, LDX, M, ISX(M), IP, IDF, LDQ, IRANK, IFAIL REAL (KIND=nag_wp) X(LDX,M), Y(N), WT(*), RSS, B(IP), SE(IP), COV(IP*(IP+1)/2), RES(N), H(N), Q(LDQ,IP+1), P(2*IP+IP*IP), TOL, WK(max(2,5*(IP-1)+IP*IP)) LOGICAL SVD CHARACTER(1) MEAN, WEIGHT

## 3  Description

The general linear regression model is defined by
 $y=Xβ+ε,$
where
• $y$ is a vector of $n$ observations on the dependent variable,
• $X$ is an $n$ by $p$ matrix of the independent variables of column rank $k$,
• $\beta$ is a vector of length $p$ of unknown parameters, and
• $\epsilon$ is a vector of length $n$ of unknown random errors such that $\mathrm{var}\epsilon =V{\sigma }^{2}$, where $V$ is a known diagonal matrix.
If $V=I$, the identity matrix, then least squares estimation is used. If $V\ne I$, then for a given weight matrix $W\propto {V}^{-1}$, weighted least squares estimation is used.
The least squares estimates $\stackrel{^}{\beta }$ of the parameters $\beta$ minimize ${\left(y-X\beta \right)}^{\mathrm{T}}\left(y-X\beta \right)$ while the weighted least squares estimates minimize ${\left(y-X\beta \right)}^{\mathrm{T}}W\left(y-X\beta \right)$.
G02DAF finds a $QR$ decomposition of $X$ (or ${W}^{1/2}X$ in weighted case), i.e.,
 $X=QR* or W1/2X=QR*,$
where ${R}^{*}=\left(\begin{array}{c}R\\ 0\end{array}\right)$ 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 $\stackrel{^}{\beta }$ is the solution to
 $Rβ^=c1,$
where $c={Q}^{\mathrm{T}}y$ (or ${Q}^{\mathrm{T}}{W}^{1/2}y$) and ${c}_{1}$ 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
 $β^=P1D-1Q*1Tc1,$
${P}_{1}$ being the first $k$ columns of $P$, i.e., $P=\left(\begin{array}{cc}{P}_{1}& {P}_{0}\end{array}\right)$, 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 parameters. These solutions can be obtained by using G02DKF after using G02DAF. Only certain linear combinations of the parameters will have unique estimates; these are known as estimable functions.
The fit of the model can be examined by considering the residuals, ${r}_{i}={y}_{i}-\stackrel{^}{y}$, where $\stackrel{^}{y}=X\stackrel{^}{\beta }$ are the fitted values. The fitted values can be written as $Hy$ for an $n$ by $n$ matrix $H$. The $i$th diagonal elements of $H$, ${h}_{i}$, give a measure of the influence of the $i$th values of the independent variables on the fitted regression model. The values ${h}_{i}$ are sometimes known as leverages. Both ${r}_{i}$ and ${h}_{i}$ are provided by G02DAF.
The output of G02DAF also includes $\stackrel{^}{\beta }$, the residual sum of squares and associated degrees of freedom, $\left(n-k\right)$, 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., ${X}_{i,1}=1$, for $i=1,2,\dots ,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.

## 4  References

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

## 5  Parameters

1:     MEAN – CHARACTER(1)Input
On entry: indicates if a mean term is to be included.
${\mathbf{MEAN}}=\text{'M'}$
A mean term, intercept, will be included in the model.
${\mathbf{MEAN}}=\text{'Z'}$
The model will pass through the origin, zero-point.
Constraint: ${\mathbf{MEAN}}=\text{'M'}$ or $\text{'Z'}$.
2:     WEIGHT – CHARACTER(1)Input
On entry: indicates if weights are to be used.
${\mathbf{WEIGHT}}=\text{'U'}$
Least squares estimation is used.
${\mathbf{WEIGHT}}=\text{'W'}$
Weighted least squares is used and weights must be supplied in array WT.
Constraint: ${\mathbf{WEIGHT}}=\text{'U'}$ or $\text{'W'}$.
3:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}\ge 2$.
4:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{i},\mathit{j}\right)$ must contain the $\mathit{i}$th observation for the $\mathit{j}$th independent variable, for $\mathit{i}=1,2,\dots ,{\mathbf{N}}$ and $\mathit{j}=1,2,\dots ,{\mathbf{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: ${\mathbf{LDX}}\ge {\mathbf{N}}$.
6:     M – INTEGERInput
On entry: $m$, the total number of independent variables in the dataset.
Constraint: ${\mathbf{M}}\ge 1$.
7:     ISX(M) – INTEGER arrayInput
On entry: indicates which independent variables are to be included in the model.
${\mathbf{ISX}}\left(j\right)>0$
The variable contained in the $j$th column of X is included in the regression model.
Constraints:
• ${\mathbf{ISX}}\left(\mathit{j}\right)\ge 0$, for $\mathit{j}=1,2,\dots ,m$;
• if ${\mathbf{MEAN}}=\text{'M'}$, exactly ${\mathbf{IP}}-1$ values of ISX must be $\text{}>0$;
• if ${\mathbf{MEAN}}=\text{'Z'}$, exactly IP values of ISX must be $\text{}>0$.
8:     IP – INTEGERInput
On entry: the number of independent variables in the model, including the mean or intercept if present.
Constraints:
• if ${\mathbf{MEAN}}=\text{'M'}$, $1\le {\mathbf{IP}}\le {\mathbf{M}}+1$;
• if ${\mathbf{MEAN}}=\text{'Z'}$, $1\le {\mathbf{IP}}\le {\mathbf{M}}$;
• otherwise $1\le {\mathbf{IP}}\le {\mathbf{N}}$.
9:     Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: $y$, observations on the dependent variable.
10:   WT($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least ${\mathbf{N}}$ if ${\mathbf{WEIGHT}}=\text{'W'}$, and at least $1$ otherwise.
On entry: if ${\mathbf{WEIGHT}}=\text{'W'}$, WT must contain the weights to be used in the weighted regression.
If ${\mathbf{WT}}\left(i\right)=0.0$, the $i$th 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 ${\mathbf{WEIGHT}}=\text{'U'}$, WT is not referenced and the effective number of observations is $n$.
Constraint: if ${\mathbf{WEIGHT}}=\text{'W'}$, ${\mathbf{WT}}\left(\mathit{i}\right)\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$.
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:   B(IP) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{B}}\left(i\right)$, $i=1,2,\dots ,{\mathbf{IP}}$ contains the least squares estimates of the parameters of the regression model, $\stackrel{^}{\beta }$.
If ${\mathbf{MEAN}}=\text{'M'}$, ${\mathbf{B}}\left(1\right)$ will contain the estimate of the mean parameter and ${\mathbf{B}}\left(i+1\right)$ will contain the coefficient of the variable contained in column $j$ of X, where ${\mathbf{ISX}}\left(j\right)$ is the $i$th positive value in the array ISX.
If ${\mathbf{MEAN}}=\text{'Z'}$, ${\mathbf{B}}\left(i\right)$ will contain the coefficient of the variable contained in column $j$ of X, where ${\mathbf{ISX}}\left(j\right)$ is the $i$th positive value in the array ISX.
14:   SE(IP) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{SE}}\left(i\right)$, $i=1,2,\dots ,{\mathbf{IP}}$ contains the standard errors of the IP parameter estimates given in B.
15:   COV(${\mathbf{IP}}×\left({\mathbf{IP}}+1\right)/2$) – REAL (KIND=nag_wp) arrayOutput
On exit: the first ${\mathbf{IP}}×\left({\mathbf{IP}}+1\right)/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 ${\mathbf{B}}\left(i\right)$ and the parameter estimate given in ${\mathbf{B}}\left(j\right)$, $j\ge i$, is stored in ${\mathbf{COV}}\left(j×\left(j-1\right)/2+i\right)$.
16:   RES(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the (weighted) residuals, ${r}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
17:   H(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the diagonal elements of $H$, ${h}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
18:   Q(LDQ,${\mathbf{IP}}+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 ${\mathbf{IP}}+1$ contain the $R$ matrix;
• the strictly lower triangular part of columns $2$ to ${\mathbf{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: ${\mathbf{LDQ}}\ge {\mathbf{N}}$.
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 ${\mathbf{SVD}}=\mathrm{.FALSE.}$, ${\mathbf{IRANK}}={\mathbf{IP}}$.
If ${\mathbf{SVD}}=\mathrm{.TRUE.}$, IRANK is an estimate of the rank of the independent variables.
IRANK is calculated as the number of singular values greater that ${\mathbf{TOL}}×\text{}$ (largest singular value). It is possible for the SVD to be carried out but IRANK to be returned as IP.
22:   P($2×{\mathbf{IP}}+{\mathbf{IP}}×{\mathbf{IP}}$) – REAL (KIND=nag_wp) arrayOutput
On exit: details of the $QR$ decomposition and SVD if used.
If ${\mathbf{SVD}}=\mathrm{.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 ${\mathbf{SVD}}=\mathrm{.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 ${\mathbf{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: ${\mathbf{TOL}}=0.000001$.
Constraint: ${\mathbf{TOL}}\ge 0.0$.
24:   WK($\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(2,5×\left({\mathbf{IP}}-1\right)+{\mathbf{IP}}×{\mathbf{IP}}\right)$) – REAL (KIND=nag_wp) arrayOutput
On exit: if on exit ${\mathbf{SVD}}=\mathrm{.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\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ 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 parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}<2$, or ${\mathbf{M}}<1$, or ${\mathbf{LDX}}<{\mathbf{N}}$, or ${\mathbf{LDQ}}<{\mathbf{N}}$, or ${\mathbf{TOL}}<0.0$, or ${\mathbf{IP}}\le 0$, or ${\mathbf{IP}}>{\mathbf{N}}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{MEAN}}\ne \text{'M'}$ or $\text{'Z'}$, or ${\mathbf{WEIGHT}}\ne \text{'W'}$ or $\text{'U'}$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{WEIGHT}}=\text{'W'}$ and a value of ${\mathbf{WT}}<0.0$.
${\mathbf{IFAIL}}=4$
 On entry, a value of ${\mathbf{ISX}}<0$, or the value of IP is incompatible with the values of MEAN and ISX, or IP is greater than the effective number of observations.
${\mathbf{IFAIL}}=5$
The degrees of freedom for the residuals are zero, i.e., the designated number of parameters 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.
${\mathbf{IFAIL}}=6$
The singular value decomposition has failed to converge, see F02WUF. This is an unlikely error.

## 7  Accuracy

The accuracy of G02DAF is closely related to the accuracy of F02WUF and F08AEF (DGEQRF). These routine documents should be consulted.

Standardized residuals and further measures of influence can be computed using G02FAF. G02FAF requires, in particular, the results stored in RES and H.

## 9  Example

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 (${R}_{2}$), adjusted ${R}_{2}$ and Akaike's information criteria (AIC) are calculated.
G02BUF is then called to calculate the total sums of squares and the coefficient of determination (${R}_{2}$), adjusted ${R}_{2}$ and Akaike's information criteria (AIC) are calculated.

### 9.1  Program Text

Program Text (g02dafe.f90)

### 9.2  Program Data

Program Data (g02dafe.d)

### 9.3  Program Results

Program Results (g02dafe.r)