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_newvar (g02dg)

## Purpose

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

## Syntax

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

## Description

nag_correg_linregm_fit_newvar (g02dg) uses the results given by nag_correg_linregm_fit (g02da) to fit the same set of independent variables to a new dependent variable.
nag_correg_linregm_fit (g02da) computes a QR$QR$ decomposition of the matrix of p$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$QR$ decomposition leads to the formation of an upper triangular p$p$ by p$p$ matrix R$R$ and an n$n$ by n$n$ orthogonal matrix Q$Q$. In addition the vector c = QTy$c={Q}^{\mathrm{T}}y$ (or QTW1 / 2y${Q}^{\mathrm{T}}{W}^{1/2}y$) is computed. For a new dependent variable, ynew${y}_{\mathrm{new}}$, nag_correg_linregm_fit_newvar (g02dg) computes a new value of c = QTynew$c={Q}^{\mathrm{T}}{y}_{\text{new}}$ or QTW1 / 2ynew${Q}^{\mathrm{T}}{W}^{1/2}{y}_{\text{new}}$.
If R$R$ is of full rank, then the least squares parameter estimates, β̂$\stackrel{^}{\beta }$, are the solution to
 Rβ̂ = c1, $Rβ^=c1,$
where c1${c}_{1}$ is the first p$p$ elements of c$c$.
If R$R$ is not of full rank, then nag_correg_linregm_fit (g02da) will have computed an 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 − 1 Q*1T c1, $β^=P1D-1 Q*1T c1,$
P1${P}_{1}$ being the first k$k$ columns of P$P$, i.e., P = (P1P0)$P=\left({P}_{1}{P}_{0}\right)$, and Q*1${Q}_{{*}_{1}}$ being the first k$k$ columns of Q*${Q}_{*}$. Details of the SVD are made available by nag_correg_linregm_fit (g02da) in the form of the matrix P*${P}^{*}$:
P* =
 ( D − 1 P1T ) P0T
.
$P*= D-1 P1T P0T .$
The matrix Q*${Q}_{*}$ is made available through the workspace of nag_correg_linregm_fit (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

## Parameters

### Compulsory Input Parameters

The residual sum of squares for the original dependent variable.
Constraint: rss > 0.0${\mathbf{rss}}>0.0$.
2:     ip – int64int32nag_int scalar
p$p$, the number of independent variables (including the mean if fitted).
Constraint: 1ipn$1\le {\mathbf{ip}}\le {\mathbf{n}}$.
3:     irank – int64int32nag_int scalar
The rank of the independent variables, as given by nag_correg_linregm_fit (g02da).
Constraint: irank > 0${\mathbf{irank}}>0$, and if svd = false${\mathbf{svd}}=\mathbf{false}$, then ${\mathbf{irank}}={\mathbf{ip}}$, else ${\mathbf{irank}}\le {\mathbf{ip}}$.
4:     cov(ip × (ip + 1) / 2${\mathbf{ip}}×\left({\mathbf{ip}}+1\right)/2$) – double array
The covariance matrix of the parameter estimates as given by nag_correg_linregm_fit (g02da).
5:     q(ldq,ip + 1${\mathbf{ip}}+1$) – double array
ldq, the first dimension of the array, must satisfy the constraint ldqn$\mathit{ldq}\ge {\mathbf{n}}$.
The results of the QR$QR$ decomposition as returned by nag_correg_linregm_fit (g02da).
6:     svd – logical scalar
Indicates if a singular value decomposition was used by nag_correg_linregm_fit (g02da).
svd = true${\mathbf{svd}}=\mathbf{true}$
A singular value decomposition was used by nag_correg_linregm_fit (g02da).
svd = false${\mathbf{svd}}=\mathbf{false}$
A singular value decomposition was not used by nag_correg_linregm_fit (g02da).
7:     p( : $:$) – double array
Note: the dimension of the array p must be at least ip${\mathbf{ip}}$ if svd = false${\mathbf{svd}}=\mathbf{false}$, and at least ip × ip + 2 × ip${\mathbf{ip}}×{\mathbf{ip}}+2×{\mathbf{ip}}$ otherwise.
Details of the QR$QR$ decomposition and SVD, if used, as returned in array p by nag_correg_linregm_fit (g02da).
If svd = false${\mathbf{svd}}=\mathbf{false}$, only the first ip elements of p are used; these 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 contain the zeta values for the QR$QR$ decomposition (see nag_lapack_dgeqrf (f08ae) for details) and the next ip × ip + ip${\mathbf{ip}}×{\mathbf{ip}}+{\mathbf{ip}}$ elements of p contain details of the singular value decomposition.
8:     y(n) – double array
n, the dimension of the array, must satisfy the constraint nip${\mathbf{n}}\ge {\mathbf{ip}}$.
The new dependent variable, ynew${y}_{\text{new}}$.
9:     wk(5 × (ip1) + ip × ip$5×\left({\mathbf{ip}}-1\right)+{\mathbf{ip}}×{\mathbf{ip}}$) – double array
If svd = true${\mathbf{svd}}=\mathbf{true}$, wk must be unaltered from the previous call to nag_correg_linregm_fit (g02da) or nag_correg_linregm_fit_newvar (g02dg).
If svd = false${\mathbf{svd}}=\mathbf{false}$, wk is used as workspace.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array y and the first dimension of the array q. (An error is raised if these dimensions are not equal.)
n$n$, the number of observations.
Constraint: nip${\mathbf{n}}\ge {\mathbf{ip}}$.
2:     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 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.
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$.

weight ldq

### Output Parameters

The residual sum of squares for the new dependent variable.
2:     cov(ip × (ip + 1) / 2${\mathbf{ip}}×\left({\mathbf{ip}}+1\right)/2$) – double array
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(\left(j×\left(j-1\right)/2+i\right)\right)$.
3:     q(ldq,ip + 1${\mathbf{ip}}+1$) – double array
ldqn$\mathit{ldq}\ge {\mathbf{n}}$.
The first column of q contains the new values of c$c$, the remainder of q will be unchanged.
4:     b(ip) – double array
The least squares estimates of the parameters of the regression model, β̂$\stackrel{^}{\beta }$.
5:     se(ip) – double array
The standard error of the estimates of the parameters.
6:     res(n) – double array
The residuals for the new regression model.
7:     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:
ifail = 1${\mathbf{ifail}}=1$
 On entry, ip < 1${\mathbf{ip}}<1$, or n < ip${\mathbf{n}}<{\mathbf{ip}}$, or irank ≤ 0${\mathbf{irank}}\le 0$, or svd = false${\mathbf{svd}}=\mathbf{false}$ and ${\mathbf{irank}}\ne {\mathbf{ip}}$, or svd = true${\mathbf{svd}}=\mathbf{true}$ and ${\mathbf{irank}}>{\mathbf{ip}}$, or ldq < n$\mathit{ldq}<{\mathbf{n}}$, or rss ≤ 0.0${\mathbf{rss}}\le 0.0$, or weight ≠ 'U'$\mathit{weight}\ne \text{'U'}$ or 'W'$\text{'W'}$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, weight = 'W'$\mathit{weight}=\text{'W'}$ and a value of wt < 0.0${\mathbf{wt}}<0.0$.

## Accuracy

The same accuracy as nag_correg_linregm_fit (g02da) is obtained.

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

## Example

```function nag_correg_linregm_fit_newvar_example
ip = int64(5);
irank = int64(4);
covar = [0.1481786666666657;
0.03704466666666638;
0.7038486666666622;
0.03704466666666652;
-0.222268;
0.7038486666666626;
0.03704466666666648;
-0.222268;
-0.222268;
0.7038486666666621;
0.03704466666666641;
-0.222268;
-0.222268;
-0.222268;
0.7038486666666622];
q = [-132.3142479415325, -3.464101615137754, -0.866025403784439, ...
-0.8660254037844387, -0.8660254037844387, -0.8660254037844387;
-4.385, 0.2542949013547401, 1.5, -0.5, -0.5, -0.5;
3.450681092190354, 0.2542949013547401, 0.2464408878569322, ...
-1.414213562373095, 0.7071067811865476, 0.7071067811865477;
-4.564215887385989, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, -1.224744871391589, 1.224744871391589;
-0.5165745490611717, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, -0.2512250416211346, 3.684598669906146e-16;
-1.786105166371516, 0.2542949013547401, 0.2464408878569322, ...
0.4236386677182777, 0.04757984926531376, 0.05853513115087475;
-2.040746763558067, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, -0.2512250416211346, 0.4717748148066523;
1.699599417218472, 0.2542949013547401, -0.3431208422711533, ...
-0.05795805473930137, -0.09747415577321789, 0.2226984422646312;
1.338178326423549, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, 0.4136728082784363, 0.1941369084874573;
2.919599417218471, 0.2542949013547401, -0.3431208422711533, ...
-0.05795805473930137, -0.09747415577321789, 0.2226984422646312;
-1.151821673576453, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, 0.4136728082784363, 0.1941369084874573;
0.2738948336284861, 0.2542949013547401, 0.2464408878569322, ...
0.4236386677182777, 0.04757984926531376, 0.05853513115087475];
svd = true;
p = [1.135198279858991;
1.13078348304906;
1.234006904805161;
1.228003039942231;
1.263332232672042;
3.872983346207417;
1.732050807568878;
1.732050807568878;
1.732050807568877;
1.64780261914309e-16;
0.2309401076758503;
8.35089227579159e-18;
-1.238223776129407e-17;
7.900875770685859e-17;
-0.447213595499958;
0.0577350269189626;
0.2871305767251709;
0.2022129403845741;
-0.3559016137228031;
0.447213595499958;
0.05773502691896256;
0.03726461792122844;
0.271286303307133;
0.4183480487454616;
0.447213595499958;
0.05773502691896257;
0.1515575558927682;
-0.4677758485215264;
0.0906424999201606;
0.447213595499958;
0.05773502691896258;
-0.4759527505391677;
-0.005723395170180857;
-0.153088934942819;
0.4472135954999578];
y = [63;
69;
68;
71;
68;
65;
65;
66;
72;
67;
70;
67];
wk = [-1;
4.888782090057172e-17;
3.783865581974411e-17;
3.205148747669446e-17;
2.127304033926049e-17;
-7.232084855102773e-18;
0.574261153450342;
-0.282082169979652;
-0.7685400295478783;
-1.766548161010173e-16;
1.072333245697965e-17;
0.4044258807691484;
-0.7184712955241166;
0.5658963725559621;
-3.152388336389935e-17;
-6.842359129558908e-17;
-0.7118032274456063;
-0.635789781993599;
-0.2985088248453474;
7.896994727506343e-17;
2.12730403392605e-17;
1.57267977670022e-16;
-8.430959034105682e-17;
-1.677854456096144e-16;
1;
0.2886751345948128;
0.03726461792122845;
0.271286303307133;
0.4183480487454616;
5;
3.25;
5.25;
4.25;
5.25;
6.070050924028785e-16;
0.7071067811865474;
-0.5700260616371526;
0.9995120760870788;
1;
0.7071067811865475;
0.8216266117004956;
0.03123475237772124;
5.777618349603308e-32;
-0.7071067811865476;
-0.5700260616371526];
[rssOut, covarOut, qOut, b, se, res, ifail] = ...
nag_correg_linregm_fit_newvar(rss, ip, irank, covar, q, svd, p, y, wk)
```
```

24.0000

covarOut =

0.1600
0.0400
0.7600
0.0400
-0.2400
0.7600
0.0400
-0.2400
-0.2400
0.7600
0.0400
-0.2400
-0.2400
-0.2400
0.7600

qOut =

-234.1155   -3.4641   -0.8660   -0.8660   -0.8660   -0.8660
-4.5000    0.2543    1.5000   -0.5000   -0.5000   -0.5000
3.5355    0.2543    0.2464   -1.4142    0.7071    0.7071
-4.4907    0.2543    0.2464   -0.1494   -1.2247    1.2247
-0.5880    0.2543    0.2464   -0.1494   -0.2512    0.0000
-2.1727    0.2543    0.2464    0.4236    0.0476    0.0585
-2.6415    0.2543    0.2464   -0.1494   -0.2512    0.4718
1.6690    0.2543   -0.3431   -0.0580   -0.0975    0.2227
0.9087    0.2543    0.2464   -0.1494    0.4137    0.1941
2.6690    0.2543   -0.3431   -0.0580   -0.0975    0.2227
-1.0913    0.2543    0.2464   -0.1494    0.4137    0.1941
-0.1727    0.2543    0.2464    0.4236    0.0476    0.0585

b =

54.0667
11.2667
12.6000
16.9333
13.2667

se =

0.4000
0.8718
0.8718
0.8718
0.8718

res =

-2.3333
1.6667
1.3333
0.0000
0.6667
-1.6667
-2.3333
0.6667
1.0000
1.6667
-1.0000
0.3333

ifail =

0

```
```function g02dg_example
ip = int64(5);
irank = int64(4);
covar = [0.1481786666666657;
0.03704466666666638;
0.7038486666666622;
0.03704466666666652;
-0.222268;
0.7038486666666626;
0.03704466666666648;
-0.222268;
-0.222268;
0.7038486666666621;
0.03704466666666641;
-0.222268;
-0.222268;
-0.222268;
0.7038486666666622];
q = [-132.3142479415325, -3.464101615137754, -0.866025403784439, ...
-0.8660254037844387, -0.8660254037844387, -0.8660254037844387;
-4.385, 0.2542949013547401, 1.5, -0.5, -0.5, -0.5;
3.450681092190354, 0.2542949013547401, 0.2464408878569322, ...
-1.414213562373095, 0.7071067811865476, 0.7071067811865477;
-4.564215887385989, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, -1.224744871391589, 1.224744871391589;
-0.5165745490611717, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, -0.2512250416211346, 3.684598669906146e-16;
-1.786105166371516, 0.2542949013547401, 0.2464408878569322, ...
0.4236386677182777, 0.04757984926531376, 0.05853513115087475;
-2.040746763558067, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, -0.2512250416211346, 0.4717748148066523;
1.699599417218472, 0.2542949013547401, -0.3431208422711533, ...
-0.05795805473930137, -0.09747415577321789, 0.2226984422646312;
1.338178326423549, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, 0.4136728082784363, 0.1941369084874573;
2.919599417218471, 0.2542949013547401, -0.3431208422711533, ...
-0.05795805473930137, -0.09747415577321789, 0.2226984422646312;
-1.151821673576453, 0.2542949013547401, 0.2464408878569322, ...
-0.1493782079840455, 0.4136728082784363, 0.1941369084874573;
0.2738948336284861, 0.2542949013547401, 0.2464408878569322, ...
0.4236386677182777, 0.04757984926531376, 0.05853513115087475];
svd = true;
p = [1.135198279858991;
1.13078348304906;
1.234006904805161;
1.228003039942231;
1.263332232672042;
3.872983346207417;
1.732050807568878;
1.732050807568878;
1.732050807568877;
1.64780261914309e-16;
0.2309401076758503;
8.35089227579159e-18;
-1.238223776129407e-17;
7.900875770685859e-17;
-0.447213595499958;
0.0577350269189626;
0.2871305767251709;
0.2022129403845741;
-0.3559016137228031;
0.447213595499958;
0.05773502691896256;
0.03726461792122844;
0.271286303307133;
0.4183480487454616;
0.447213595499958;
0.05773502691896257;
0.1515575558927682;
-0.4677758485215264;
0.0906424999201606;
0.447213595499958;
0.05773502691896258;
-0.4759527505391677;
-0.005723395170180857;
-0.153088934942819;
0.4472135954999578];
y = [63;
69;
68;
71;
68;
65;
65;
66;
72;
67;
70;
67];
wk = [-1;
4.888782090057172e-17;
3.783865581974411e-17;
3.205148747669446e-17;
2.127304033926049e-17;
-7.232084855102773e-18;
0.574261153450342;
-0.282082169979652;
-0.7685400295478783;
-1.766548161010173e-16;
1.072333245697965e-17;
0.4044258807691484;
-0.7184712955241166;
0.5658963725559621;
-3.152388336389935e-17;
-6.842359129558908e-17;
-0.7118032274456063;
-0.635789781993599;
-0.2985088248453474;
7.896994727506343e-17;
2.12730403392605e-17;
1.57267977670022e-16;
-8.430959034105682e-17;
-1.677854456096144e-16;
1;
0.2886751345948128;
0.03726461792122845;
0.271286303307133;
0.4183480487454616;
5;
3.25;
5.25;
4.25;
5.25;
6.070050924028785e-16;
0.7071067811865474;
-0.5700260616371526;
0.9995120760870788;
1;
0.7071067811865475;
0.8216266117004956;
0.03123475237772124;
5.777618349603308e-32;
-0.7071067811865476;
-0.5700260616371526];
[rssOut, covarOut, qOut, b, se, res, ifail] = ...
g02dg(rss, ip, irank, covar, q, svd, p, y, wk)
```
```

24.0000

covarOut =

0.1600
0.0400
0.7600
0.0400
-0.2400
0.7600
0.0400
-0.2400
-0.2400
0.7600
0.0400
-0.2400
-0.2400
-0.2400
0.7600

qOut =

-234.1155   -3.4641   -0.8660   -0.8660   -0.8660   -0.8660
-4.5000    0.2543    1.5000   -0.5000   -0.5000   -0.5000
3.5355    0.2543    0.2464   -1.4142    0.7071    0.7071
-4.4907    0.2543    0.2464   -0.1494   -1.2247    1.2247
-0.5880    0.2543    0.2464   -0.1494   -0.2512    0.0000
-2.1727    0.2543    0.2464    0.4236    0.0476    0.0585
-2.6415    0.2543    0.2464   -0.1494   -0.2512    0.4718
1.6690    0.2543   -0.3431   -0.0580   -0.0975    0.2227
0.9087    0.2543    0.2464   -0.1494    0.4137    0.1941
2.6690    0.2543   -0.3431   -0.0580   -0.0975    0.2227
-1.0913    0.2543    0.2464   -0.1494    0.4137    0.1941
-0.1727    0.2543    0.2464    0.4236    0.0476    0.0585

b =

54.0667
11.2667
12.6000
16.9333
13.2667

se =

0.4000
0.8718
0.8718
0.8718
0.8718

res =

-2.3333
1.6667
1.3333
0.0000
0.6667
-1.6667
-2.3333
0.6667
1.0000
1.6667
-1.0000
0.3333

ifail =

0

```