G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02DGF

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

G02DGF calculates the estimates of the parameters of a general linear regression model for a new dependent variable after a call to G02DAF.

## 2  Specification

 SUBROUTINE G02DGF ( WEIGHT, N, WT, RSS, IP, IRANK, COV, Q, LDQ, SVD, P, Y, B, SE, RES, WK, IFAIL)
 INTEGER N, IP, IRANK, LDQ, IFAIL REAL (KIND=nag_wp) WT(*), RSS, COV(IP*(IP+1)/2), Q(LDQ,IP+1), P(*), Y(N), B(IP), SE(IP), RES(N), WK(5*(IP-1)+IP*IP) LOGICAL SVD CHARACTER(1) WEIGHT

## 3  Description

G02DGF uses the results given by G02DAF to fit the same set of independent variables to a new dependent variable.
G02DAF computes a $QR$ decomposition of the matrix of $p$ independent variables and also, if the model is not of full rank, a singular value decomposition (SVD). These results can be used to compute estimates of the parameters for a general linear model with a new dependent variable. The $QR$ decomposition leads to the formation of an upper triangular $p$ by $p$ matrix $R$ and an $n$ by $n$ orthogonal matrix $Q$. In addition the vector $c={Q}^{\mathrm{T}}y$ (or ${Q}^{\mathrm{T}}{W}^{1/2}y$) is computed. For a new dependent variable, ${y}_{\mathrm{new}}$, G02DGF computes a new value of $c={Q}^{\mathrm{T}}{y}_{\text{new}}$ or ${Q}^{\mathrm{T}}{W}^{1/2}{y}_{\text{new}}$.
If $R$ is of full rank, then the least squares parameter estimates, $\stackrel{^}{\beta }$, are the solution to
 $Rβ^=c1,$
where ${c}_{1}$ is the first $p$ elements of $c$.
If $R$ is not of full rank, then G02DAF will have computed an 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-1 Q*1T c1,$
${P}_{1}$ being the first $k$ columns of $P$, i.e., $P=\left({P}_{1}{P}_{0}\right)$, and ${Q}_{{*}_{1}}$ being the first $k$ columns of ${Q}_{*}$. Details of the SVD are made available by G02DAF in the form of the matrix ${P}^{*}$:
 $P*= D-1 P1T P0T .$
The matrix ${Q}_{*}$ is made available through the workspace of G02DAF.
In addition to parameter estimates, the new residuals are computed and the variance-covariance matrix of the parameter estimates are found by scaling the variance-covariance matrix for the original regression.

## 4  References

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
Searle S R (1971) Linear Models Wiley

## 5  Parameters

1:     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'}$.
2:     N – INTEGERInput
On entry: $n$, the number of observations.
Constraint: ${\mathbf{N}}\ge {\mathbf{IP}}$.
3:     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.
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 entry: the residual sum of squares for the original dependent variable.
On exit: the residual sum of squares for the new dependent variable.
Constraint: ${\mathbf{RSS}}>0.0$.
5:     IP – INTEGERInput
On entry: $p$, the number of independent variables (including the mean if fitted).
Constraint: $1\le {\mathbf{IP}}\le {\mathbf{N}}$.
6:     IRANK – INTEGERInput
On entry: the rank of the independent variables, as given by G02DAF.
Constraint: ${\mathbf{IRANK}}>0$, and if ${\mathbf{SVD}}=\mathrm{.FALSE.}$, then ${\mathbf{IRANK}}={\mathbf{IP}}$, else ${\mathbf{IRANK}}\le {\mathbf{IP}}$.
7:     COV(${\mathbf{IP}}×\left({\mathbf{IP}}+1\right)/2$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the covariance matrix of the parameter estimates as given by G02DAF.
On exit: 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(\left(j×\left(j-1\right)/2+i\right)\right)$.
8:     Q(LDQ,${\mathbf{IP}}+1$) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the results of the $QR$ decomposition as returned by G02DAF.
On exit: the first column of Q contains the new values of $c$, the remainder of Q will be unchanged.
9:     LDQ – INTEGERInput
On entry: the first dimension of the array Q as declared in the (sub)program from which G02DGF is called.
Constraint: ${\mathbf{LDQ}}\ge {\mathbf{N}}$.
10:   SVD – LOGICALInput
On entry: indicates if a singular value decomposition was used by G02DAF.
${\mathbf{SVD}}=\mathrm{.TRUE.}$
A singular value decomposition was used by G02DAF.
${\mathbf{SVD}}=\mathrm{.FALSE.}$
A singular value decomposition was not used by G02DAF.
11:   P($*$) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array P must be at least ${\mathbf{IP}}$ if ${\mathbf{SVD}}=\mathrm{.FALSE.}$, and at least ${\mathbf{IP}}×{\mathbf{IP}}+2×{\mathbf{IP}}$ otherwise.
On entry: details of the $QR$ decomposition and SVD, if used, as returned in array P by G02DAF.
If ${\mathbf{SVD}}=\mathrm{.FALSE.}$, only the first IP elements of P are used; these contain the zeta values for the $QR$ decomposition (see F08AEF (DGEQRF) for details).
If ${\mathbf{SVD}}=\mathrm{.TRUE.}$, the first IP elements of P contain the zeta values for the $QR$ decomposition (see F08AEF (DGEQRF) for details) and the next ${\mathbf{IP}}×{\mathbf{IP}}+{\mathbf{IP}}$ elements of P contain details of the singular value decomposition.
12:   Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: the new dependent variable, ${y}_{\text{new}}$.
13:   B(IP) – REAL (KIND=nag_wp) arrayOutput
On exit: the least squares estimates of the parameters of the regression model, $\stackrel{^}{\beta }$.
14:   SE(IP) – REAL (KIND=nag_wp) arrayOutput
On exit: the standard error of the estimates of the parameters.
15:   RES(N) – REAL (KIND=nag_wp) arrayOutput
On exit: the residuals for the new regression model.
16:   WK($5×\left({\mathbf{IP}}-1\right)+{\mathbf{IP}}×{\mathbf{IP}}$) – REAL (KIND=nag_wp) arrayInput
On entry: if ${\mathbf{SVD}}=\mathrm{.TRUE.}$, WK must be unaltered from the previous call to G02DAF or G02DGF.
If ${\mathbf{SVD}}=\mathrm{.FALSE.}$, WK is used as workspace.
17:   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{IP}}<1$, or ${\mathbf{N}}<{\mathbf{IP}}$, or ${\mathbf{IRANK}}\le 0$, or ${\mathbf{SVD}}=\mathrm{.FALSE.}$ and ${\mathbf{IRANK}}\ne {\mathbf{IP}}$, or ${\mathbf{SVD}}=\mathrm{.TRUE.}$ and ${\mathbf{IRANK}}>{\mathbf{IP}}$, or ${\mathbf{LDQ}}<{\mathbf{N}}$, or ${\mathbf{RSS}}\le 0.0$, or ${\mathbf{WEIGHT}}\ne \text{'U'}$ or $\text{'W'}$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{WEIGHT}}=\text{'W'}$ and a value of ${\mathbf{WT}}<0.0$.

## 7  Accuracy

The same accuracy as G02DAF is obtained.

The values of the leverages, ${h}_{i}$, are unaltered by a change in the dependent variable so a call to G02FAF can be made using the value of H from G02DAF.

## 9  Example

A dataset consisting of $12$ observations with four independent variables and two dependent variables are read in. A model with all four independent variables is fitted to the first dependent variable by G02DAF and the results printed. The model is then fitted to the second dependent variable by G02DGF and those results printed.

### 9.1  Program Text

Program Text (g02dgfe.f90)

### 9.2  Program Data

Program Data (g02dgfe.d)

### 9.3  Program Results

Program Results (g02dgfe.r)