SourcePro® API Reference Guide

 
Loading...
Searching...
No Matches
RWLinearRegression Class Reference

Constructs a linear regression model from a matrix of predictor variable data and a vector of observation variable data. More...

#include <rw/analytics/linregress.h>

Inheritance diagram for RWLinearRegression:
RWRegression< double, double >

Public Member Functions

 RWLinearRegression ()
 
 RWLinearRegression (const RWGenMat< double > &predictorData, const RWMathVec< double > &obsVec, const RWRegressionCalc< double, double > &c, InterceptOption interceptOpt=addIntercept)
 
 RWLinearRegression (const RWGenMat< double > &predictorData, const RWMathVec< double > &obsVec, InterceptOption interceptOpt=addIntercept)
 
 RWLinearRegression (const RWLinearRegression &lr)
 
RWLinearRegressionoperator= (const RWLinearRegression &lr)
 
RWGenMat< double > paramDispersionMatrix () const
 
RWTValVector< RWLinearRegressionParamparameterEstimates () const
 
RWMathVec< double > predictedObservation (const RWGenMat< double > &m) const
 
double predictedObservation (const RWMathVec< double > &v) const
 
RWInterval< double > predictionInterval (const RWMathVec< double > &x, double alpha) const
 
RWMathVec< double > residuals () const
 
double variance () const
 
- Public Member Functions inherited from RWRegression< double, double >
void addInterceptParameter ()
 
void addObservation (const RWMathVec< double > &, double)
 
void addObservations (const RWGenMat< double > &, const RWMathVec< double > &)
 
void addPredictor (const RWMathVec< double > &)
 
void addPredictors (const RWGenMat< double > &)
 
const RWRegressionCalc< double, double > & calcMethod () const
 
bool fail () const
 
bool hasInterceptParameter () const
 
size_t numObservations () const
 
size_t numParameters () const
 
size_t numPredictors () const
 
RWMathVec< double > observationVector ()
 
const RWMathVec< double > observationVector () const
 
const RWMathVec< double > parameters () const
 
RWGenMat< double > predictorMatrix ()
 
const RWGenMat< double > predictorMatrix () const
 
void reCalculateParameters ()
 
const RWGenMat< double > regressionMatrix () const
 
void removeInterceptParameter ()
 
void removeObservations (size_t startingIndex, size_t numToRemove)
 
void removePredictors (size_t startingIndex, size_t numToRemove)
 
void setCalcMethod (const RWRegressionCalc< double, double > &c, bool recalculate=true)
 
void setRegressionData (const RWGenMat< double > &r, const RWMathVec< double > &o, InterceptOption interceptOpt=addIntercept)
 

Additional Inherited Members

- Public Types inherited from RWRegression< double, double >
enum  InterceptOption
 

Detailed Description

Class RWLinearRegression constructs a linear regression model from a matrix of predictor variable data and a vector of observation variable data. The class implements multiple linear regression as described in Section 3.2, "Multiple Linear Regression," in the Business Analysis Module User's Guide.

The class makes several assumptions regarding the predictor matrix and observation vector passed to the constructor. Columns in the predictor matrix correspond to predictor variables, and the matrix rows correspond to predictor patterns. Both predictor matrix and observation vector are double precision, and the length of the observation vector should equal the number of rows in the predictor matrix. The user has the choice of specifying whether the model has an intercept parameter, and if so, whether the provided predictor matrix includes a column of 1s for the intercept parameter.

You can choose from among several regression parameter calculation methods, including QR decomposition without pivoting (RWLeastSqQRCalc), QR decomposition with pivoting (RWLeastSqQRPvtCalc), and singular value decomposition (RWLeastSqSVDCalc). The calculation method is set by passing a calculation class instance to the method setCalcMethod(). You have the option of providing your own implementation derived from the class RWRegressionCalc<double,double>.

Once the linear regression object is constructed, it can be queried for specific values related to linear regression, such as model parameters, model predictions, prediction intervals, and confidence intervals for parameter values. The model may also be updated by changing values in the predictor matrix and observation vector, or by changing the calculation method.

Synopsis
#include <rw/analytics/linregress.h>
#include <rw/math/genmat.h>
#include <rw/math/mathvec.h>
RWMathVecd<double> obsVec;
RWLinearRegression lr(predMat, obsVec);
A templatized general matrix class.
Definition genmat.h:741
Constructs a linear regression model from a matrix of predictor variable data and a vector of observa...
Definition linregress.h:96
Example

This simple example prints out the calculated parameter values for a linear regression model.

#include <rw/analytics/linregress.h>
int main() {
"5x2 [1.2 2.1 8 7 3 3.2 6.4 4.6 2 2.3]";
RWMathVec<double> observationVector = "[2.5 3.7 1.4 2.3 5.6]";
// Print out the model parameters if the calculation succeeded.
if (!lr.fail()) {
cout << "Model parameters: " << lr.parameters() << endl;
} else {
cout << "Parameter calculation failed." << endl;
}
return 0;
}
A templatized vector class.
Definition mathvec.h:667
const RWGenMat< double > predictorMatrix() const
Definition regress.h:120
const RWMathVec< double > observationVector() const
Definition regress.h:148

Constructor & Destructor Documentation

◆ RWLinearRegression() [1/4]

RWLinearRegression::RWLinearRegression ( )
inline

Constructs an empty linear regression object. Behavior is undefined.

◆ RWLinearRegression() [2/4]

RWLinearRegression::RWLinearRegression ( const RWLinearRegression & lr)
inline

Constructs a copy of lr.

◆ RWLinearRegression() [3/4]

RWLinearRegression::RWLinearRegression ( const RWGenMat< double > & predictorData,
const RWMathVec< double > & obsVec,
InterceptOption interceptOpt = addIntercept )
inline

Constructs a regression object from the given predictor data matrix and observation vector. The intercept option parameter indicates whether or not the model contains an intercept parameter. The parameter calculation is done using the QR decomposition method.

◆ RWLinearRegression() [4/4]

RWLinearRegression::RWLinearRegression ( const RWGenMat< double > & predictorData,
const RWMathVec< double > & obsVec,
const RWRegressionCalc< double, double > & c,
InterceptOption interceptOpt = addIntercept )
inline

Constructs a regression object from the given predictor data matrix and observation vector. The intercept option parameter indicates whether or not the model contains an intercept parameter. The parameter calculation is done using the method defined by the regression calculation object c.

Member Function Documentation

◆ operator=()

RWLinearRegression & RWLinearRegression::operator= ( const RWLinearRegression & lr)
inline

Copies the contents of lr to self.

◆ paramDispersionMatrix()

RWGenMat< double > RWLinearRegression::paramDispersionMatrix ( ) const

Returns the dispersion matrix for the parameter estimates. This is also known as the parameter variance-covariance matrix.

◆ parameterEstimates()

RWTValVector< RWLinearRegressionParam > RWLinearRegression::parameterEstimates ( ) const

Returns the list of estimated parameters.

◆ predictedObservation() [1/2]

RWMathVec< double > RWLinearRegression::predictedObservation ( const RWGenMat< double > & m) const

Returns the predicted response vector, \(\mathbf{\hat{Y}}\) , as defined in Section 3.2, "Multiple Linear Regression," in the Business Analysis Module User's Guide.

◆ predictedObservation() [2/2]

double RWLinearRegression::predictedObservation ( const RWMathVec< double > & v) const

Returns the predicted response, \(\mathbf{\hat{Y}}\) , as defined in Section 3.2, "Multiple Linear Regression," in the Business Analysis Module User's Guide.

◆ predictionInterval()

RWInterval< double > RWLinearRegression::predictionInterval ( const RWMathVec< double > & x,
double alpha ) const

Returns an alpha level confidence interval for the response predicted by the regression at x as defined in Section 3.2, "Multiple Linear Regression," in the Business Analysis Module User's Guide.

◆ residuals()

RWMathVec< double > RWLinearRegression::residuals ( ) const

Returns the vector of residuals \(\mathbf{e = Y - \hat{Y}}\) See Section 3.2, "Multiple Linear Regression," in the Business Analysis Module User's Guide.

◆ variance()

double RWLinearRegression::variance ( ) const

Returns the variance for the model. This is the residual sum of squares divided by its degrees of freedom.

Copyright © 2024 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.