【问题标题】:Linear regression with conditional statement in RR中带有条件语句的线性回归
【发布时间】:2018-10-17 13:30:44
【问题描述】:

我有一个庞大的数据库,我需要使用条件语句运行不同的回归。 所以我看到了这样做的选项:1)在回归中包括命令数据子集(industrycodes==12)和2)我没有获得相同的结果,就像将数据切割成家具==12时的值一样。他们应该是一样的。 有人可以帮我写代码吗,我想我有这个问题。 我举了一个非常基本的例子来解释它。

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

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

我在这里运行回归:

1) 对于整个数据仅采用行业代码==12 --> 这里我有 6 个观察结果

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

2) 在行业代码==12 时切割样本 --> 这里当然我有 4 个观察结果

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

有什么想法可能是错的吗?谢谢!

【问题讨论】:

    标签: r conditional regression subset


    【解决方案1】:

    欢迎来到 StackOverflow,我在两种情况下的结果完全相同,我唯一更改的是用点“.”替换逗号“,”以正确指示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
    

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

    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]))))
    }
    

    【讨论】:

      【解决方案2】:

      问题在于,您首先指定了一个数据集(称为子集(data1,industrycodes==12)的数据集),然后在另一个数据集(data1 - 原始数据集)中运行 lm。

      额外的注释是,由于您在 lm 中使用命令 data=... ,因此您不必使用 $ 指定变量,它在函数 lm attach 命令中起作用。

      试试这个:

      data3 <- subset(data1,industrycodes==12)
      
      
      summary(lm(roa~employees, data=data3))
      

      希望有效果

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-05
        • 1970-01-01
        • 2014-05-08
        • 2020-11-25
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 1970-01-01
        相关资源
        最近更新 更多