# NAG Library Routine Document

## 1Purpose

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.

## 2Specification

Fortran Interface
 Subroutine g02daf ( mean, n, x, ldx, m, isx, ip, y, wt, rss, idf, b, se, cov, res, h, q, ldq, svd, p, tol, wk,
 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
#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)

## 3Description

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.
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

## 5Arguments

1:     $\mathbf{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:     $\mathbf{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:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge 2$.
4:     $\mathbf{x}\left({\mathbf{ldx}},{\mathbf{m}}\right)$ – 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:     $\mathbf{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:     $\mathbf{m}$ – IntegerInput
On entry: $m$, the total number of independent variables in the dataset.
Constraint: ${\mathbf{m}}\ge 1$.
7:     $\mathbf{isx}\left({\mathbf{m}}\right)$ – 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:     $\mathbf{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:     $\mathbf{y}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: $y$, the observations on the dependent variable.
10:   $\mathbf{wt}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
Note: the dimension of the array wt must be at least ${\mathbf{n}}$ if ${\mathbf{weight}}=\text{'W'}$.
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$.
11:   $\mathbf{rss}$ – Real (Kind=nag_wp)Output
On exit: the residual sum of squares for the regression.
12:   $\mathbf{idf}$ – IntegerOutput
On exit: the degrees of freedom associated with the residual sum of squares.
13:   $\mathbf{b}\left({\mathbf{ip}}\right)$ – 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:   $\mathbf{se}\left({\mathbf{ip}}\right)$ – 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:   $\mathbf{cov}\left({\mathbf{ip}}×\left({\mathbf{ip}}+1\right)/2\right)$ – 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:   $\mathbf{res}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the (weighted) residuals, ${r}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
17:   $\mathbf{h}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the diagonal elements of $H$, ${h}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
18:   $\mathbf{q}\left({\mathbf{ldq}},{\mathbf{ip}}+1\right)$ – 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:   $\mathbf{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:   $\mathbf{svd}$ – LogicalOutput
On exit: if a singular value decomposition has been performed then svd will be .TRUE., otherwise svd will be .FALSE..
21:   $\mathbf{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:   $\mathbf{p}\left(2×{\mathbf{ip}}+{\mathbf{ip}}×{\mathbf{ip}}\right)$ – 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:   $\mathbf{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:   $\mathbf{wk}\left(\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(2,5×\left({\mathbf{ip}}-1\right)+{\mathbf{ip}}×{\mathbf{ip}}\right)\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:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . 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  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  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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{ip}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ip}}\ge 1$.
On entry, ${\mathbf{ip}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ip}}\le {\mathbf{n}}$.
On entry, ${\mathbf{ldq}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldq}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{ldx}}=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ldx}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 2$.
On entry, ${\mathbf{tol}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tol}}\ge 0.0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{mean}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mean}}=\text{'M'}$ or $\text{'Z'}$.
On entry, ${\mathbf{weight}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{weight}}=\text{'U'}$ or $\text{'W'}$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{wt}}\left(〈\mathit{\text{value}}〉\right)<0.0$.
Constraint: ${\mathbf{wt}}\left(i\right)\ge 0.0$, for $i=1,2,\dots ,n$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{ip}}=〈\mathit{\text{value}}〉$.
Constraint: ip must be compatible with the number of nonzero elements in isx.
On entry, ${\mathbf{isx}}\left(〈\mathit{\text{value}}〉\right)<0$.
Constraint: ${\mathbf{isx}}\left(i\right)\ge 0.0$, for $i=1,2,\dots ,m$.
${\mathbf{ifail}}=5$
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.
${\mathbf{ifail}}=6$
SVD solution failed to converge.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

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

## 8Parallelism 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.

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

## 10Example

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.

### 10.1Program Text

Program Text (g02dafe.f90)

### 10.2Program Data

Program Data (g02dafe.d)

### 10.3Program Results

Program Results (g02dafe.r)