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 (g02cb)

## Purpose

nag_correg_linregs_noconst (g02cb) performs a simple linear regression with no constant, with dependent variable y$y$ and independent variable x$x$.

## Syntax

[result, ifail] = g02cb(x, y, 'n', n)
[result, ifail] = nag_correg_linregs_noconst(x, y, 'n', n)

## Description

nag_correg_linregs_noconst (g02cb) fits a straight line of the form
 y = bx $y=bx$
to the data points
 (x1,y1),(x2,y2), … ,(xn,yn) , $(x1,y1),(x2,y2),…,(xn,yn) ,$
such that
 yi = bxi + ei,  i = 1,2, … ,n(n ≥ 2). $yi=bxi+ei, i=1,2,…,n(n≥2).$
The function calculates the regression coefficient, b$b$, and the various other statistical quantities by minimizing
 n ∑ ei2. i = 1
$∑i=1nei2.$
The input data consists of the n$n$ pairs of observations (x1,y1),(x2,y2),,(xn,yn)$\left({x}_{1},{y}_{1}\right),\left({x}_{2},{y}_{2}\right),\dots ,\left({x}_{n},{y}_{n}\right)$ on the independent variable x$x$ and the dependent variable y$y$.
The quantities calculated are:
(a) Means:
 n n x = 1/n ∑ xi;  y = 1/n ∑ yi. i = 1 i = 1
$x-=1n∑i=1nxi; y-=1n∑i=1nyi.$
(b) Standard deviations:
 sx = sqrt(1/(n − 1) ∑ i = 1n(xi − x)2);   sy = sqrt(1/(n − 1) ∑ i = 1n(yi − y)2). $sx=1n- 1 ∑i= 1n (xi-x-) 2; sy=1n- 1 ∑i= 1n (yi-y-) 2.$
(c) Pearson product-moment correlation coefficient:
 r = ( ∑ i = 1n(xi − x)(yi − y))/(sqrt( ∑ i = 1n(xi − x)2 ∑ i = 1n(yi − y)2)). $r=∑i=1n(xi-x-)(yi-y-) ∑i=1n (xi-x-) 2∑i=1n (yi-y-) 2 .$
(d) The regression coefficient, b$b$:
 b = ( ∑ i = 1nxiyi)/( ∑ i = 1nxi2). $b=∑i=1nxiyi ∑i=1nxi2 .$
(e) The sum of squares attributable to the regression, SSR$SSR$, the sum of squares of deviations about the regression, SSD$SSD$, and the total sum of squares, SST$SST$:
 n n SST = ∑ yi2;  SSD = ∑ (yi − bxi)2,  SSR = SST − SSD. i = 1 i = 1
$SST=∑i=1nyi2; SSD=∑i=1n (yi-bxi)2, SSR=SST-SSD.$
(f) The degrees of freedom attributable to the regression, DFR$DFR$, the degrees of freedom of deviations about the regression, DFD$DFD$, and the total degrees of freedom, DFT$DFT$:
 DFT = n;  DFD = n − 1,  DFR = 1. $DFT=n; DFD=n-1, DFR=1.$
(g) The mean square attributable to the regression, MSR$MSR$, and the mean square of deviations about the regression, MSD.$MSD\text{.}$
 MSR = SSR / DFR;  MSD = SSD / DFD. $MSR=SSR/DFR; MSD=SSD/DFD.$
(h) The F$F$ value for the analysis of variance:
 F = MSR / MSD. $F=MSR/MSD.$
(i) The standard error of the regression coefficient:
 se(b) = sqrt((MSD)/( ∑ i = 1nxi2)). $se(b)=MSD ∑i= 1nxi2 .$
(j) The t$t$ value for the regression coefficient:
 t(b) = b/(se(b)). $t(b)=bse(b) .$

## References

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

## Parameters

### Compulsory Input Parameters

1:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n2${\mathbf{n}}\ge 2$.
x(i)${\mathbf{x}}\left(\mathit{i}\right)$ must contain xi${x}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
2:     y(n) – double array
n, the dimension of the array, must satisfy the constraint n2${\mathbf{n}}\ge 2$.
y(i)${\mathbf{y}}\left(\mathit{i}\right)$ must contain yi${y}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.

### Optional Input Parameters

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

None.

### Output Parameters

1:     result(20$20$) – double array
The following information:
 result(1)${\mathbf{result}}\left(1\right)$ x$\stackrel{-}{x}$, the mean value of the independent variable, x$x$; result(2)${\mathbf{result}}\left(2\right)$ y$\stackrel{-}{y}$, the mean value of the dependent variable, y$y$; result(3)${\mathbf{result}}\left(3\right)$ sx${s}_{x}$, the standard deviation of the independent variable, x$x$; result(4)${\mathbf{result}}\left(4\right)$ sy${s}_{y}$, the standard deviation of the dependent variable, y$y$; result(5)${\mathbf{result}}\left(5\right)$ r$r$, the Pearson product-moment correlation between the independent variable x$x$ and the dependent variable y$y$; result(6)${\mathbf{result}}\left(6\right)$ b$b$, the regression coefficient; result(7)${\mathbf{result}}\left(7\right)$ the value 0.0$0.0$; result(8)${\mathbf{result}}\left(8\right)$ se(b)$se\left(b\right)$, the standard error of the regression coefficient; result(9)${\mathbf{result}}\left(9\right)$ the value 0.0$0.0$; result(10)${\mathbf{result}}\left(10\right)$ t(b)$t\left(b\right)$, the t$t$ value for the regression coefficient; result(11)${\mathbf{result}}\left(11\right)$ the value 0.0$0.0$; result(12)${\mathbf{result}}\left(12\right)$ SSR$SSR$, the sum of squares attributable to the regression; result(13)${\mathbf{result}}\left(13\right)$ DFR$DFR$, the degrees of freedom attributable to the regression; result(14)${\mathbf{result}}\left(14\right)$ MSR$MSR$, the mean square attributable to the regression; result(15)${\mathbf{result}}\left(15\right)$ F$F$, the F$F$ value for the analysis of variance; result(16)${\mathbf{result}}\left(16\right)$ SSD$SSD$, the sum of squares of deviations about the regression; result(17)${\mathbf{result}}\left(17\right)$ DFD$DFD$, the degrees of freedom of deviations about the regression; result(18)${\mathbf{result}}\left(18\right)$ MSD$MSD$, the mean square of deviations about the regression; result(19)${\mathbf{result}}\left(19\right)$ SST$SST$, the total sum of squares; result(20)${\mathbf{result}}\left(20\right)$ DFT$DFT$, the total degrees of freedom.
2:     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, n < 2${\mathbf{n}}<2$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, all n values of at least one of the variables x$x$ and y$y$ are identical.

## Accuracy

nag_correg_linregs_noconst (g02cb) does not use additional precision arithmetic for the accumulation of scalar products, so there may be a loss of significant figures for large n$n$.
If, in calculating F$F$ or t(b)$t\left(b\right)$  (see Section [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).

Computation time depends on n$n$.
nag_correg_linregs_noconst (g02cb) uses a two-pass algorithm.

## Example

```function nag_correg_linregs_noconst_example
x = [1;
0;
4;
7.5;
2.5;
0;
10;
5];
y = [20;
15.5;
28.3;
45;
24.5;
10;
99;
31.2];
[result, ifail] = nag_correg_linregs_noconst(x, y)
```
```

result =

1.0e+04 *

0.0004
0.0034
0.0004
0.0028
0.0001
0.0008
0
0.0001
0
0.0009
0
1.3768
0.0001
1.3768
0.0082
0.1173
0.0007
0.0168
1.4941
0.0008

ifail =

0

```
```function g02cb_example
x = [1;
0;
4;
7.5;
2.5;
0;
10;
5];
y = [20;
15.5;
28.3;
45;
24.5;
10;
99;
31.2];
[result, ifail] = g02cb(x, y)
```
```

result =

1.0e+04 *

0.0004
0.0034
0.0004
0.0028
0.0001
0.0008
0
0.0001
0
0.0009
0
1.3768
0.0001
1.3768
0.0082
0.1173
0.0007
0.0168
1.4941
0.0008

ifail =

0

```