Intercept Option
When constructing a regression object, you must specify an intercept option for the model along with the predictor and observation data. The intercept option is an enumeration
interceptOption defined in the base class
RWRegression. The enumeration has three possible values:
noIntercept means the model does not have an intercept. In this case, the input data provided at construction time is the regression matrix. Once the regression object is constructed, calls to the
RWLinearRegression and
RWLogisticRegression methods
predictorMatrix() and
regressionMatrix() yield the same matrices.
addIntercept means the model has an intercept parameter, but it is not represented in the input data matrix as a leading column of 1s. When you specify this option, a column of 1s is
prepended to the input data matrix to obtain the regression matrix. In this case, calls to the
RWLinearRegression and
RWLogisticRegression methods
predictorMatrix() and
regressionMatrix() will yield different results. Column 0 of the regression matrix will contain all 1s for the intercept parameter, and columns 1 through
n-1, where
n is the number of predictors in the model, will contain the predictor matrix.
intercept means the model has an intercept parameter, which is represented in the input data matrix as a column of 1s. In this case, the column of 1s representing the intercept parameter must be the first column. If the
intercept option is specified and the first column is not 1s, an error will occur.
If the intercept option isn’t specified at construction time, it defaults to
addIntercept. The intercept parameter may be added or removed from the regression model at any time using the
RWLinearRegression and
RWLogisticRegression methods
addInterceptParameter() and
removeInterceptParameter().
The following examples demonstrate each of the intercept options, using linear regression:
// m1: the regression model has one predictor variable and an
// intercept parameter:
// y = B0 + B1x1
// The leading column of 1s is included in the data.
// m1 = [ 1 2
// 1 5
// 1 6 ]
RWGenMat<double> m1( "3x2 [1 2 1 5 1 6]" );
RWMathVec<double> y1( "[ 1 4 9]" );
// m2: the regression model has one predictor variable and an
// intercept parameter:
// y = B0 + B1x1
// The leading column of 1s is NOT included in the data.
// m2 = [ 2
// 5
// 6 ]
RWGenMat<double> m2( "3x1 [2 5 6]" );
RWMathVec<double> y2( "[ 1 4 9]" );
// m3: the regression model has two predictor variables and NO
// intercept parameter:
// y = B0x0 + B1x1
// The leading column of 1s is included in the data.
// m3 = [ 3 2
// 9 5
// 7 6 ]
RWGenMat<double> m3( "3x2 [3 2 9 5 7 6]" );
RWMathVec<double> y3( "[ 1 4 7]" );
// Model 1 contains an intercept parameter that is represented by a
// leading column of 1s in the input data matrix m1.
RWLinearRegression model1( m1, y1, RWLinearRegression::intercept );
cout << "Number of predictors: " << model1.numPredictors() << endl;
cout << "Number of parameters: " << model1.numParameters() << endl;
// Model 2 contains an intercept parameter, but the input data
// matrix m2 contains only the data for the predictor variable.
// The class should add a leading column of 1s when
// forming the regression matrix.
RWLinearRegression model2( m2, y2 );
// This is equivalent to:
// RWLinearRegression model2( m2, y2, RWLinearRegression::addIntercept);
// since the default argument for the third parameter in the
// constructor is RWLinearRegression::addIntercept.
cout << "Number of predictors: " << model2.numPredictors() << endl;
cout << "Number of parameters: " << model2.numParameters() << endl;
// Model 3 does not contains an intercept parameter. The input data
// matrix m2 should be used as the regression matrix.
RWLinearRegression model3( m3, y3, RWLinearRegression::noIntercept );
cout << "Number of predictors: " << model3.numPredictors() << endl;
cout << "Number of parameters: " << model3.numParameters() << endl;
.
.
.
Note that in the example above, objects model1 and model2 are identical.