| 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 | greg | 2.2 | #include <math.h> | 
| 12 |  |  |  | 
| 13 | greg | 1.1 | #include "linregr.h" | 
| 14 |  |  |  | 
| 15 |  |  |  | 
| 16 |  |  | lrclear(l)                      /* initialize sum */ | 
| 17 |  |  | register LRSUM  *l; | 
| 18 |  |  | { | 
| 19 |  |  | l->xs = l->ys = l->xxs = l->yys = l->xys = 0.0; | 
| 20 |  |  | l->n = 0; | 
| 21 |  |  | } | 
| 22 |  |  |  | 
| 23 |  |  |  | 
| 24 |  |  | flrpoint(x, y, l)               /* add point (x,y) to sum */ | 
| 25 |  |  | double  x, y; | 
| 26 |  |  | register LRSUM  *l; | 
| 27 |  |  | { | 
| 28 |  |  | l->xs += x; | 
| 29 |  |  | l->ys += y; | 
| 30 |  |  | l->xxs += x*x; | 
| 31 |  |  | l->yys += y*y; | 
| 32 |  |  | l->xys += x*y; | 
| 33 |  |  | return(++l->n); | 
| 34 |  |  | } | 
| 35 |  |  |  | 
| 36 |  |  |  | 
| 37 |  |  | lrfit(r, l)                     /* compute linear regression */ | 
| 38 |  |  | register LRLIN  *r; | 
| 39 |  |  | register LRSUM  *l; | 
| 40 |  |  | { | 
| 41 |  |  | double  nxvar, nyvar; | 
| 42 |  |  |  | 
| 43 |  |  | if (l->n < 2) | 
| 44 |  |  | return(-1); | 
| 45 |  |  | nxvar = l->xxs - l->xs*l->xs/l->n; | 
| 46 |  |  | nyvar = l->yys - l->ys*l->ys/l->n; | 
| 47 |  |  | if (nxvar == 0.0 || nyvar == 0.0) | 
| 48 |  |  | return(-1); | 
| 49 |  |  | r->slope = (l->xys - l->xs*l->ys/l->n) / nxvar; | 
| 50 |  |  | r->intercept = (l->ys - r->slope*l->xs) / l->n; | 
| 51 |  |  | r->correlation = r->slope*sqrt(nxvar/nyvar); | 
| 52 |  |  | return(0); | 
| 53 |  |  | } |