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_obs_edit (g02dc)

## Purpose

nag_correg_linregm_obs_edit (g02dc) adds or deletes an observation from a general regression model fitted by nag_correg_linregm_fit (g02da).

## Syntax

[q, rss, ifail] = g02dc(update, mean, isx, q, x, ix, y, rss, 'm', m, 'ip', ip, 'wt', wt)
[q, rss, ifail] = nag_correg_linregm_obs_edit(update, mean, isx, q, x, ix, y, rss, 'm', m, 'ip', ip, 'wt', wt)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: ip has been made optional
Mark 23: weight dropped from interface, wt now optional
.

## Description

nag_correg_linregm_fit (g02da) fits a general linear regression model to a dataset. You may wish to change the model by either adding or deleting an observation from the dataset. nag_correg_linregm_obs_edit (g02dc) takes the results from nag_correg_linregm_fit (g02da) and makes the required changes to the vector c$c$ and the upper triangular matrix R$R$ produced by nag_correg_linregm_fit (g02da). The regression coefficients, standard errors and the variance-covariance matrix of the regression coefficients can be obtained from nag_correg_linregm_update (g02dd) after all required changes to the dataset have been made.
nag_correg_linregm_fit (g02da) performs a QR$QR$ decomposition on the (weighted) X$X$ matrix of independent variables. To add a new observation to a model with p$p$ parameters, the upper triangular matrix R$R$ and vector c1${c}_{1}$ (the first p$p$ elements of c$c$) are augmented by the new observation on independent variables in xT${x}^{\mathrm{T}}$ and dependent variable ynew${y}_{\text{new}}$. Givens rotations are then used to restore the upper triangular form.
 ( R : c1 ) x : ynew
 ( R* : c1 * ) 0 : ynew *
.
$R:c1 x:ynew → R*:c1* 0:ynew* .$
Note:  only R$R$ and the upper part of c$c$ are updated the remainder of the Q$Q$ matrix is unchanged.

## 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

## Parameters

### Compulsory Input Parameters

1:     update – string (length ≥ 1)
Indicates if an observation is to be added or deleted.
update = 'A'${\mathbf{update}}=\text{'A'}$
update = 'D'${\mathbf{update}}=\text{'D'}$
The observation is deleted.
Constraint: update = 'A'${\mathbf{update}}=\text{'A'}$ or 'D'$\text{'D'}$.
2:     mean – string (length ≥ 1)
Indicates if a mean has been used in the model.
mean = 'M'${\mathbf{mean}}=\text{'M'}$
A mean term or intercept will have been included in the model by nag_correg_linregm_fit (g02da).
mean = 'Z'${\mathbf{mean}}=\text{'Z'}$
A model with no mean term or intercept will have been fitted by nag_correg_linregm_fit (g02da).
Constraint: mean = 'M'${\mathbf{mean}}=\text{'M'}$ or 'Z'$\text{'Z'}$.
3:     isx(m) – int64int32nag_int array
m, the dimension of the array, must satisfy the constraint m1${\mathbf{m}}\ge 1$.
If isx(j)${\mathbf{isx}}\left(\mathit{j}\right)$ is greater than 0$0$, the value contained in x((j1) × ix + 1)${\mathbf{x}}\left(\left(\mathit{j}-1\right)×{\mathbf{ix}}+1\right)$ is to be included as a value of xT${x}^{\mathrm{T}}$, for j = 1,2,,m$\mathit{j}=1,2,\dots ,{\mathbf{m}}$.
Constraint: if mean = 'M'${\mathbf{mean}}=\text{'M'}$, exactly ip1${\mathbf{ip}}-1$ elements of isx must be > 0$\text{}>0$ and if mean = 'Z'${\mathbf{mean}}=\text{'Z'}$, exactly ip elements of isx must be > 0$\text{}>0$.
4:     q(ldq,ip + 1${\mathbf{ip}}+1$) – double array
ldq, the first dimension of the array, must satisfy the constraint ldqip$\mathit{ldq}\ge {\mathbf{ip}}$.
Must be array q as output by nag_correg_linregm_fit (g02da), nag_correg_linregm_var_add (g02de), nag_correg_linregm_var_del (g02df) or nag_correg_linregm_fit_onestep (g02ee), or a previous call to nag_correg_linregm_obs_edit (g02dc).
5:     x( : $:$) – double array
Note: the dimension of the array x must be at least (m1) × ix + 1 $\left({\mathbf{m}}-1\right)×{\mathbf{ix}}+1$.
The ip values for the dependent variables of the new observation, xT${x}^{\mathrm{T}}$. The positions will depend on the value of ix.
6:     ix – int64int32nag_int scalar
The increment for elements of x. Two situations are common:
ix = 1${\mathbf{ix}}=1$
The values of x$x$ are to be chosen from consecutive locations in x, i.e., x(1),x(2),,x(m)${\mathbf{x}}\left(1\right),{\mathbf{x}}\left(2\right),\dots ,{\mathbf{x}}\left({\mathbf{m}}\right)$.
${\mathbf{ix}}={\mathbf{ldx}}$
The values of x$x$ are to be chosen from a row of a two-dimensional array with first dimension ldx, i.e., x(1),x(ldx + 1),,x((m1)ldx + 1)${\mathbf{x}}\left(1\right),{\mathbf{x}}\left({\mathbf{ldx}}+1\right),\dots ,{\mathbf{x}}\left(\left({\mathbf{m}}-1\right){\mathbf{ldx}}+1\right)$.
Constraint: ix1${\mathbf{ix}}\ge 1$.
7:     y – double scalar
The value of the dependent variable for the new observation, ynew${y}_{\text{new}}$.
The value of the residual sums of squares for the original set of observations.
Constraint: rss0.0${\mathbf{rss}}\ge 0.0$.

### Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The dimension of the array isx.
m$m$, the total number of independent variables in the dataset.
Constraint: m1${\mathbf{m}}\ge 1$.
2:     ip – int64int32nag_int scalar
Default: The first dimension of the array q.
The number of linear terms in general linear regression model (including mean if there is one).
Constraint: ip1${\mathbf{ip}}\ge 1$.
3:     wt – double scalar
If provided , wt must contain the weight to be used with the new observation.
If wt = 0.0${\mathbf{wt}}=0.0$, the observation is not included in the model.
Default: 0$0$
Constraint: if wt0.0${\mathbf{wt}}\ge 0.0$, weight = 'W'$\mathit{weight}=\text{'W'}$.

weight ldq wk

### Output Parameters

1:     q(ldq,ip + 1${\mathbf{ip}}+1$) – double array
ldqip$\mathit{ldq}\ge {\mathbf{ip}}$.
The first ip elements of the first column of q will contain c1 * ${c}_{1}^{*}$ the upper triangular part of columns 2$2$ to ip + 1${\mathbf{ip}}+1$ will contain R*${R}^{*}$ the remainder is unchanged.
The updated values of the residual sums of squares.
Note:  this will only be valid if the model is of full rank.
3:     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 ldq < ip$\mathit{ldq}<{\mathbf{ip}}$, or m < 1${\mathbf{m}}<1$, or ix < 1${\mathbf{ix}}<1$, or rss < 0.0${\mathbf{rss}}<0.0$, or update ≠ 'A'${\mathbf{update}}\ne \text{'A'}$ or 'D'$\text{'D'}$, or mean ≠ 'M'${\mathbf{mean}}\ne \text{'M'}$ or 'Z'$\text{'Z'}$, or weight ≠ 'U'$\mathit{weight}\ne \text{'U'}$ or 'W'$\text{'W'}$, or mean = 'M'${\mathbf{mean}}=\text{'M'}$ and there are not exactly ip − 1${\mathbf{ip}}-1$ nonzero values of isx, or mean = 'Z'${\mathbf{mean}}=\text{'Z'}$ and there are not exactly ip nonzero values of isx,
ifail = 2${\mathbf{ifail}}=2$
 On entry, weight = 'W'$\mathit{weight}=\text{'W'}$ and wt < 0.0${\mathbf{wt}}<0.0$.
ifail = 3${\mathbf{ifail}}=3$
The R$R$ matrix could not be updated. This may occur if an attempt is made to delete an observation which was not in the original dataset or to add an observation to a R$R$ matrix with a zero diagonal element. This error is also possible when removing an observation which reduces the rank of design matrix. In such cases the model should be recomputed using nag_correg_linregm_fit (g02da).
ifail = 4${\mathbf{ifail}}=4$
The residual sums of squares cannot be updated. This will occur if the input residual sum of squares is less than the calculated decrease in residual sum of squares when the new observation is deleted.

## Accuracy

Higher accuracy is achieved by updating the R$R$ matrix rather than the traditional methods of updating XX${X}^{\prime }X$.

Care should be taken with the use of nag_correg_linregm_obs_edit (g02dc).
 (a) It is possible to delete observations which were not included in the original model. (b) If several additions/deletions have been performed you are advised to recompute the regression using nag_correg_linregm_fit (g02da). (c) Adding or deleting observations can alter the rank of the model. Such changes will only be detected when a call to nag_correg_linregm_update (g02dd) has been made. nag_correg_linregm_update (g02dd) should also be used to compute the new residual sum of squares when the model is not of full rank.
nag_correg_linregm_obs_edit (g02dc) may also be used after nag_correg_linregm_var_add (g02de), nag_correg_linregm_var_del (g02df) and nag_correg_linregm_fit_onestep (g02ee)

## Example

function nag_correg_linregm_obs_edit_example
update = 'D';
mean_p = 'Z';
isx = [int64(1);1;1;1];
q = [-72.95, -2, -0.5, -0.5, -0.5;
45.48298632955565, 0, 1.6583123951777, 0.4522670168666454, 0.4522670168666454;
56.11154475279979, 0, -0.6030226891555274, 1.882937743382544, 0.2896827297511605;
-42.04198673812751, 0, -0, -0.4963538360840667, -1.860521018838127];
x = [1; 1; 1; 1];
ix = int64(1);
y = 37.89;
[qOut, rssOut, ifail] = nag_correg_linregm_obs_edit(update, mean_p, isx, q, x, ix, y, rss)

qOut =

-62.3596   -1.7321    0.0000   -0.0000    0.0000
52.3330         0    1.4142    0.0000   -0.0000
72.0591         0   -0.6030    1.7321    0.0000
-65.6043         0         0   -0.4964   -1.7321

21.7046

ifail =

0

function g02dc_example
update = 'D';
mean_p = 'Z';
isx = [int64(1);1;1;1];
q = [-72.95, -2, -0.5, -0.5, -0.5;
45.48298632955565, 0, 1.6583123951777, 0.4522670168666454, 0.4522670168666454;
56.11154475279979, 0, -0.6030226891555274, 1.882937743382544, 0.2896827297511605;
-42.04198673812751, 0, -0, -0.4963538360840667, -1.860521018838127];
x = [1; 1; 1; 1];
ix = int64(1);
y = 37.89;
[qOut, rssOut, ifail] = g02dc(update, mean_p, isx, q, x, ix, y, rss)

qOut =

-62.3596   -1.7321    0.0000   -0.0000    0.0000
52.3330         0    1.4142    0.0000   -0.0000
72.0591         0   -0.6030    1.7321    0.0000
-65.6043         0         0   -0.4964   -1.7321

21.7046

ifail =

0