Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

Linear regression with conditional statement in R

I have a huge database and I need to run different regressions with conditional statements. So I see to options to do it: 1) in the regression include the command data subset (industrycodes==12) and 2) I don't obtain the same results as if cut the data to the values when furniture==12. And they should be the same. Could somebody help me with the codes, I think I have a problem with this. I put an example very basic to explain it.

ID  roa   employees    industrycodes
1   0,5      10              12
2   0,3      20              11
3   0,8      15              12
4   0,2      12              12
5   0,7      13              11
6   0,4       8              12

so first I create the subdatabase to compare (when the industry code is 12)

data2<-data1[data1$industrycodes==12,]

and here I run the regressions:

1) for the whole data taking only industrycodes==12 --> here I have the 6 observations

summary(lm(data1$roa~data1$employees, data=subset(data1,industrycodes==12)))  

2) cutting the sample when the industrycode==12 --> here of course I have 4 observations

summary(lm(data2$roa~data2$employees),data=data2)

Any ideas of what can be wrong?? Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that in the first you specify a dataset ( the one called subset(data1,industrycodes==12)) but then run the lm in another datset (data1 - the original one).

An extra comment is that since you use the command data=... in the lm you do not have to specify the variables with the $ , it works as an in the function lm attach command.

try this:

data3<- subset(data1,industrycodes==12)

summary(lm(roa~employees, data=data3) )

Hope it works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...