hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_correg_linregs_noconst (g02cb)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_correg_linregs_noconst (g02cb) performs a simple linear regression with no constant, with dependent variable y and independent variable 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  
to the data points
x1,y1,x2,y2,,xn,yn ,  
such that
yi=bxi+ei,  i=1,2,,nn2.  
The function calculates the regression coefficient, b, and the various other statistical quantities by minimizing
i=1nei2.  
The input data consists of the n pairs of observations x1,y1,x2,y2,,xn,yn on the independent variable x and the dependent variable y.
The quantities calculated are:
(a) Means:
x-=1ni=1nxi;  y-=1ni=1nyi.  
(b) Standard deviations:
sx=1n- 1 i= 1n xi-x- 2;   sy=1n- 1 i= 1n yi-y- 2.  
(c) Pearson product-moment correlation coefficient:
r=i=1nxi-x-yi-y- i=1n xi-x- 2i=1n yi-y- 2 .  
(d) The regression coefficient, b:
b=i=1nxiyi i=1nxi2 .  
(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=1nyi2;   SSD=i=1n yi-bxi2,   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=n;  DFD=n-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= 1nxi2 .  
(j) The t value for the regression coefficient:
tb=bseb .  

References

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

Parameters

Compulsory Input Parameters

1:     xn – double array
xi must contain xi, for i=1,2,,n.
2:     yn – double array
yi must contain yi, for i=1,2,,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, the number of pairs of observations.
Constraint: n>2.

Output Parameters

1:     result20 – double array
The following information:
result1 x-, the mean value of the independent variable, x;
result2 y-, the mean value of the dependent variable, y;
result3 sx, the standard deviation of the independent variable, x;
result4 sy, the standard deviation of the dependent variable, y;
result5 r, the Pearson product-moment correlation between the independent variable x and the dependent variable y;
result6 b, the regression coefficient;
result7 the value 0.0;
result8 seb, the standard error of the regression coefficient;
result9 the value 0.0;
result10 tb, the t value for the regression coefficient;
result11 the value 0.0;
result12 SSR, the sum of squares attributable to the regression;
result13 DFR, the degrees of freedom attributable to the regression;
result14 MSR, the mean square attributable to the regression;
result15 F, the F value for the analysis of variance;
result16 SSD, the sum of squares of deviations about the regression;
result17 DFD, the degrees of freedom of deviations about the regression;
result18 MSD, the mean square of deviations about the regression;
result19 SST, the total sum of squares;
result20 DFT, the total degrees of freedom.
2:     ifail int64int32nag_int scalar
ifail=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
On entry,n<2.
   ifail=2
On entry,all n values of at least one of the variables x and y are identical.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

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.
If, in calculating F or tb  (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).

Further Comments

Computation time depends on n.
nag_correg_linregs_noconst (g02cb) 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. Finally the results are printed.
function g02cb_example


fprintf('g02cb 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]);

[result, ifail] = g02cb(x, y);

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('About  regression %11.3f%8d%14.3f\n', result(16:18));
fprintf('Total             %11.3f%8d\n', result(19:20));


g02cb 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               =   3.7500
Mean of   dependent variable               =  34.1875
Standard deviation of independent variable =   3.6253
Standard deviation of   dependent variable =  28.2604
Correlation coefficient                    =   0.9096

Regression coefficient                     =   8.2051
Standard error of coefficient              =   0.9052
t-value for coefficient                    =   9.0642

Analysis of regression table :-

     Source       Sum of squares  D.F.    Mean square     F-value
Due to regression   13767.805       1     13767.805        82.159
About  regression    1173.025       7       167.575
Total               14940.830       8

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015