简体   繁体   中英

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:

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

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.

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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