>
RWLinearRegression RWRegression<double,double>
Data Types | |
InterceptOption |
>#include <rw/math/mathvec.h> #include <rw/math/genmat.h> #include <rw/analytics/linregress.h> RWGenMat<double> predMat; RWMathVecd<double> obsVec; RWLinearRegression lr(predMat, obsVec);
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.
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.
>This simple example prints out the calculated parameter values for a linear regression model.
#include <rw/analytics/linregress.h> main() { RWGenMat<double> predictorMatrix = "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]"; RWLinearRegression lr(predictorMatrix, observationVector); // 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; }>
enum InterceptOption {noIntercept, intercept, addIntercept}
Used at construction time to indicated whether the model has:
no intercept
an intercept with an input matrix that contains a column of 1s prepended to the predictor data matrix
an intercept that does not contain a column of 1s prepended to the predictor matrix.
RWLinearRegression();
Constructs an empty linear regression object. Behavior is undefined.
RWLinearRegression(const RWLinearRegression& d);
Constructs a copy of d.
RWLinearRegression(const RWGenMat<double>& predictorData, const RWMathVec<double>& observationVector, InterceptOption intOpt=addIntercept);
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(const RWGenMat<double>& predictorData, const RWMathVec<double>& observationVector, const RWRegressionCalc<double,double>& c, InterceptOption intOpt=addIntercept);
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.
void addInterceptParameter();
Adds an intercept parameter to the model and recalculates the parameters. If the model already has an intercept parameter, this function does nothing.
void addObservation(const RWMathVec<double>& observedPredictorValues, double observation);
Appends the given row of observed predictor variable values to the predictor data matrix, appends the observation value to the observation vector, then recalculates the model parameters. The input predictor variable values should not contain a leading 1 for the intercept parameter, regardless of whether or not the model has an intercept parameter. The model accounts for a leading 1 that corresponds to the intercept parameter.
void addObservations(const RWGenMat<double>& observedPredictorValues, const RWMathVec<double>& observations);
Appends the given rows of observed predictor variable values to the predictor data matrix, appends the observation values to the observation vector, then recalculates the model parameters. The input predictor variable values should not contain a leading 1 for the intercept parameter, regardless of whether or not the model has an intercept parameter. The model accounts for any leading 1s that correspond to the intercept parameter.
void addPredictor(const RWMathVec<double>& predictorValues);
Appends the given column of predictor values to the predictor data matrix and recalculates the regression.
void addPredictors(const RWGenMat<double>& predictorValues);
Appends the given columns of predictor values to the predictor data matrix and recalculates the regression.
const RWRegressionCalc<double,double>& calcMethod() const;
Returns the calculation method currently in use.
RWBoolean fail() const;
Returns TRUE if the most recent parameter calculation fails. Returns FALSE if the calculation is successful.
RWBoolean hasInterceptParameter() const;
Returns TRUE if the model has an intercept parameter, otherwise returns FALSE.
size_t numObservations() const;
Returns the length, n, of the vector input response vector, .
size_t numParameters() const;
Returns the number of parameters being estimated. If the model contains an intercept parameter, this is the same as the number of predictor variables plus 1.
size_t numPredictors() const;
Returns the number of predictor variables in the model. If the model contains an intercept parameter, this is 1 less than the number of parameters. If the model does not contain an intercept parameter, this is the same as the number of parameters.
const RWMathVec<double> observationVector() const;
Returns the current observation vector, .
RWMathVec<double> observationVector();
Non-const version of the observationVector() function. May be used to modify .
RWGenMat<double> paramDispersionMatrix() const;
Returns the dispersion matrix for the parameter estimates. This is also known as the parameter variance-covariance matrix.
RWTValSlist< RWLinearRegressionParam > parameterEstimates() const;
Returns the list of estimated parameters.
const RWMathVec<double> parameters() const;
Returns , the vector of estimated parameter values.
double predictedObservation(const RWMathVec<double>& x ) const;
Returns the predicted response, , as defined in Section 3.2.
RWMathVec<double> predictedObservation(const RWGenMat<double>& r) const;
Returns the predicted response vector, , as defined in Section 3.2.
RWInterval<double> 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.6.
const RWGenMat<double> predictorMatrix() const;
Returns the predictor data matrix, , as defined in Section 3.2.1. If the model does not have an intercept parameter, this method is the same as the regressionMatrix() method. If the model does contain an intercept parameter, be aware that the first column of the returned matrix contains the values for the first predictor variable, not the column of 1s associated with the intercept parameter.
RWGenMat<double> predictorMatrix();
Non-const version of the predictorMatrix() function. May be used to modify the contents of the predictor data matrix, . If the model does not have an intercept parameter, this method is the same as the regressionMatrix() method. If the model does contain an intercept parameter, be aware that the first column of the returned matrix contains the values for the first predictor variable, not the column of 1s associated with the intercept parameter.
void reCalculateParameters();
Recalculates the regression parameters. Should be called only if you modify the regression data through the methods observationVector() or predictorMatrix().
const RWGenMat<double> regressionMatrix() const;
Returns regression matrix X as defined in Section 3.2. Note that if the model does not contain an intercept parameter, this method returns the same matrix as the predictorMatrix() method.
void removeInterceptParameter();
Removes the intercept parameter from the model and recalculates the parameters. If the model currently has no intercept parameter, this function does nothing.
void removeObservations(size_t startingIndex, size_t numToRemove);
Removes numToRemove rows from the predictor data matrix beginning with row startingIndex, removes the corresponding elements from the observation vector, then recalculates the regression. The first row has index 0.
void removePredictors(size_t startingIndex, size_t numToRemove);
Removes numToRemove columns from the predictor data matrix beginning with column startingIndex, then recalculates the regression. The first column has an index of 0. Note that an index of 0 refers to the first predictor variable and not to the intercept parameter, if the model contains one.
RWMathVec<double> residuals() const;
Returns the vector of residuals . See Section 3.2.1.
void setCalcMethod(const RWRegressionCalc<double,double>& c, RWBoolean recalculate=TRUE);
Sets the regression calculation object that is used for computing model parameters to c. Recalculates the model parameters if recalculate is TRUE.
void setRegressionData(const RWGenMat<double>& predictorData, const RWMathVec<double>& observationVector, InterceptOption intOpt=addIntercept);
Sets the predictor matrix, observation vector, and intercept option to the specified values.
double variance() const;
Returns the variance for the model. This is the residual sum of squares divided by its degrees of freedom.
RWLinearRegression& operator=(const RWLinearRegression& r);
Copies the contents of r to self.
©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.