>
RWLogisticRegression RWRegression<double,RWBoolean>
Data Types | |
InterceptOption |
>#include <rw/analytics/logregress.h> RWMathVec<RWBoolean> o; RWGenMat<double> p; RWLogisticRegression lr(p, o);
RWLogisticRegression is the class for performing basic logistic regression, described in Section 3.3. RWLogisticRegression receives data input in the form of a matrix of values for the predictor variables, and a vector of values for the observations. In addition, an intercept option must be specified. The interception option is an enumeration type defined in the regression class. It can have three possible values:
intercept - The regression model contains an intercept parameter, and the input matrix of predictor variable values contains a leading column of 1s corresponding to the intercept parameter.
noIntercept - The regression model does not contain an intercept parameter.
addIntercept - The regression model has an intercept parameter, but it is not represented in the input predictor data matrix as a leading column of 1s. In this case, the RWLogisticRegression class prepends a column of 1s to the input predictor data matrix to obtain the full regression matrix.
For a more detailed discussion of the intercept option, see Section 5.2.2.
>#include <rw/analytics/logregress.h> #include <rw/rstream.h> int main() { RWGenMat<double> predData = "5x2 [1 234 2 431 3 333 4 654 5 788]"; RWMathVec<RWBoolean> obsData(5, rwUninitialized ); obsData[0] = obsData[3] = obsData[4] = TRUE; obsData[1] = obsData[2] = FALSE; // Create a logistic regression object. RWLogisticRegression model(predData, obsData); if ( model.fail() ) { cout << "Parameter calculation failed" << endl; } else { // Print parameters from the full model. cout << "Model parameters: " << model.parameters() << endl; } return 0; }>
enum InterceptOption {noIntercept, intercept, addIntercept}
Used at construction time to indicate whether the model:
has no intercept
has an intercept and the input matrix contains a column of 1s prepended to the predictor data matrix
has an intercept but does not contain a column of 1s prepended to the predictor matrix.
RWLogisticRegression();
Constructs an empty logistic regression object. Behavior is undefined.
RWLogisticRegression(const RWLogisticRegression<T>& d);
Constructs a copy of d.
RWLogisticRegression(const RWGenMat<double>& predictorData, const RWMathVec<RWBoolean>& obsVector, InterceptOption intOpt=addIntercept);
Constructs a regression object with predictor matrix predictorData and observation vector obsVector. The intercept option parameter indicates whether or not the model contains an intercept parameter. The default parameter calculation method is iterative least squares.
RWLogisticRegression(const RWGenMat<double>& data, const RWMathVec<RWBoolean>& y, const RWRegressionCalc<double,RWBoolean>& c, InterceptOption intOpt=addIntercept);
Constructs a regression object with predictor matrix data and observation vector y. The intercept option parameter indicates whether or not the model contains an intercept parameter. Parameter calculation is done using the method c. Methods currently available include iterative least squares and an algorithm based on the Levenberg-Marquardt approach.
void addInterceptParameter();
Adds an intercept parameter to the model and recalculates the parameters. If the model already has an intercept parameter, nothing is done.
void addObservation(const RWMathVec<double>& observedPredictorValues, RWBoolean 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. Predictor values should not contain a leading 1 for the intercept parameter, regardless of whether or not the model has an intercept parameter.
void addObservations(const RWGenMat<double>& observedPredictorValues, const RWMathVec<RWBoolean>& 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. Predictor values should not contain a leading 1 for the intercept parameter, regardless of whether or not the model has an 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,RWBoolean>& calcMethod() const;
Returns the current calculation method.
RWBoolean fail() const;
Returns TRUE if the most recent parameter calculation failed. Returns FALSE if the calculation was 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 response vector, .
size_t numParameters() const;
Returns the number of parameters being estimated. If the model contains an intercept parameter, the number of parameters 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, the number of predictor values is 1 less than the number of parameters. If the model does not contain an intercept parameter, the number of predictor values is the same as the number of parameters.
const RWMathVec<RWBoolean> observationVector() const;
Returns the current observation vector .
RWMathVec<RWBoolean> observationVector();
Non-const version of the observationVector() function. May be used to modify .
RWGenMat<double> paramDispersionMatrix() const;
Returns the parameter variance-covariance matrix.
RWTValVector<RWLogisticRegressionParam> parameterEstimates() const;
Returns the list of estimated parameters.
const RWMathVec<double> parameters() const;
Returns , the vector of estimated parameter values.
RWMathVec<double> predictedProbSuccess() const;
Returns the success probabilities predicted for the predictor data used to construct the model.
double predictedProbSuccess(const RWMathVec<double>& v) const; RWMathVec<double> predictedProbSuccess(const RWGenMat<double>& m) const;
Returns the model's predicted success probabilities for the input predictor data.
const RWGenMat<double> predictorMatrix() const;
Returns predictor data matrix as defined in Section 3.3. If the model does not have in intercept parameter, this method is the same as the regressionMatrix() method. If the model contains 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 contains 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.
NOTE:If you use this function to modify the predictor data, you must call reCalculateParameters().
void reCalculateParameters();
Recalculates the regression parameters. Applications should call this function only if they modify the regression data through the methods observationVector() or predictorMatrix().
const RWGenMat<double> regressionMatrix() const;
Returns a regression matrix as defined in Section 3.3. 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, and 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 and recalculates the regression. The first column has 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 differences between the probabilities of success predicted by the model and those predicted by the observed value.
void setCalcMethod(const RWRegressionCalc<double,RWBoolean>& c, RWBoolean recalculate=TRUE);
Sets the regression calculation object to be used for computing model parameters to c. Recalculates the model parameters if recalculate is TRUE.
void setRegressionData(const RWGenMat<T>& r, const RWMathVec<S>& o, InterceptOption interceptOpt=addIntercept);
Sets the data for the logistic regression.
RWMathVec<double> variance() const;
Returns the variance estimate for each observation. If is the predicted probability of success for the ith observation, then .
RWLogisticRegression& operator=(const RWLogisticRegression& r);
Copies the contents of r to self.
©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.