Gin
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
LinearRegression Class Reference

Performs linear regression to fit a straight line to data points. More...

#include <gin_linearregression.h>

Public Member Functions

 LinearRegression ()
 Creates an empty linear regression object.
 
 LinearRegression (juce::Array< Point< double > > points)
 Creates a linear regression object and adds the given points.
 
void addPoint (Point< double > pnt)
 Adds a data point to the regression.
 
void calculate ()
 Calculates the regression coefficients from the added points.
 
void clear ()
 Removes all data points and resets the regression.
 
bool haveData () const
 Checks if enough data points have been added.
 
int items () const
 Returns the number of data points.
 
double getA () const
 Returns the intercept of the fitted line.
 
double getB () const
 Returns the slope of the fitted line.
 
double getCoefDeterm () const
 Returns the coefficient of determination (R²).
 
double getCoefCorrel () const
 Returns the coefficient of correlation.
 
double getStdErrorEst () const
 Returns the standard error of estimate.
 
double estimateY (double x) const
 Estimates the y value for a given x using the fitted line.
 

Protected Attributes

int n = 0
 
double sumX = 0
 
double sumY = 0
 
double sumXsquared = 0
 
double sumYsquared = 0
 
double sumXY = 0
 
double a = 0
 
double b = 0
 
double coefD = 0
 
double coefC = 0
 
double stdError = 0
 

Detailed Description

Performs linear regression to fit a straight line to data points.

This class fits a linear equation y = a + bx to a set of data points using the least squares method. It calculates the coefficients (a, b) that minimize the sum of squared differences between the data points and the line.

The class also computes:

Example usage:

regression.addPoint (Point<double> (1.0, 2.3));
regression.addPoint (Point<double> (2.0, 4.1));
regression.addPoint (Point<double> (3.0, 5.9));
regression.calculate();
double a = regression.getA(); // Intercept
double b = regression.getB(); // Slope
double predicted = regression.estimateY (4.0); // Predict y for x=4
Performs linear regression to fit a straight line to data points.
Definition gin_linearregression.h:41
double a
Definition gin_linearregression.h:124
double b
Definition gin_linearregression.h:125
A lightweight 2D point class for projects that don't use juce_graphics.
Definition gin_point.h:25
See also
getA, getB, estimateY, getCoefDeterm

Constructor & Destructor Documentation

◆ LinearRegression() [1/2]

LinearRegression::LinearRegression ( )

Creates an empty linear regression object.

◆ LinearRegression() [2/2]

LinearRegression::LinearRegression ( juce::Array< Point< double > >  points)

Creates a linear regression object and adds the given points.

Parameters
pointsInitial set of data points

Member Function Documentation

◆ addPoint()

void LinearRegression::addPoint ( Point< double pnt)

Adds a data point to the regression.

Parameters
pntThe point to add

◆ calculate()

void LinearRegression::calculate ( )

Calculates the regression coefficients from the added points.

Must be called after adding points and before retrieving results.

◆ clear()

void LinearRegression::clear ( )

Removes all data points and resets the regression.

◆ haveData()

bool LinearRegression::haveData ( ) const

Checks if enough data points have been added.

Returns
true if more than 2 points have been added

References n.

◆ items()

int LinearRegression::items ( ) const

Returns the number of data points.

Returns
The number of points added

References n.

◆ getA()

double LinearRegression::getA ( ) const

Returns the intercept of the fitted line.

Returns
The 'a' coefficient in y = a + bx

References a.

◆ getB()

double LinearRegression::getB ( ) const

Returns the slope of the fitted line.

Returns
The 'b' coefficient in y = a + bx

References b.

◆ getCoefDeterm()

double LinearRegression::getCoefDeterm ( ) const

Returns the coefficient of determination (R²).

Returns
R² value (0 to 1), where 1 indicates perfect fit

References coefD.

◆ getCoefCorrel()

double LinearRegression::getCoefCorrel ( ) const

Returns the coefficient of correlation.

Returns
Correlation coefficient (-1 to 1)

References coefC.

◆ getStdErrorEst()

double LinearRegression::getStdErrorEst ( ) const

Returns the standard error of estimate.

Returns
The typical prediction error

References stdError.

◆ estimateY()

double LinearRegression::estimateY ( double  x) const

Estimates the y value for a given x using the fitted line.

Parameters
xThe x coordinate
Returns
The estimated y value

References a, and b.

Member Data Documentation

◆ n

int LinearRegression::n = 0
protected

Referenced by haveData(), and items().

◆ sumX

double LinearRegression::sumX = 0
protected

◆ sumY

double LinearRegression::sumY = 0
protected

◆ sumXsquared

double LinearRegression::sumXsquared = 0
protected

◆ sumYsquared

double LinearRegression::sumYsquared = 0
protected

◆ sumXY

double LinearRegression::sumXY = 0
protected

◆ a

double LinearRegression::a = 0
protected

Referenced by estimateY(), and getA().

◆ b

double LinearRegression::b = 0
protected

Referenced by estimateY(), and getB().

◆ coefD

double LinearRegression::coefD = 0
protected

Referenced by getCoefDeterm().

◆ coefC

double LinearRegression::coefC = 0
protected

Referenced by getCoefCorrel().

◆ stdError

double LinearRegression::stdError = 0
protected

Referenced by getStdErrorEst().


The documentation for this class was generated from the following file: