| 1 |
greg |
1.1 |
/* Copyright (c) 1991 Regents of the University of California */
|
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint
|
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
|
|
#endif
|
| 6 |
|
|
|
| 7 |
|
|
/*
|
| 8 |
|
|
* Basic linear regression calculation.
|
| 9 |
|
|
*/
|
| 10 |
|
|
|
| 11 |
|
|
#include "linregr.h"
|
| 12 |
|
|
|
| 13 |
|
|
|
| 14 |
|
|
lrclear(l) /* initialize sum */
|
| 15 |
|
|
register LRSUM *l;
|
| 16 |
|
|
{
|
| 17 |
|
|
l->xs = l->ys = l->xxs = l->yys = l->xys = 0.0;
|
| 18 |
|
|
l->n = 0;
|
| 19 |
|
|
}
|
| 20 |
|
|
|
| 21 |
|
|
|
| 22 |
|
|
flrpoint(x, y, l) /* add point (x,y) to sum */
|
| 23 |
|
|
double x, y;
|
| 24 |
|
|
register LRSUM *l;
|
| 25 |
|
|
{
|
| 26 |
|
|
l->xs += x;
|
| 27 |
|
|
l->ys += y;
|
| 28 |
|
|
l->xxs += x*x;
|
| 29 |
|
|
l->yys += y*y;
|
| 30 |
|
|
l->xys += x*y;
|
| 31 |
|
|
return(++l->n);
|
| 32 |
|
|
}
|
| 33 |
|
|
|
| 34 |
|
|
|
| 35 |
|
|
lrfit(r, l) /* compute linear regression */
|
| 36 |
|
|
register LRLIN *r;
|
| 37 |
|
|
register LRSUM *l;
|
| 38 |
|
|
{
|
| 39 |
|
|
double nxvar, nyvar;
|
| 40 |
|
|
|
| 41 |
|
|
if (l->n < 2)
|
| 42 |
|
|
return(-1);
|
| 43 |
|
|
nxvar = l->xxs - l->xs*l->xs/l->n;
|
| 44 |
|
|
nyvar = l->yys - l->ys*l->ys/l->n;
|
| 45 |
|
|
if (nxvar == 0.0 || nyvar == 0.0)
|
| 46 |
|
|
return(-1);
|
| 47 |
|
|
r->slope = (l->xys - l->xs*l->ys/l->n) / nxvar;
|
| 48 |
|
|
r->intercept = (l->ys - r->slope*l->xs) / l->n;
|
| 49 |
|
|
r->correlation = r->slope*sqrt(nxvar/nyvar);
|
| 50 |
|
|
return(0);
|
| 51 |
|
|
}
|