# NAG FL Interfaceg02caf (linregs_​const)

## 1Purpose

g02caf performs a simple linear regression with dependent variable $y$ and independent variable $x$.

## 2Specification

Fortran Interface
 Subroutine g02caf ( n, x, y,
 Integer, Intent (In) :: n Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(n), y(n) Real (Kind=nag_wp), Intent (Out) :: result(20)
#include <nag.h>
 void g02caf_ (const Integer *n, const double x[], const double y[], double result[], Integer *ifail)
The routine may be called by the names g02caf or nagf_correg_linregs_const.

## 3Description

g02caf fits a straight line of the form
 $y=a+bx$
to the data points
 $x1,y1,x2,y2,…,xn,yn,$
such that
 $yi=a+bxi+ei, i=1,2,…,nn>2.$
The routine calculates the regression coefficient, $b$, the regression constant, $a$ (and various other statistical quantities) by minimizing
 $∑i=1nei2.$
The input data consist 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:
1. (a)Means:
 $x¯=1n∑i=1nxi; y¯=1n∑i=1nyi.$
2. (b)Standard deviations:
 $sx=1n- 1 ∑i= 1n xi-x¯ 2; sy=1n- 1 ∑i= 1n yi-y¯ 2.$
3. (c)Pearson product-moment correlation coefficient:
 $r=∑i=1nxi-x¯yi-y¯ ∑i=1n xi-x¯ 2∑i=1n yi-y¯ 2 .$
4. (d)The regression coefficient, $b$, and the regression constant, $a$:
 $b=∑i=1nxi-x¯yi-y¯ ∑i=1n xi-x¯ 2 ;a=y¯-bx¯.$
5. (e)The sum of squares attributable to the regression, $\mathrm{SSR}$, the sum of squares of deviations about the regression, $\mathrm{SSD}$, and the total sum of squares, $\mathrm{SST}$:
 $SST=∑i=1n yi-y¯ 2;SSD=∑i=1n yi-a-bxi 2;SSR=SST-SSD.$
6. (f)The degrees of freedom attributable to the regression, $\mathrm{DFR}$, the degrees of freedom of deviations about the regression, $\mathrm{DFD}$, and the total degrees of freedom, $\mathrm{DFT}$:
 $DFT=n-1; ​DFD=n-2; ​DFR=1.$
7. (g)The mean square attributable to the regression, $\mathrm{MSR}$, and the mean square of deviations about the regression, $\mathrm{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, $se\left(b\right)$, and the standard error of the regression constant, $se\left(a\right)$:
 $seb=MSD∑i=1n xi-x¯ 2 ; sea=MSD 1n+x¯2 ∑i=1n xi-x¯ 2 .$
10. (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 .$

## 4References

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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the number of pairs of observations.
Constraint: ${\mathbf{n}}>2$.
2: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{x}}\left(\mathit{i}\right)$ must contain ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3: $\mathbf{y}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{y}}\left(\mathit{i}\right)$ must contain ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
4: $\mathbf{result}\left(20\right)$Real (Kind=nag_wp) array Output
On exit: the following information:
 ${\mathbf{result}}\left(1\right)$ $\overline{x}$, the mean value of the independent variable, $x$; ${\mathbf{result}}\left(2\right)$ $\overline{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)$ $\mathrm{SSR}$, the sum of squares attributable to the regression; ${\mathbf{result}}\left(13\right)$ $\mathrm{DFR}$, the degrees of freedom attributable to the regression; ${\mathbf{result}}\left(14\right)$ $\mathrm{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)$ $\mathrm{SSD}$, the sum of squares of deviations about the regression; ${\mathbf{result}}\left(17\right)$ $\mathrm{DFD}$, the degrees of freedom of deviations about the regression ${\mathbf{result}}\left(18\right)$ $\mathrm{MSD}$, the mean square of deviations about the regression; ${\mathbf{result}}\left(19\right)$ $\mathrm{SST}$, the total sum of squares; ${\mathbf{result}}\left(20\right)$ DFT, the total degrees of freedom.
5: $\mathbf{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 $-\mathbf{1}$ 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).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}>2$.
${\mathbf{ifail}}=2$
On entry, all n values of at least one of x and y are identical.
${\mathbf{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.
${\mathbf{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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

g02caf 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$, $t\left(a\right)$ or $t\left(b\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.

## 8Parallelism and Performance

g02caf is not threaded in any implementation.

The time taken by g02caf depends on $n$.
The routine uses a two-pass algorithm.

## 10Example

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. Finally the results are printed.

### 10.1Program Text

Program Text (g02cafe.f90)

### 10.2Program Data

Program Data (g02cafe.d)

### 10.3Program Results

Program Results (g02cafe.r)