I'm doing some work in R
using the gbm
package. I'm curious about the repercussions of treating categorical variables as factors or using 1/0 flags
for each individually. Is there any literature on how the 2 are handled either in gbm
or in R
in general?
1 Answer
In most cases, it will not make any difference if you create dummys yourself (I guess that is what flags means) or use a factor variable and let the software make the dummys. In both cases, a complete set of dummys will be linearly dependent, so you will need to use a reduced set. How you do the reduction changes the parameterization of the model, but not the predictions it produces.
If your software can do this for you, there should be little reason to do it "by hand".
If you have data, say, from a questionnaire of the type "mark any that applies", that could be represented by flags, but not by a factor variable. If you estimate a model using regularization (lasso/ridge/elastic net), then you should use the complete set of dummys, the regularization takes care of the problems caused by linear dependence, and which dummy you omitted could actually change the predictions! But good lasso software (like glmnet
) should take care of that.
contrasts
? $\endgroup$