G02 Chapter Contents
G02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG02CCF

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.

## 1  Purpose

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

## 2  Specification

 SUBROUTINE G02CCF ( N, X, Y, XMISS, YMISS, RESULT, IFAIL)
 INTEGER N, IFAIL REAL (KIND=nag_wp) X(N), Y(N), XMISS, YMISS, RESULT(21)

## 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 $\left({x}_{i},{y}_{i}\right)$, $i=1,2,\dots ,n\text{ }\left(n>2\right)$ which do not include missing values.
The routine 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$, the regression constant, $a$, 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 Section 7).
Let ${w}_{i}=0$ if the $i$th observation of either $x$ or $y$ is missing, i.e., if ${x}_{i}=\mathit{xm}$ and/or ${y}_{i}=\mathit{ym}$; and ${w}_{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$, and the regression constant, $a$:
 $b=∑i=1nwixi-x-yi-y- ∑i=1nwi xi-x- 2 , a=y--bx-.$
(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.$
(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.$
(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, $se\left(b\right)$, and the standard error of the regression constant, $se\left(a\right)$:
 $seb=MSD ∑i=1nwi xi-x- 2 ; sea=MSD 1∑i=1nwi +x-2 ∑i=1nwi xi-x- 2 .$
(j) The $t$ value for the regression coefficient, $t\left(b\right)$, and the $t$ value for the regression constant, $t\left(a\right)$:
 $tb=bseb ; ta=asea .$
(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  Parameters

1:     N – INTEGERInput
On entry: $n$, the number of pairs of observations.
Constraint: ${\mathbf{N}}>2$.
2:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{X}}\left(\mathit{i}\right)$ must contain ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3:     Y(N) – REAL (KIND=nag_wp) arrayInput
On entry: ${\mathbf{Y}}\left(\mathit{i}\right)$ must contain ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,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:     RESULT($21$) – REAL (KIND=nag_wp) arrayOutput
On exit: 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)$ $a$, the regression constant; ${\mathbf{RESULT}}\left(8\right)$ $se\left(b\right)$, the standard error of the regression coefficient; ${\mathbf{RESULT}}\left(9\right)$ $se\left(a\right)$, the standard error of the regression constant; ${\mathbf{RESULT}}\left(10\right)$ $t\left(b\right)$, the $t$ value for the regression coefficient; ${\mathbf{RESULT}}\left(11\right)$ $t\left(a\right)$, the $t$ value for the regression constant; ${\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.
7:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ 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\text{​ 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 $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{N}}\le 2$.
${\mathbf{IFAIL}}=2$
After observations with missing values were omitted, two or fewer 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.

## 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 $\left(1±{0.1}^{\left({\mathbf{X02BEF}}-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(a\right)$  (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.

The time taken by G02CCF depends on $n$ and the number of missing observations.
The routine uses a two-pass algorithm.

## 9  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.

### 9.1  Program Text

Program Text (g02ccfe.f90)

### 9.2  Program Data

Program Data (g02ccfe.d)

### 9.3  Program Results

Program Results (g02ccfe.r)