【问题标题】:Dealing with nested variables in R linear regression在 R 线性回归中处理嵌套变量
【发布时间】:2021-07-20 00:00:56
【问题描述】:

我有一个包含一些嵌套变量的数据集。例如,我有以下变量: 一辆车的speed,是否有另一辆车跟随它other_car,如果有另一辆车,两辆车之间的距离distance。虚拟数据集:

speed <- c(30,50,60,30,33,54,65,33,33,54,65,34,45,32)
other_car <- c(0,1,0,0,0,1,1,1,1,0,1,0,1,0)
distance <- c(NA,20,NA,NA,NA,21,5,15,17,NA,34,NA,13,NA)

dft <- data.frame(speed,other_car,distance)

我想以嵌套变量的形式将变量 other_cardistance 包含在模型中,即如果汽车存在,还要考虑距离。按照这里提到的方法:https://stats.stackexchange.com/questions/372257/how-do-you-deal-with-nested-variables-in-a-regression-model,我尝试了以下方法:

dft <- data.frame(speed,other_car,distance)
dft$other_car<-factor(dft$other_car)

lm_speed <- lm(speed ~ dft$other_car + dft$other_car:dft$distance)
summary(lm_speed)

这给出了以下错误:

contrasts&lt;-(*tmp*, value = contr.funs[1 + isOF[nn]]) 中的错误:对比只能应用于具有 2 个或更多级别的因子

有什么想法吗?

【问题讨论】:

    标签: r nested linear-regression


    【解决方案1】:

    这是因为other_car==0时,距离都等于NAsee

    dft$distance[dft$other_car==0]
    [1] NA NA NA NA NA NA NA
    

    您可以指定一个恒定距离来将NA 替换为other_car==0,以便模型使用因子other_car==0 并发现该距离对该子集没有影响:

    dft$distance[dft$other_car==0]<-0
    
    dft$other_car<- factor(dft$other_car)
    
    lm_speed <- lm(speed ~ other_car + other_car:distance, data = dft)
    summary(lm_speed)
    
    Call:
    lm(formula = speed ~ other_car + other_car:distance, data = dft)
    
    Residuals:
        Min      1Q  Median      3Q     Max 
    -16.015  -8.500  -3.876   8.894  21.000 
    
    Coefficients: (1 not defined because of singularities)
                        Estimate Std. Error t value Pr(>|t|)    
    (Intercept)          39.0000     5.0405   7.737 8.96e-06 ***
    other_car1            4.6480    13.0670   0.356    0.729    
    other_car0:distance       NA         NA      NA       NA    
    other_car1:distance   0.3157     0.6133   0.515    0.617    
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 13.34 on 11 degrees of freedom
    Multiple R-squared:  0.1758,    Adjusted R-squared:  0.026 
    F-statistic: 1.174 on 2 and 11 DF,  p-value: 0.3452
    
    

    另一种解决方法是将factor 转换为numeric,但这是isn't the same model

    speed <- c(30,50,60,30,33,54,65,33,33,54,65,34,45,32)
    other_car <- c(0,1,0,0,0,1,1,1,1,0,1,0,1,0)
    distance <- c(NA,20,NA,NA,NA,21,5,15,17,NA,34,NA,13,NA)
    
    dft <- data.frame(speed,other_car,distance)
    
    
    
    dft$other_car<- as.numeric(factor(dft$other_car))
    
    lm_speed <- lm(speed ~ other_car + other_car:distance, data = dft)
    summary(lm_speed)
    
    Call:
    lm(formula = speed ~ other_car + other_car:distance, data = dft)
    
    Residuals:
            2         6         7         8         9        11        13 
      0.03776   3.72205  19.77341 -15.38369 -16.01511  10.61782  -2.75227 
    
    Coefficients: (1 not defined because of singularities)
                       Estimate Std. Error t value Pr(>|t|)  
    (Intercept)         43.6480    12.9010   3.383   0.0196 *
    other_car                NA         NA      NA       NA  
    other_car:distance   0.1579     0.3281   0.481   0.6508  
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    Residual standard error: 14.27 on 5 degrees of freedom
      (7 observations deleted due to missingness)
    Multiple R-squared:  0.04424,   Adjusted R-squared:  -0.1469 
    F-statistic: 0.2314 on 1 and 5 DF,  p-value: 0.6508
    

    这说明速度会随着与其他汽车的距离而增加(或者反过来,当其他汽车离得太近时,司机往往会放慢速度)。

    【讨论】:

    • 感谢您的回答。如果我做对了,在第二个模型中,我们可以看到速度随着距离的增加而增加,但我们不能根据变量 other_car 比较速度? (即不仅包括交互 other_car:distance ,还考虑变量 other_car 作为因子变量)。遵循第一种方法但将 0 分配给所有 NA 是否准确?
    • 为所有NAs 分配相同的常量值确实有效。我的直觉是取一个高值而不是 0,因为这就像后面没有车,但这没有区别
    猜你喜欢
    • 2014-10-31
    • 2018-07-19
    • 2019-04-25
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2014-05-08
    • 2020-11-25
    • 1970-01-01
    相关资源
    最近更新 更多