简体   繁体   English

如何在 R 的回归中设置虚拟变量

[英]how can I set a dummy variable in a regression in R

The following is my data以下是我的数据

y   r1  r2  r3

1   0.1 0.2 -0.3
2   0.7 -0.9    0.03
3   -0.93   -0.32   -0.22

1.The first question is how can I get the output like this: 1.第一个问题是我怎样才能得到这样的输出:

y   r1     r2    r3    dummy_r1  dummy_r2 dummy_r3

1   0.1    0.2   -0.3    0        0          1
2   0.7   -0.9   0.03    0        1          0
3   -0.93 -0.32  -0.22   1        1          1

Note:I want the negative data equals to 1, and the positive data equals to 0注:我要负数为1,正数为0

2.The second question is that if I want to do the regression like: lm(y~r1+r2+r3+dummy_r1+ dummy_r2+dummy_r3) ,what should I do if I don't want to use the output data(dummy_r1,dummy_r2,dummy_r3) above, because it is not convenient. 2.第二个问题是,如果我想做这样的回归: lm(y~r1+r2+r3+dummy_r1+ dummy_r2+dummy_r3) ,如果我不想使用输出data(dummy_r1,dummy_r2,dummy_r3)上面,因为不方便。

Using DF shown reproducibly in the Note at the end, define DF2 to also have the sign.* columns and then run the regression on that.使用末尾注释中可重现的DF ,将DF2定义为也具有sign.*列,然后对其运行回归。 Of cousse you don't have enough data shown in the question to actually get coefficients for so many predictors but if in your real problem you have more data then it should be ok.当然,您在问题中没有足够的数据来实际获得这么多预测变量的系数,但是如果在您的实际问题中您有更多数据,那么应该没问题。

DF2 <- cbind(DF, sign = +(DF[-1] < 0))
lm(y ~., DF2)

giving:给予:

Call:
lm(formula = y ~ ., data = DF2)

Coefficients:
(Intercept)           r1           r2           r3      sign.r1  
      1.425       -1.163       -1.543           NA           NA  
    sign.r2      sign.r3  
         NA           NA  

Note笔记

Lines <- "y   r1  r2  r3
1   0.1 0.2 -0.3
2   0.7 -0.9    0.03
3   -0.93   -0.32   -0.22"
DF <- read.table(text = Lines, header = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM