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

# Syntax

C#
```public static void g02dg(
string weight,
int n,
double[] wt,
int ip,
int irank,
double[] cov,
double[,] q,
bool svd,
double[] p,
double[] y,
double[] b,
double[] se,
double[] res,
double[] wk,
out int ifail
)```
Visual Basic
```Public Shared Sub g02dg ( _
weight As String, _
n As Integer, _
wt As Double(), _
ip As Integer, _
irank As Integer, _
cov As Double(), _
q As Double(,), _
svd As Boolean, _
p As Double(), _
y As Double(), _
b As Double(), _
se As Double(), _
res As Double(), _
wk As Double(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g02dg(
String^ weight,
int n,
array<double>^ wt,
int ip,
int irank,
array<double>^ cov,
array<double,2>^ q,
bool svd,
array<double>^ p,
array<double>^ y,
array<double>^ b,
array<double>^ se,
array<double>^ res,
array<double>^ wk,
[OutAttribute] int% ifail
)```
F#
```static member g02dg :
weight : string *
n : int *
wt : float[] *
ip : int *
irank : int *
cov : float[] *
q : float[,] *
svd : bool *
p : float[] *
y : float[] *
b : float[] *
se : float[] *
res : float[] *
wk : float[] *
ifail : int byref -> unit
```

#### Parameters

weight
Type: System..::..String
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"}$.
n
Type: System..::..Int32
On entry: $n$, the number of observations.
Constraint: ${\mathbf{n}}\ge {\mathbf{ip}}$.
wt
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
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-1\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}=0,1,\dots ,n-1$.
Type: System..::..Double%
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$.
ip
Type: System..::..Int32
On entry: $p$, the number of independent variables (including the mean if fitted).
Constraint: $1\le {\mathbf{ip}}\le {\mathbf{n}}$.
irank
Type: System..::..Int32
On entry: the rank of the independent variables, as given by g02da.
Constraint: ${\mathbf{irank}}>0$, and if ${\mathbf{svd}}=\mathrm{false}$, then ${\mathbf{irank}}={\mathbf{ip}}$, else ${\mathbf{irank}}\le {\mathbf{ip}}$.
cov
Type: array<System..::..Double>[]()[][]
An array of size [${\mathbf{ip}}×\left({\mathbf{ip}}+1\right)/2$]
On entry: the covariance matrix of the parameter estimates as given by g02da.
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-1\right]$ and the parameter estimate given in ${\mathbf{b}}\left[j-1\right]$, $j\ge i$, is stored in ${\mathbf{cov}}\left[\left(j×\left(j-1\right)/2+i\right)-1\right]$.
q
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, ${\mathbf{ip}}+1$]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: the results of the $QR$ decomposition as returned by g02da.
On exit: the first column of q contains the new values of $c$, the remainder of q will be unchanged.
svd
Type: System..::..Boolean
On entry: indicates if a singular value decomposition was used by g02da.
${\mathbf{svd}}=\mathrm{true}$
A singular value decomposition was used by g02da.
${\mathbf{svd}}=\mathrm{false}$
A singular value decomposition was not used by g02da.
p
Type: array<System..::..Double>[]()[][]
An array of size [dim1]
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 g02da.
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 not in this release) for details).
If ${\mathbf{svd}}=\mathrm{true}$, the first ip elements of p contain the zeta values for the $QR$ decomposition (see (F08AEF not in this release) for details) and the next ${\mathbf{ip}}×{\mathbf{ip}}+{\mathbf{ip}}$ elements of p contain details of the singular value decomposition.
y
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: the new dependent variable, ${y}_{\text{new}}$.
b
Type: array<System..::..Double>[]()[][]
An array of size [ip]
On exit: the least squares estimates of the parameters of the regression model, $\stackrel{^}{\beta }$.
se
Type: array<System..::..Double>[]()[][]
An array of size [ip]
On exit: the standard error of the estimates of the parameters.
res
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: the residuals for the new regression model.
wk
Type: array<System..::..Double>[]()[][]
An array of size [$5×\left({\mathbf{ip}}-1\right)+{\mathbf{ip}}×{\mathbf{ip}}$]
On entry: if ${\mathbf{svd}}=\mathrm{true}$, wk must be unaltered from the previous call to g02da or g02dg.
If ${\mathbf{svd}}=\mathrm{false}$, wk is used as workspace.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

g02dg uses the results given by g02da to fit the same set of independent variables to a new dependent variable.
g02da 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}}$, g02dg 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 g02da will have computed an SVD of $R$,
 $R=Q*D000PT,$
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({P}_{1}{P}_{0}\right)$, and ${Q}_{{*}_{1}}$ being the first $k$ columns of ${Q}_{*}$. Details of the SVD are made available by g02da in the form of the matrix ${P}^{*}$:
 $P*=D-1P1TP0T.$
The matrix ${Q}_{*}$ is made available through the workspace of g02da.
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.

# 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

# Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDQ) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\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{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$.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

The same accuracy as g02da is obtained.

# Parallelism and Performance

None.

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