﻿ g02da Method
g02da performs a general multiple linear regression when the independent variables may be linearly dependent. Parameter estimates, standard errors, residuals and influence statistics are computed. g02da may be used to perform a weighted regression.

# Syntax

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

#### Parameters

mean
Type: System..::..String
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"}$.
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 2$.
x
Type: array<System..::..Double,2>[,](,)[,][,]
An array of size [dim1, m]
Note: dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{n}}$
On entry: ${\mathbf{x}}\left[\mathit{i}-1,\mathit{j}-1\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}}$.
m
Type: System..::..Int32
On entry: $m$, the total number of independent variables in the dataset.
Constraint: ${\mathbf{m}}\ge 1$.
isx
Type: array<System..::..Int32>[]()[][]
An array of size [m]
On entry: indicates which independent variables are to be included in the model.
${\mathbf{isx}}\left[j-1\right]>0$
The variable contained in the $j$th column of x is included in the regression model.
Constraints:
• ${\mathbf{isx}}\left[\mathit{j}-1\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$.
ip
Type: System..::..Int32
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}}$.
y
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: $y$, observations on the dependent variable.
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. 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}-1\right]\ge 0.0$, for $\mathit{i}=1,2,\dots ,n$.
Type: System..::..Double%
On exit: the residual sum of squares for the regression.
idf
Type: System..::..Int32%
On exit: the degrees of freedom associated with the residual sum of squares.
b
Type: array<System..::..Double>[]()[][]
An array of size [ip]
On exit: ${\mathbf{b}}\left[i-1\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[0\right]$ will contain the estimate of the mean parameter and ${\mathbf{b}}\left[i\right]$ will contain the coefficient of the variable contained in column $j$ of x, where ${\mathbf{isx}}\left[j-1\right]$ is the $i$th positive value in the array isx.
If ${\mathbf{mean}}=\text{"Z"}$, ${\mathbf{b}}\left[i-1\right]$ will contain the coefficient of the variable contained in column $j$ of x, where ${\mathbf{isx}}\left[j-1\right]$ is the $i$th positive value in the array isx.
se
Type: array<System..::..Double>[]()[][]
An array of size [ip]
On exit: ${\mathbf{se}}\left[i-1\right]$, $i=1,2,\dots ,{\mathbf{ip}}$ contains the standard errors of the ip parameter estimates given in b.
cov
Type: array<System..::..Double>[]()[][]
An array of size [${\mathbf{ip}}×\left({\mathbf{ip}}+1\right)/2$]
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-1\right]$ and the parameter estimate given in ${\mathbf{b}}\left[j-1\right]$, $j\ge i$, is stored in ${\mathbf{cov}}\left[j×\left(j-1\right)/2+i-1\right]$.
res
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: the (weighted) residuals, ${r}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
h
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: the diagonal elements of $H$, ${h}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
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 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.
svd
Type: System..::..Boolean%
On exit: if a singular value decomposition has been performed then svd will be true, otherwise svd will be false.
irank
Type: System..::..Int32%
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.
p
Type: array<System..::..Double>[]()[][]
An array of size [$2×{\mathbf{ip}}+{\mathbf{ip}}×{\mathbf{ip}}$]
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 not in this release) for details).
If ${\mathbf{svd}}=\mathrm{true}$, the first ip elements of p will contain the zeta values for the $QR$ decomposition (see (F08AEF not in this release) 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.
tol
Type: System..::..Double
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$.
wk
Type: array<System..::..Double>[]()[][]
An array of size [$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(2,5×\left({\mathbf{ip}}-1\right)+{\mathbf{ip}}×{\mathbf{ip}}\right)$]
On exit: if on exit ${\mathbf{svd}}=\mathrm{true}$, wk contains information which is needed by g02dg; otherwise 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

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)$.
g02da 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*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(\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-1P1TP0T.$
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 g02dk after using g02da. 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 g02da.
The output of g02da 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 g02dc, adding or deleting a variable using g02de and g02df or estimating and testing an estimable function using g02dn.

# 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

# Error Indicators and Warnings

Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (LDX, LDQ) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$, or ${\mathbf{m}}<1$, 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 not in this release). This is an unlikely error.
${\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 accuracy of g02da is closely related to the accuracy of (F08AEF not in this release) (F02WUF not in this release). These method documents should be consulted.

None.