楼主: Eviewschen
1536 2

[100 Multilevel Questions]How do I incorporate dummy's in R? [推广有奖]

  • 0关注
  • 2粉丝

硕士生

46%

还不是VIP/贵宾

-

TA的文库  其他...

Nvivo(定性数据分析)

Observational Study

Mathematica NewOccidental

威望
0
论坛币
3794 个
通用积分
1.4437
学术水平
14 点
热心指数
9 点
信用等级
11 点
经验
1458 点
帖子
102
精华
1
在线时间
19 小时
注册时间
2006-5-13
最后登录
2016-12-10

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
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!
二维码

扫码加我 拉你入群

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

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

关键词:Incorporate Multilevel questions Corporate question individual modifying because several special

沙发
Eviewschen 发表于 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.

使用道具

藤椅
Eviewschen 发表于 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.

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-22 23:40