Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_correg_linregm_fit (g02da)

## Purpose

nag_correg_linregm_fit (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. nag_correg_linregm_fit (g02da) may be used to perform a weighted regression.

## Syntax

[rss, idf, b, se, cov, res, h, q, svd, irank, p, wk, ifail] = g02da(mean, x, isx, ip, y, 'n', n, 'm', m, 'wt', wt, 'tol', tol)
[rss, idf, b, se, cov, res, h, q, svd, irank, p, wk, ifail] = nag_correg_linregm_fit(mean, x, isx, ip, y, 'n', n, 'm', m, 'wt', wt, 'tol', tol)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: weight dropped from interface, wt now optional
.

## Description

The general linear regression model is defined by
 y = Xβ + ε, $y=Xβ+ε,$
where
• y$y$ is a vector of n$n$ observations on the dependent variable,
• X$X$ is an n$n$ by p$p$ matrix of the independent variables of column rank k$k$,
• β$\beta$ is a vector of length p$p$ of unknown parameters, and
• ε$\epsilon$ is a vector of length n$n$ of unknown random errors such that varε = Vσ2$\mathrm{var}\epsilon =V{\sigma }^{2}$, where V$V$ is a known diagonal matrix.
If V = I$V=I$, the identity matrix, then least squares estimation is used. If VI$V\ne I$, then for a given weight matrix WV1$W\propto {V}^{-1}$, weighted least squares estimation is used.
The least squares estimates β̂$\stackrel{^}{\beta }$ of the parameters β$\beta$ minimize (yXβ)T (yXβ) ${\left(y-X\beta \right)}^{\mathrm{T}}\left(y-X\beta \right)$ while the weighted least squares estimates minimize (yXβ)T W(yXβ) ${\left(y-X\beta \right)}^{\mathrm{T}}W\left(y-X\beta \right)$.
nag_correg_linregm_fit (g02da) finds a QR$QR$ decomposition of X$X$ (or W1 / 2X${W}^{1/2}X$ in weighted case), i.e.,
 X = QR* (or  W1 / 2X = QR*), $X=QR* (or W1/2X=QR*),$
where R* =
 ( R ) 0
${R}^{*}=\left(\begin{array}{c}R\\ 0\end{array}\right)$ and R$R$ is a p$p$ by p$p$ upper triangular matrix and Q$Q$ is an n$n$ by n$n$ orthogonal matrix. If R$R$ is of full rank, then β̂$\stackrel{^}{\beta }$ is the solution to
 Rβ̂ = c1, $Rβ^=c1,$
where c = QTy$c={Q}^{\mathrm{T}}y$ (or QTW1 / 2y${Q}^{\mathrm{T}}{W}^{1/2}y$) and c1${c}_{1}$ is the first p$p$ elements of c$c$. If R$R$ is not of full rank a solution is obtained by means of a singular value decomposition (SVD) of R$R$,
R = Q*
 ( D 0 ) 0 0
PT,
$R=Q* D 0 0 0 PT,$
where D$D$ is a k$k$ by k$k$ diagonal matrix with nonzero diagonal elements, k$k$ being the rank of R$R$, and Q*${Q}_{*}$ and P$P$ are p$p$ by p$p$ orthogonal matrices. This gives the solution
 β̂ = P1D − 1Q*1Tc1, $β^=P1D-1Q*1Tc1,$
P1${P}_{1}$ being the first k$k$ columns of P$P$, i.e., P =
 ( P1 P0 )
$P=\left(\begin{array}{cc}{P}_{1}& {P}_{0}\end{array}\right)$, and Q*1${Q}_{{*}_{1}}$ being the first k$k$ columns of Q*${Q}_{*}$.
Details of the SVD, are made available, in the form of the matrix P*${P}^{*}$:
P* =
 ( D − 1 P1T ) P0T
.
$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 nag_correg_linregm_constrain (g02dk) after using nag_correg_linregm_fit (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, ri = yi${r}_{i}={y}_{i}-\stackrel{^}{y}$, where = Xβ̂$\stackrel{^}{y}=X\stackrel{^}{\beta }$ are the fitted values. The fitted values can be written as Hy$Hy$ for an n$n$ by n$n$ matrix H$H$. The i$i$th diagonal elements of H$H$, hi${h}_{i}$, give a measure of the influence of the i$i$th values of the independent variables on the fitted regression model. The values hi${h}_{i}$ are sometimes known as leverages. Both ri${r}_{i}$ and hi${h}_{i}$ are provided by nag_correg_linregm_fit (g02da).
The output of nag_correg_linregm_fit (g02da) also includes β̂$\stackrel{^}{\beta }$, the residual sum of squares and associated degrees of freedom, (nk)$\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., Xi,1 = 1${X}_{i,1}=1$, for i = 1,2,,n$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$QR$ decomposition and, if used, the SVD, are made available. These allow the regression to be updated by adding or deleting an observation using nag_correg_linregm_obs_edit (g02dc), adding or deleting a variable using nag_correg_linregm_var_add (g02de) and nag_correg_linregm_var_del (g02df) or estimating and testing an estimable function using nag_correg_linregm_estfunc (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

## Parameters

### Compulsory Input Parameters

1:     mean – string (length ≥ 1)
Indicates if a mean term is to be included.
mean = 'M'${\mathbf{mean}}=\text{'M'}$
A mean term, intercept, will be included in the model.
mean = 'Z'${\mathbf{mean}}=\text{'Z'}$
The model will pass through the origin, zero-point.
Constraint: mean = 'M'${\mathbf{mean}}=\text{'M'}$ or 'Z'$\text{'Z'}$.
2:     x(ldx,m) – double array
ldx, the first dimension of the array, must satisfy the constraint ldxn$\mathit{ldx}\ge {\mathbf{n}}$.
x(i,j)${\mathbf{x}}\left(\mathit{i},\mathit{j}\right)$ must contain the i$\mathit{i}$th observation for the j$\mathit{j}$th independent variable, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$ and j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
3:     isx(m) – int64int32nag_int array
m, the dimension of the array, must satisfy the constraint m1${\mathbf{m}}\ge 1$.
Indicates which independent variables are to be included in the model.
isx(j) > 0${\mathbf{isx}}\left(j\right)>0$
The variable contained in the j$j$th column of x is included in the regression model.
Constraints:
• isx(j)0${\mathbf{isx}}\left(\mathit{j}\right)\ge 0$, for j = 1,2,,m$\mathit{j}=1,2,\dots ,m$;
• if mean = 'M'${\mathbf{mean}}=\text{'M'}$, exactly ip1${\mathbf{ip}}-1$ values of isx must be > 0$\text{}>0$;
• if mean = 'Z'${\mathbf{mean}}=\text{'Z'}$, exactly ip values of isx must be > 0$\text{}>0$.
4:     ip – int64int32nag_int scalar
The number of independent variables in the model, including the mean or intercept if present.
Constraints:
• if mean = 'M'${\mathbf{mean}}=\text{'M'}$, 1ipm + 1$1\le {\mathbf{ip}}\le {\mathbf{m}}+1$;
• if mean = 'Z'${\mathbf{mean}}=\text{'Z'}$, 1ipm$1\le {\mathbf{ip}}\le {\mathbf{m}}$;
• otherwise 1ipn$1\le {\mathbf{ip}}\le {\mathbf{n}}$.
5:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n2${\mathbf{n}}\ge 2$.
y$y$, observations on the dependent variable.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array y and the first dimension of the array x. (An error is raised if these dimensions are not equal.)
n$n$, the number of observations.
Constraint: n2${\mathbf{n}}\ge 2$.
2:     m – int64int32nag_int scalar
Default: The dimension of the array isx and the second dimension of the array x. (An error is raised if these dimensions are not equal.)
m$m$, the total number of independent variables in the dataset.
Constraint: m1${\mathbf{m}}\ge 1$.
3:     wt( : $:$) – double array
Note: the dimension of the array wt must be at least n${\mathbf{n}}$ if weight = 'W'$\mathit{weight}=\text{'W'}$, and at least 1$1$ otherwise.
If If provided, wt must contain the weights to be used in the weighted regression.
If wt(i) = 0.0${\mathbf{wt}}\left(i\right)=0.0$, the i$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 wt is not provided, the effective number of observations is n$n$.
Constraint: if weight = 'W'$\mathit{weight}=\text{'W'}$, wt(i)0.0${\mathbf{wt}}\left(\mathit{i}\right)\ge 0.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
4:     tol – double scalar
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 tol = 0.0${\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.
Default: 0.000001$0.000001$
Constraint: tol0.0${\mathbf{tol}}\ge 0.0$.

weight ldx ldq

### Output Parameters

The residual sum of squares for the regression.
2:     idf – int64int32nag_int scalar
The degrees of freedom associated with the residual sum of squares.
3:     b(ip) – double array
b(i)${\mathbf{b}}\left(i\right)$, i = 1,2,,ip$i=1,2,\dots ,{\mathbf{ip}}$ contains the least squares estimates of the parameters of the regression model, β̂$\stackrel{^}{\beta }$.
If mean = 'M'${\mathbf{mean}}=\text{'M'}$, b(1)${\mathbf{b}}\left(1\right)$ will contain the estimate of the mean parameter and b(i + 1)${\mathbf{b}}\left(i+1\right)$ will contain the coefficient of the variable contained in column j$j$ of x, where isx(j)${\mathbf{isx}}\left(j\right)$ is the i$i$th positive value in the array isx.
If mean = 'Z'${\mathbf{mean}}=\text{'Z'}$, b(i)${\mathbf{b}}\left(i\right)$ will contain the coefficient of the variable contained in column j$j$ of x, where isx(j)${\mathbf{isx}}\left(j\right)$ is the i$i$th positive value in the array isx.
4:     se(ip) – double array
se(i)${\mathbf{se}}\left(i\right)$, i = 1,2,,ip$i=1,2,\dots ,{\mathbf{ip}}$ contains the standard errors of the ip parameter estimates given in b.
5:     cov(ip × (ip + 1) / 2${\mathbf{ip}}×\left({\mathbf{ip}}+1\right)/2$) – double array
The first ip × (ip + 1) / 2${\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 b(i)${\mathbf{b}}\left(i\right)$ and the parameter estimate given in b(j)${\mathbf{b}}\left(j\right)$, ji$j\ge i$, is stored in cov(j × (j1) / 2 + i)${\mathbf{cov}}\left(j×\left(j-1\right)/2+i\right)$.
6:     res(n) – double array
The (weighted) residuals, ri${r}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
7:     h(n) – double array
The diagonal elements of H$H$, hi${h}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
8:     q(ldq,ip + 1${\mathbf{ip}}+1$) – double array
ldqn$\mathit{ldq}\ge {\mathbf{n}}$.
The results of the QR$QR$ decomposition:
• the first column of q contains c$c$;
• the upper triangular part of columns 2$2$ to ip + 1${\mathbf{ip}}+1$ contain the R$R$ matrix;
• the strictly lower triangular part of columns 2$2$ to ip + 1${\mathbf{ip}}+1$ contain details of the Q$Q$ matrix.
9:     svd – logical scalar
If a singular value decomposition has been performed then svd will be true, otherwise svd will be false.
10:   irank – int64int32nag_int scalar
The rank of the independent variables.
If svd = false${\mathbf{svd}}=\mathbf{false}$, ${\mathbf{irank}}={\mathbf{ip}}$.
If svd = true${\mathbf{svd}}=\mathbf{true}$, irank is an estimate of the rank of the independent variables.
irank is calculated as the number of singular values greater that tol × ${\mathbf{tol}}×\text{}$ (largest singular value). It is possible for the SVD to be carried out but irank to be returned as ip.
11:   p(2 × ip + ip × ip$2×{\mathbf{ip}}+{\mathbf{ip}}×{\mathbf{ip}}$) – double array
Details of the QR$QR$ decomposition and SVD if used.
If svd = false${\mathbf{svd}}=\mathbf{false}$, only the first ip elements of p are used these will contain the zeta values for the QR$QR$ decomposition (see nag_lapack_dgeqrf (f08ae) for details).
If svd = true${\mathbf{svd}}=\mathbf{true}$, the first ip elements of p will contain the zeta values for the QR$QR$ decomposition (see nag_lapack_dgeqrf (f08ae) for details) and the next ip elements of p contain singular values. The following ip by ip elements contain the matrix P*${P}^{*}$ stored by columns.
12:   wk(max (2,5 × (ip1) + ip × ip)$\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(2,5×\left({\mathbf{ip}}-1\right)+{\mathbf{ip}}×{\mathbf{ip}}\right)$) – double array
If on exit svd = true${\mathbf{svd}}=\mathbf{true}$, wk contains information which is needed by nag_correg_linregm_fit_newvar (g02dg); otherwise wk is used as workspace.
13:   ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 2${\mathbf{n}}<2$, or m < 1${\mathbf{m}}<1$, or ldx < n$\mathit{ldx}<{\mathbf{n}}$, or ldq < n$\mathit{ldq}<{\mathbf{n}}$, or tol < 0.0${\mathbf{tol}}<0.0$, or ip ≤ 0${\mathbf{ip}}\le 0$, or ip > n${\mathbf{ip}}>{\mathbf{n}}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, mean ≠ 'M'${\mathbf{mean}}\ne \text{'M'}$ or 'Z'$\text{'Z'}$, or weight ≠ 'W'$\mathit{weight}\ne \text{'W'}$ or 'U'$\text{'U'}$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, weight = 'W'$\mathit{weight}=\text{'W'}$ and a value of wt < 0.0${\mathbf{wt}}<0.0$.
ifail = 4${\mathbf{ifail}}=4$
 On entry, a value of isx < 0${\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.
W ifail = 5${\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$H$, but neither standard errors nor the variance-covariance matrix will be calculated.
ifail = 6${\mathbf{ifail}}=6$
The singular value decomposition has failed to converge, see nag_eigen_real_triang_svd (f02wu). This is an unlikely error.

## Accuracy

The accuracy of nag_correg_linregm_fit (g02da) is closely related to the accuracy of nag_eigen_real_triang_svd (f02wu) and nag_lapack_dgeqrf (f08ae). These function documents should be consulted.

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

## Example

```function nag_correg_linregm_fit_example
mean_p = 'M';
x = [1, 0, 0, 0;
0, 0, 0, 1;
0, 1, 0, 0;
0, 0, 1, 0;
0, 0, 0, 1;
0, 1, 0, 0;
0, 0, 0, 1;
1, 0, 0, 0;
0, 0, 1, 0;
1, 0, 0, 0;
0, 0, 1, 0;
0, 1, 0, 0];
isx = [int64(1);1;1;1];
ip = int64(5);
y = [33.63;
39.62;
38.18;
41.46;
38.02;
35.83;
35.99;
36.58;
42.92;
37.8;
40.43;
37.89];
[rss, idf, b, se, covar, res, h, q, svd, irank, p, wk, ifail] = ...
nag_correg_linregm_fit(mean_p, x, isx, ip, y)
```
```

22.2268

idf =

8

b =

30.5567
5.4467
6.7433
11.0467
7.3200

se =

0.3849
0.8390
0.8390
0.8390
0.8390

covar =

0.1482
0.0370
0.7038
0.0370
-0.2223
0.7038
0.0370
-0.2223
-0.2223
0.7038
0.0370
-0.2223
-0.2223
-0.2223
0.7038

res =

-2.3733
1.7433
0.8800
-0.1433
0.1433
-1.4700
-1.8867
0.5767
1.3167
1.7967
-1.1733
0.5900

h =

0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333

q =

-132.3142   -3.4641   -0.8660   -0.8660   -0.8660   -0.8660
-4.3850    0.2543    1.5000   -0.5000   -0.5000   -0.5000
3.4507    0.2543    0.2464   -1.4142    0.7071    0.7071
-4.5642    0.2543    0.2464   -0.1494   -1.2247    1.2247
0.9125    0.2543    0.2464   -0.1494   -0.2512   -0.0000
-2.1257    0.2543    0.2464    0.4236    0.0476   -0.6063
-1.7393    0.2543    0.2464   -0.1494   -0.2512         0
1.6157    0.2543   -0.3431   -0.0580   -0.0975   -0.3638
1.4622    0.2543    0.2464   -0.1494    0.4137         0
2.8357    0.2543   -0.3431   -0.0580   -0.0975   -0.3638
-1.0278    0.2543    0.2464   -0.1494    0.4137         0
-0.0657    0.2543    0.2464    0.4236    0.0476   -0.6063

svd =

1

irank =

4

p =

1.1352
1.1308
1.2340
1.2280
1.0000
3.8730
1.7321
1.7321
1.7321
0.0000
-0.2309
0
-0.0000
0
0.4472
-0.0577
0.3922
0.3050
0.0562
-0.4472
-0.0577
-0.4154
0.2719
-0.0595
-0.4472
-0.0577
0.0695
-0.2884
-0.4025
-0.4472
-0.0577
-0.0463
-0.2884
0.4058
-0.4472

wk =

1.0000
-0.0000
0.0000
0
0.0000
0
0.7844
0.6038
-0.1418
0.0000
0.0000
0.6100
-0.7924
0.0000
0.0000
0
0.1124
0.0865
0.9899
-0.0000
-0.0000
-0.0000
-0.0000
0.0000
1.0000
-0.2887
-0.4154
0.2719
-0.0595
1.0000
0.5000
0.5774
0.7071
0
-0.8660
-0.8165
-0.7071
0
0
0
0
0
0
0
0

ifail =

0

```
```function g02da_example
mean_p = 'M';
x = [1, 0, 0, 0;
0, 0, 0, 1;
0, 1, 0, 0;
0, 0, 1, 0;
0, 0, 0, 1;
0, 1, 0, 0;
0, 0, 0, 1;
1, 0, 0, 0;
0, 0, 1, 0;
1, 0, 0, 0;
0, 0, 1, 0;
0, 1, 0, 0];
isx = [int64(1);1;1;1];
ip = int64(5);
y = [33.63;
39.62;
38.18;
41.46;
38.02;
35.83;
35.99;
36.58;
42.92;
37.8;
40.43;
37.89];
[rss, idf, b, se, covar, res, h, q, svd, irank, p, wk, ifail] = g02da(mean_p, x, isx, ip, y)
```
```

22.2268

idf =

8

b =

30.5567
5.4467
6.7433
11.0467
7.3200

se =

0.3849
0.8390
0.8390
0.8390
0.8390

covar =

0.1482
0.0370
0.7038
0.0370
-0.2223
0.7038
0.0370
-0.2223
-0.2223
0.7038
0.0370
-0.2223
-0.2223
-0.2223
0.7038

res =

-2.3733
1.7433
0.8800
-0.1433
0.1433
-1.4700
-1.8867
0.5767
1.3167
1.7967
-1.1733
0.5900

h =

0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333
0.3333

q =

-132.3142   -3.4641   -0.8660   -0.8660   -0.8660   -0.8660
-4.3850    0.2543    1.5000   -0.5000   -0.5000   -0.5000
3.4507    0.2543    0.2464   -1.4142    0.7071    0.7071
-4.5642    0.2543    0.2464   -0.1494   -1.2247    1.2247
0.9125    0.2543    0.2464   -0.1494   -0.2512   -0.0000
-2.1257    0.2543    0.2464    0.4236    0.0476   -0.6063
-1.7393    0.2543    0.2464   -0.1494   -0.2512         0
1.6157    0.2543   -0.3431   -0.0580   -0.0975   -0.3638
1.4622    0.2543    0.2464   -0.1494    0.4137         0
2.8357    0.2543   -0.3431   -0.0580   -0.0975   -0.3638
-1.0278    0.2543    0.2464   -0.1494    0.4137         0
-0.0657    0.2543    0.2464    0.4236    0.0476   -0.6063

svd =

1

irank =

4

p =

1.1352
1.1308
1.2340
1.2280
1.0000
3.8730
1.7321
1.7321
1.7321
0.0000
-0.2309
0
-0.0000
0
0.4472
-0.0577
0.3922
0.3050
0.0562
-0.4472
-0.0577
-0.4154
0.2719
-0.0595
-0.4472
-0.0577
0.0695
-0.2884
-0.4025
-0.4472
-0.0577
-0.0463
-0.2884
0.4058
-0.4472

wk =

1.0000
-0.0000
0.0000
0
0.0000
0
0.7844
0.6038
-0.1418
0.0000
0.0000
0.6100
-0.7924
0.0000
0.0000
0
0.1124
0.0865
0.9899
-0.0000
-0.0000
-0.0000
-0.0000
0.0000
1.0000
-0.2887
-0.4154
0.2719
-0.0595
1.0000
0.5000
0.5774
0.7071
0
-0.8660
-0.8165
-0.7071
0
0
0
0
0
0
0
0

ifail =

0

```