﻿ g02ca Method
g02ca performs a simple linear regression with dependent variable $y$ and independent variable $x$.

# Syntax

C#
```public static void g02ca(
int n,
double[] x,
double[] y,
double[] result,
out int ifail
)```
Visual Basic
```Public Shared Sub g02ca ( _
n As Integer, _
x As Double(), _
y As Double(), _
result As Double(), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g02ca(
int n,
array<double>^ x,
array<double>^ y,
array<double>^ result,
[OutAttribute] int% ifail
)```
F#
```static member g02ca :
n : int *
x : float[] *
y : float[] *
result : float[] *
ifail : int byref -> unit
```

#### Parameters

n
Type: System..::..Int32
On entry: $n$, the number of pairs of observations.
Constraint: ${\mathbf{n}}>2$.
x
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: ${\mathbf{x}}\left[\mathit{i}-1\right]$ must contain ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
y
Type: array<System..::..Double>[]()[][]
An array of size [n]
On entry: ${\mathbf{y}}\left[\mathit{i}-1\right]$ must contain ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
result
Type: array<System..::..Double>[]()[][]
An array of size [$20$]
On exit: the following information:
 ${\mathbf{result}}\left[0\right]$ $\stackrel{-}{x}$, the mean value of the independent variable, $x$; ${\mathbf{result}}\left[1\right]$ $\stackrel{-}{y}$, the mean value of the dependent variable, $y$; ${\mathbf{result}}\left[2\right]$ ${s}_{x}$ the standard deviation of the independent variable, $x$; ${\mathbf{result}}\left[3\right]$ ${s}_{y}$ the standard deviation of the dependent variable, $y$; ${\mathbf{result}}\left[4\right]$ $r$, the Pearson product-moment correlation between the independent variable $x$ and the dependent variable $y$; ${\mathbf{result}}\left[5\right]$ $b$, the regression coefficient; ${\mathbf{result}}\left[6\right]$ $a$, the regression constant; ${\mathbf{result}}\left[7\right]$ $se\left(b\right)$, the standard error of the regression coefficient; ${\mathbf{result}}\left[8\right]$ $se\left(a\right)$, the standard error of the regression constant; ${\mathbf{result}}\left[9\right]$ $t\left(b\right)$, the $t$ value for the regression coefficient; ${\mathbf{result}}\left[10\right]$ $t\left(a\right)$, the $t$ value for the regression constant; ${\mathbf{result}}\left[11\right]$ $\mathrm{SSR}$, the sum of squares attributable to the regression; ${\mathbf{result}}\left[12\right]$ $\mathrm{DFR}$, the degrees of freedom attributable to the regression; ${\mathbf{result}}\left[13\right]$ $\mathrm{MSR}$, the mean square attributable to the regression; ${\mathbf{result}}\left[14\right]$ $F$, the $F$ value for the analysis of variance; ${\mathbf{result}}\left[15\right]$ $\mathrm{SSD}$, the sum of squares of deviations about the regression; ${\mathbf{result}}\left[16\right]$ $\mathrm{DFD}$, the degrees of freedom of deviations about the regression ${\mathbf{result}}\left[17\right]$ $\mathrm{MSD}$, the mean square of deviations about the regression; ${\mathbf{result}}\left[18\right]$ $\mathrm{SST}$, the total sum of squares; ${\mathbf{result}}\left[19\right]$ DFT, the total degrees of freedom.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

g02ca 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 method 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:
(a) Means:
 $x-=1n∑i=1nxi; y-=1n∑i=1nyi.$
(b) Standard deviations:
 $sx=1n-1∑i=1nxi-x-2; sy=1n-1∑i=1nyi-y-2.$
(c) Pearson product-moment correlation coefficient:
 $r=∑i=1nxi-x-yi-y-∑i=1nxi-x-2∑i=1nyi-y-2.$
(d) The regression coefficient, $b$, and the regression constant, $a$:
 $b=∑i=1nxi-x-yi-y-∑i=1nxi-x-2;a=y--bx-.$
(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=1nyi-y-2;SSD=∑i=1nyi-a-bxi2;SSR=SST-SSD.$
(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.$
(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.$
(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=1nxi-x-2; sea=MSD1n+x-2∑i=1nxi-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.$

# References

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

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}\le 2$.
${\mathbf{ifail}}=2$
 On entry, all n values of at least one of the variables $x$ and $y$ are identical.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

g02ca 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 [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 real variable, by means of a call to x02al.

# Parallelism and Performance

None.

The time taken by g02ca depends on $n$.
The method 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 the first variable as the independent variable, and the second variable as the dependent variable. Finally the results are printed.

Example program (C#): g02cae.cs

Example program data: g02cae.d

Example program results: g02cae.r