G02CBF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

G02CBF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

G02CBF performs a simple linear regression with no constant, with dependent variable y and independent variable x.

2  Specification

SUBROUTINE G02CBF ( N, X, Y, RESULT, IFAIL)
INTEGER  N, IFAIL
REAL (KIND=nag_wp)  X(N), Y(N), RESULT(20)

3  Description

G02CBF 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 routine 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 .  

4  References

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

5  Parameters

1:     N – INTEGERInput
On entry: n, the number of pairs of observations.
Constraint: N>2.
2:     XN – REAL (KIND=nag_wp) arrayInput
On entry: Xi must contain xi, for i=1,2,,n.
3:     YN – REAL (KIND=nag_wp) arrayInput
On entry: Yi must contain yi, for i=1,2,,n.
4:     RESULT20 – REAL (KIND=nag_wp) arrayOutput
On exit: 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.
5:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
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.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

G02CBF 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 Section 3), 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 real variable, by means of a call to X02ALF.

8  Parallelism and Performance

Not applicable.

9  Further Comments

Computation time depends on n.
G02CBF uses a two-pass algorithm.

10  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.

10.1  Program Text

Program Text (g02cbfe.f90)

10.2  Program Data

Program Data (g02cbfe.d)

10.3  Program Results

Program Results (g02cbfe.r)


G02CBF (PDF version)
G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

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