全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 HLM专版
1691 2
2014-03-17
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!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-3-17 23:35:22
To make R recognize that your variable is categorical and let it create dummies when building a model, the variable should be of class factor. You can check the class by class(object), or by str(df) if its a data frame. If it is not a factor but a numeric variable you can convert it by the factor() command. Show your code if things are not working out still.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-3-17 23:36:04

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.

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群