NAG FL Interface
g02ccf (linregs_​const_​miss)

1 Purpose

g02ccf performs a simple linear regression with dependent variable y and independent variable x, omitting cases involving missing values.

2 Specification

Fortran Interface
Subroutine g02ccf ( n, x, y, xmiss, ymiss, result, ifail)
Integer, Intent (In) :: n
Integer, Intent (Inout) :: ifail
Real (Kind=nag_wp), Intent (In) :: x(n), y(n), xmiss, ymiss
Real (Kind=nag_wp), Intent (Out) :: result(21)
C Header Interface
#include <nag.h>
void  g02ccf_ (const Integer *n, const double x[], const double y[], const double *xmiss, const double *ymiss, double result[], Integer *ifail)
The routine may be called by the names g02ccf or nagf_correg_linregs_const_miss.

3 Description

g02ccf fits a straight line of the form
y=a+bx  
to those of the data points
x1,y1,x2,y2,,xn,yn  
that do not include missing values, such that
yi=a+bxi+ei  
for those xi,yi, i=1,2,,nn>2 which do not include missing values.
The routine eliminates all pairs of observations xi,yi which contain a missing value for either x or y, and then calculates the regression coefficient, b, the regression constant, a, and various other statistical quantities, by minimizing the sum of the ei2 over those cases remaining in the calculations.
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.
In addition two values, xm and ym, are given which are considered to represent missing observations for x and y respectively. (See Section 7).
Let wi=0 if the ith observation of either x or y is missing, i.e., if xi=xm and/or yi=ym; and wi=1 otherwise, for i=1,2,,n.
The quantities calculated are:
  1. (a)Means:
    x¯=i=1nwixi i=1nwi ;  y¯=i=1nwiyi i=1nwi .  
  2. (b)Standard deviations:
    sx= i= 1nwi xi-x¯ 2 i= 1nwi- 1 ;   sy= i= 1nwi yi-y¯ 2 i= 1nwi- 1 .  
  3. (c)Pearson product-moment correlation coefficient:
    r=i=1nwixi-x¯yi-y¯ i=1nwi xi-x¯ 2i=1nwi yi-y¯ 2 .  
  4. (d)The regression coefficient, b, and the regression constant, a:
    b=i=1nwixi-x¯yi-y¯ i=1nwi xi-x¯ 2 ,  a=y¯-bx¯.  
  5. (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=1nwi yi-y¯ 2;  SSD=i=1nwi yi-a-bxi 2;  SSR=SST-SSD.  
  6. (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-1;  DFD=i=1nwi-2;  DFR=1.  
  7. (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.  
  8. (h)The F value for the analysis of variance:
    F=MSR/MSD.  
  9. (i)The standard error of the regression coefficient, seb, and the standard error of the regression constant, sea:
    seb=MSD i=1nwi xi-x¯ 2 ;  sea=MSD 1i=1nwi +x¯2 i=1nwi xi-x¯ 2 .  
  10. (j)The t value for the regression coefficient, tb, and the t value for the regression constant, ta:
    tb=bseb ;  ta=asea .  
  11. (k)The number of observations used in the calculations:
    nc=i= 1nwi.  

4 References

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

5 Arguments

1: n Integer Input
On entry: n, the number of pairs of observations.
Constraint: n>2.
2: xn Real (Kind=nag_wp) array Input
On entry: xi must contain xi, for i=1,2,,n.
3: yn Real (Kind=nag_wp) array Input
On entry: yi must contain yi, for i=1,2,,n.
4: xmiss Real (Kind=nag_wp) Input
On entry: the value xm which is to be taken as the missing value for the variable x. See Section 7.
5: ymiss Real (Kind=nag_wp) Input
On entry: the value ym which is to be taken as the missing value for the variable y. See Section 7.
6: result21 Real (Kind=nag_wp) array Output
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 a, the regression constant;
result8 seb, the standard error of the regression coefficient;
result9 sea, the standard error of the regression constant;
result10 tb, the t value for the regression coefficient;
result11 ta, the t value for the regression constant;
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;
result21 nc, the number of observations used in the calculations.
7: ifail Integer Input/Output
On entry: ifail must be set to 0, -1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of -1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value -1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value 0 is recommended. 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=value.
Constraint: n>2.
ifail=2
After observations with missing values were omitted, two or fewer cases remained.
ifail=3
After observations with missing values were omitted, all remaining values of at least one of x and y were identical.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

g02ccf 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. g02ccf treats all values in the inclusive range 1±0.1x02bef-2×xmj, where xmj 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 ta (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

g02ccf is not threaded in any implementation.

9 Further Comments

The time taken by g02ccf depends on n and the number of missing observations.
The routine 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 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.

10.1 Program Text

Program Text (g02ccfe.f90)

10.2 Program Data

Program Data (g02ccfe.d)

10.3 Program Results

Program Results (g02ccfe.r)