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_linregs_noconst_miss (g02cd)

Purpose

nag_correg_linregs_noconst_miss (g02cd) performs a simple linear regression with no constant, with dependent variable $y$ and independent variable $x$, omitting cases involving missing values.

Syntax

[result, ifail] = g02cd(x, y, xmiss, ymiss, 'n', n)
[result, ifail] = nag_correg_linregs_noconst_miss(x, y, xmiss, ymiss, 'n', n)

Description

nag_correg_linregs_noconst_miss (g02cd) fits a straight line of the form
 $y=bx$
to those of the data points
 $x1,y1,x2,y2,…,xn,yn$
that do not include missing values, such that
 $yi=bxi+ei$
for those $\left({x}_{i},{y}_{i}\right)$, for $i=1,2,\dots ,n\text{ }\left(n\ge 2\right)$ which do not include missing values.
The function eliminates all pairs of observations $\left({x}_{i},{y}_{i}\right)$ which contain a missing value for either $x$ or $y$, and then calculates the regression coefficient, $b$, and various other statistical quantities by minimizing the sum of the ${e}_{i}^{2}$ over those cases remaining in the calculations.
The input data consists of the $n$ pairs of observations $\left({x}_{1},{y}_{1}\right),\left({x}_{2},{y}_{2}\right),\dots ,\left({x}_{n},{y}_{n}\right)$ on the independent variable $x$ and the dependent variable $y$.
In addition two values, $\mathit{xm}$ and $\mathit{ym}$, are given which are considered to represent missing observations for $x$ and $y$ respectively. (See Accuracy).
Let ${w}_{\mathit{i}}=0$, if the $\mathit{i}$th observation of either $x$ or $y$ is missing, i.e., if ${x}_{\mathit{i}}=\mathit{xm}$ and/or ${y}_{\mathit{i}}=\mathit{ym}$; and ${w}_{\mathit{i}}=1$ otherwise, for $\mathit{i}=1,2,\dots ,n$.
The quantities calculated are:
(a) Means:
 $x-=∑i=1nwixi ∑i=1nwi ; y-=∑i=1nwiyi ∑i=1nwi .$
(b) Standard deviations:
 $sx=∑i= 1nwi xi-x- 2 ∑i= 1nwi- 1 ; sy=∑i= 1nwi yi-y- 2 ∑i= 1nwi- 1 .$
(c) Pearson product-moment correlation coefficient:
 $r=∑i=1nwixi-x-yi-y- ∑i=1nwi xi-x- 2∑i=1nwi yi-y- 2 .$
(d) The regression coefficient, $b$:
 $b=∑i=1nwixiyi ∑i=1nwixi2 .$
(e) The sum of squares attributable to the regression, $SSR$, the sum of squares of deviations about the regression, $SSD$, and the total sum of squares, $SST$:
 $SST=∑i=1nwiyi2; SSD=∑i=1nwi yi-bxi 2; SSR=SST-SSD.$
(f) The degrees of freedom attributable to the regression, $DFR$, the degrees of freedom of deviations about the regression, $DFD$, and the total degrees of freedom, $DFT$:
 $DFT=∑i=1nwi; DFD=∑i=1nwi-1; DFR=1.$
(g) The mean square attributable to the regression, $MSR$, and the mean square of deviations about the regression, $MSD$:
 $MSR=SSR/DFR; MSD=SSD/DFD.$
(h) The $F$ value for the analysis of variance:
 $F=MSR/MSD.$
(i) The standard error of the regression coefficient:
 $seb=MSD ∑i= 1nwixi2 .$
(j) The $t$ value for the regression coefficient:
 $tb=bseb .$
(k) The number of observations used in the calculations:
 $nc=∑i= 1nwi.$

References

Draper N R and Smith H (1985) Applied Regression Analysis (2nd Edition) Wiley

Parameters

Compulsory Input Parameters

1:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
${\mathbf{x}}\left(\mathit{i}\right)$ must contain ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
2:     $\mathrm{y}\left({\mathbf{n}}\right)$ – double array
${\mathbf{y}}\left(\mathit{i}\right)$ must contain ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     $\mathrm{xmiss}$ – double scalar
The value $xm$, which is to be taken as the missing value for the variable $x$ (see Accuracy).
4:     $\mathrm{ymiss}$ – double scalar
The value $ym$, which is to be taken as the missing value for the variable $y$ (see Accuracy).

Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the arrays x, y. (An error is raised if these dimensions are not equal.)
$n$, the number of pairs of observations.
Constraint: ${\mathbf{n}}\ge 2$.

Output Parameters

1:     $\mathrm{result}\left(21\right)$ – double array
The following information:
 ${\mathbf{result}}\left(1\right)$ $\stackrel{-}{x}$, the mean value of the independent variable, $x$; ${\mathbf{result}}\left(2\right)$ $\stackrel{-}{y}$, the mean value of the dependent variable, $y$; ${\mathbf{result}}\left(3\right)$ ${s}_{x}$, the standard deviation of the independent variable, $x$; ${\mathbf{result}}\left(4\right)$ ${s}_{y}$, the standard deviation of the dependent variable, $y$; ${\mathbf{result}}\left(5\right)$ $r$, the Pearson product-moment correlation between the independent variable $x$ and the dependent variable, $y$; ${\mathbf{result}}\left(6\right)$ $b$, the regression coefficient; ${\mathbf{result}}\left(7\right)$ the value $0.0$; ${\mathbf{result}}\left(8\right)$ $se\left(b\right)$, the standard error of the regression coefficient; ${\mathbf{result}}\left(9\right)$ the value $0.0$; ${\mathbf{result}}\left(10\right)$ $t\left(b\right)$, the $t$ value for the regression coefficient; ${\mathbf{result}}\left(11\right)$ the value $0.0$; ${\mathbf{result}}\left(12\right)$ $SSR$, the sum of squares attributable to the regression; ${\mathbf{result}}\left(13\right)$ $DFR$, the degrees of freedom attributable to the regression; ${\mathbf{result}}\left(14\right)$ $MSR$, the mean square attributable to the regression; ${\mathbf{result}}\left(15\right)$ $F$, the $F$ value for the analysis of variance; ${\mathbf{result}}\left(16\right)$ $SSD$, the sum of squares of deviations about the regression; ${\mathbf{result}}\left(17\right)$ $DFD$, the degrees of freedom of deviations about the regression; ${\mathbf{result}}\left(18\right)$ $MSD$, the mean square of deviations about the regression; ${\mathbf{result}}\left(19\right)$ $SST$, the total sum of squares ${\mathbf{result}}\left(20\right)$ $DFT$, the total degrees of freedom; ${\mathbf{result}}\left(21\right)$ ${n}_{c}$, the number of observations used in the calculations.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$.
${\mathbf{ifail}}=2$
After observations with missing values were omitted, fewer than two cases remained.
${\mathbf{ifail}}=3$
After observations with missing values were omitted, all remaining values of at least one of the variables $x$ and $y$ were identical.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

Accuracy

nag_correg_linregs_noconst_miss (g02cd) does not use additional precision arithmetic for the accumulation of scalar products, so there may be a loss of significant figures for large $n$.
You are warned of the need to exercise extreme care in your selection of missing values. nag_correg_linregs_noconst_miss (g02cd) treats all values in the inclusive range $\left(1±{0.1}^{\left({\mathbf{x02be}}-2\right)}\right)×{xm}_{j}$, where ${\mathit{xm}}_{j}$ is the missing value for variable $j$ specified in xmiss.
You must therefore ensure that the missing value chosen for each variable is sufficiently different from all valid values for that variable so that none of the valid values fall within the range indicated above.
If, in calculating $F$ or $t\left(b\right)$  (see Description), the numbers involved are such that the result would be outside the range of numbers which can be stored by the machine, then the answer is set to the largest quantity which can be stored as a double variable, by means of a call to nag_machine_real_largest (x02al).

The time taken by nag_correg_linregs_noconst_miss (g02cd) depends on $n$ and the number of missing observations.
The function uses a two-pass algorithm.

Example

This example reads in eight observations on each of two variables, and then performs a simple linear regression with no constant, with the first variable as the independent variable, and the second variable as the dependent variable, omitting cases involving missing values ($0.0$ for the first variable, $99.0$ for the second). Finally the results are printed.
```function g02cd_example

fprintf('g02cd example results\n\n');

x = [ 1.0   0.0    4.0    7.5   2.5   0.0  10.0   5.0];
y = [20.0  15.5   28.3   45.0  24.5  10.0  99.0  31.2];

n = numel(x);
fprintf('  i    independent(x)   dependent(y)\n');
fprintf('%3d%14.4f%14.4f\n',[1:n; x; y]);

xmiss = 0;
ymiss = 99;

[result, ifail] = g02cd( ...
x, y, xmiss, ymiss);

fprintf('\n');
fprintf('Mean of independent variable               = %8.4f\n', result(1));
fprintf('Mean of   dependent variable               = %8.4f\n', result(2));
fprintf('Standard deviation of independent variable = %8.4f\n', result(3));
fprintf('Standard deviation of   dependent variable = %8.4f\n', result(4));
fprintf('Correlation coefficient                    = %8.4f\n', result(5));
fprintf('\n');
fprintf('Regression coefficient                     = %8.4f\n', result(6));
fprintf('Standard error of coefficient              = %8.4f\n', result(8));
fprintf('t-value for coefficient                    = %8.4f\n', result(10));

fprintf('\nAnalysis of regression table :-\n\n');

fprintf('     Source       Sum of squares  D.F.    Mean square     F-value\n');
fprintf('Due to regression %11.3f%8d%14.3f%14.3f\n', result(12:15));
fprintf('Total             %11.3f%8d\n', result(19:20));

fprintf('\nNumber of cases actually used  = %d\n', result(21));

```
```g02cd example results

i    independent(x)   dependent(y)
1        1.0000       20.0000
2        0.0000       15.5000
3        4.0000       28.3000
4        7.5000       45.0000
5        2.5000       24.5000
6        0.0000       10.0000
7       10.0000       99.0000
8        5.0000       31.2000

Mean of independent variable               =   4.0000
Mean of   dependent variable               =  29.8000
Standard deviation of independent variable =   2.4749
Standard deviation of   dependent variable =   9.4787
Correlation coefficient                    =   0.9799

Regression coefficient                     =   6.5833
Standard error of coefficient              =   0.8046
t-value for coefficient                    =   8.1816

Analysis of regression table :-

Source       Sum of squares  D.F.    Mean square     F-value
Due to regression    4528.949       1      4528.949        66.939