R中带有条件语句的线性回归 [英] Linear regression with conditional statement in R

查看:404
本文介绍了R中带有条件语句的线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的数据库,我需要使用条件语句运行不同的回归。
因此,我看到了执行此操作的选项:1)在回归中包括命令数据子集(industrycodes == 12)和2)我获得的结果不像在家具时将数据剪切为值== 12。它们应该是相同的。
有人可以帮助我提供代码,我想我对此有疑问。
我举了一个非常基本的示例来解释它。

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

所以首先我创建要比较的子数据库(行业代码为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)整个数据仅采用行业代码== 12->在这里,我有6个观察点

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)在行业代码== 12时切割样本->当然我有4个观察点

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!

推荐答案

欢迎来到StackOverflow,两种情况下我的结果完全相同,唯一的改变就是用点 替换逗号 以正确指示 roa中的小数位

Welcome to StackOverflow, I am having exactly the same results for both cases, the only thing I changes was to replace the commas "," by points "." to correctly indicate decimal places in roa

data1

  ID roa employees industrycodes
1  1 0.5        10            12
2  2 0.3        20            11
3  3 0.8        15            12
4  4 0.2        12            12
5  5 0.7        13            11
6  6 0.4         8            12

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

第一例结果:

    Call:
lm(formula = data1$roa ~ data1$employees, data = subset(data1, 
    industrycodes == 12))

Residuals:
       1        2        3        4        5        6 
 0.01667 -0.18333  0.31667 -0.28333  0.21667 -0.08333 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)
(Intercept)      4.833e-01  3.742e-01   1.292    0.266
data1$employees -5.918e-18  2.761e-02   0.000    1.000

Residual standard error: 0.259 on 4 degrees of freedom
Multiple R-squared:  8.039e-32, Adjusted R-squared:  -0.25 
F-statistic: 3.215e-31 on 1 and 4 DF,  p-value: 1
data2 <- data1[data1$industrycodes==12,]

第二例结果:

summary(lm(data1$roa~data1$employees, data=data2))
Call:
lm(formula = data1$roa ~ data1$employees, data = data2)

Residuals:
       1        2        3        4        5        6 
 0.01667 -0.18333  0.31667 -0.28333  0.21667 -0.08333 

Coefficients:
                  Estimate Std. Error t value Pr(>|t|)
(Intercept)      4.833e-01  3.742e-01   1.292    0.266
data1$employees -5.918e-18  2.761e-02   0.000    1.000

Residual standard error: 0.259 on 4 degrees of freedom
Multiple R-squared:  8.039e-32, Adjusted R-squared:  -0.25 
F-statistic: 3.215e-31 on 1 and 4 DF,  p-value: 1

如果要遍历所有条件,可以添加新列。例如,如果您有两个条件:

If you want to loop across all conditions you could add new columns. For example if you have two conditions:

data1$cond1 <- data1$industrycodes==12
data1$cond2 <- data1$industrycodes<=12

然后可以使用循环:

for( i in 5:6) {
  print(summary(lm(data1$roa~data1$employees, data=subset(data1,data1[,i]))))
}

这篇关于R中带有条件语句的线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆