Reputation: 93
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.
Upvotes: 0
Views: 177
Reputation: 269421
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
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)
Upvotes: 1