Lauren Eeckhout
Lauren Eeckhout

Reputation: 1

How do I incorporate dummy's in R?

I made several dummy variables in SPSS and was wondering if I could use the same ones in R? Is there any special code you need to put before the variables (like factor(...) for categorical) or can you just add them to your model without modifying?

I need to use R because I'm doing a multilevel analysis and I want to use both individual as contextual variables.

Thanks in advance for your help! Kind regards, Lauren

Upvotes: 0

Views: 293

Answers (1)

Maxim.K
Maxim.K

Reputation: 4180

You have several options here. First, as suggested by @Edwin, you could use the factor() function to convert a categorical variable to the factor class. R will automatically dummify factors in most linear models (this can be seen by inspecting the model object).

Of course, I don't think anything precludes you from simply including the 1/0 dummies in linear models, as they are allowed per definition. Of course, then you need to manually keep track of them, in relation to their meaning and reference category.

Therefore, the first strategy is probably the most convenient.

To somewhat expand the answer, there are several dummification options in R. The default is dummy coding achieved with contr.treatment(). This will use 1/0 dummies, and it is the default option for unordered factors. See the respective options() output part to verify. There are nevertheless other types of contrasts that can be of use. Deviation coding, in my experience, is often a good candidate. It differs from the default manner of contrast setting in that instead of a single reference category a group mean is taken for pairwise comparisons. This may be interesting in cases like comparing between countries (or states), when there is no intrinsic reason to compare everything to one specific country, but comparison to the mean of all countries makes sense. This type of contrast is set by contr.sum(). Alternatively, you can construct your own custom matrix by hand and supply it by contrasts(x) <- MyMatrix.

More information on contrast types in R can be found here.

Upvotes: 1

Related Questions