0
$\begingroup$

Hey I want to build a model in R and one of my idependent variable is categorical (it takes 10 different values). I change the type of this variable from "char" to factor and build a model in R by lm formula. Then I used step function to choose the model with lower number of variables, but this step function didn't delete any from 10 different values of my categorical variable. The opposite situation is when I create dummy variables for this variable and in this case after step function I get only 2 dummy varialbes. So I always have to split my variable into dummy variables because R don't do this for me?

$\endgroup$
1
  • 1
    $\begingroup$ The best solution to this problem is probably not to do stepwise predictor selection at all. See this page among many others on tis site. $\endgroup$
    – EdM
    May 21, 2021 at 12:42

1 Answer 1

1
$\begingroup$

The function step does not delete any level of your variable because it considers all the dummies as part of one single variable whose (at least one) coefficients turn out to be significant, that is different from 0.

If you manually build up the dummies the function step does not recognise them as part of a single variable so tests if every single coefficient is equal to 0.

The usual approach I take is to verify with the stepwise regression approach which variables should be included in the model. Then check the summary of the selected model and decide whether I should rearrange the categories of the categorical variables. As an example, if you have a lot of categories and only few of them are associated with significant coefficients you can "collapse" the non significant ones into one category.

$\endgroup$
2
  • $\begingroup$ thanks for your answer! So if I want to reduce number of variables in my model I have to create dummy variables? You write that you "collapse" the non significant ones into one category, but in that case, will our matrix not be invertible? If we don't include these non significant variables in our model, are they included in intercept term? $\endgroup$
    – Math122
    May 21, 2021 at 20:22
  • 1
    $\begingroup$ You can use a categorical variable (factor) builded from the original one with all the categories you want to model plus one for all the others. e.g. a variable with categories: c("north", "south", "east", and "west") in which only "north" and "south" are significant can be considered as a new factor with levels "north", "south", and "east-west". One of these categories will be your reference category and its effect will be considered in the intercept (otherwise you have to remove the intercept). Working with factors avoids you to worry about collinearity. $\endgroup$ May 24, 2021 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.