linreg.h
Go to the documentation of this file.
1 #pragma once
2 /* linreg.h
3 Linear Regression calculation class
4 
5 by: David C. Swaim II, Ph.D.
6 
7 This class implements a standard linear regression on
8 experimental data using a least squares fit to a straight
9 line graph. Calculates coefficients a and b of the equation:
10 
11 y = a + b * x
12 
13 for data points of x and y. Also calculates the coefficient of
14 determination, the coefficient of correlation, and standard
15 error of estimate.
16 
17 The value n (number of points) must be greater than 2 to
18 calculate the regression. This is primarily because the
19 standard error has a (N-2) in the denominator.
20 
21 Check haveData() to see if there is enough data in
22 LinearRegression to get values.
23 
24 You can think of the x,y pairs as 2 dimensional points.
25 The class Point2D is included to allow pairing x and y
26 values together to represent a point on a plane.
27 
28 */
29 
30 #ifndef _LINREG_H_
31 #define _LINREG_H_
32 
33 
34 
35 
37 {
38 
39 
40 public:
41  // Constructor using an array of Point2D objects
42  // This is also the default constructor
43 
44  // Constructor using arrays of x values and y values
45  LinearRegression(double *x, double *y, long size = 0);
46 
47  virtual void addXY(const double& x, const double& y);
48 
49  // Must have at least 3 points to calculate
50  // standard error of estimate. Do we have enough data?
51  int haveData() const { return (n > 2 ? 1 : 0); }
52  long items() const { return n; }
53 
54  virtual double getA() const { return a; }
55  virtual double getB() const { return b; }
56 
57  double getCoefDeterm() const { return coefD; }
58  double getCoefCorrel() const { return coefC; }
59  double getStdErrorEst() const { return stdError; }
60  virtual double estimateY(double x) const { return (a + b * x); }
61 
62 protected:
63  long n; // number of data points input so far
64  double sumX, sumY; // sums of x and y
65  double sumXsquared, // sum of x squares
66  sumYsquared; // sum y squares
67  double sumXY; // sum of x*y
68 
69  double a, b; // coefficients of f(x) = a + b*x
70  double coefD, // coefficient of determination
71  coefC, // coefficient of correlation
72  stdError; // standard error of estimate
73 
74  void Calculate(); // calculate coefficients
75 };
76 
77 #endif // end of linreg.h
double sumXY
Definition: linreg.h:67
Definition: linreg.h:36
double coefD
Definition: linreg.h:70
double getCoefDeterm() const
Definition: linreg.h:57
double getStdErrorEst() const
Definition: linreg.h:59
int haveData() const
Definition: linreg.h:51
virtual void addXY(const double &x, const double &y)
Definition: linreg.cpp:20
virtual double getB() const
Definition: linreg.h:55
LinearRegression(double *x, double *y, long size=0)
Definition: linreg.cpp:9
double stdError
Definition: linreg.h:70
double sumYsquared
Definition: linreg.h:65
virtual double getA() const
Definition: linreg.h:54
double getCoefCorrel() const
Definition: linreg.h:58
long n
Definition: linreg.h:63
void Calculate()
Definition: linreg.cpp:31
double sumY
Definition: linreg.h:64
double sumXsquared
Definition: linreg.h:65
double coefC
Definition: linreg.h:70
double sumX
Definition: linreg.h:64
long items() const
Definition: linreg.h:52
double a
Definition: linreg.h:69
virtual double estimateY(double x) const
Definition: linreg.h:60
double b
Definition: linreg.h:69