【问题标题】:Regression models with categorical variable: dummy code or convert to factor具有分类变量的回归模型:虚拟代码或转换为因子
【发布时间】:2018-02-06 05:10:11
【问题描述】:

我知道这可能是一个有点傻的问题,但我想问的主要原因是因为我学过 DUMMY CODE!虚拟代码!虚拟代码!由多个班级的多名教师全部使用 R。

所以我对 ISLR 包中的 Auto 数据集做了这个比较。

library(ISLR)
Auto$c3 <- ifelse(Auto$cylinders == 3, 1, 0)
Auto$c4 <- ifelse(Auto$cylinders == 4, 1, 0)
Auto$c5 <- ifelse(Auto$cylinders == 5, 1, 0)
Auto$c6 <- ifelse(Auto$cylinders == 6, 1, 0)
Auto$c8 <- ifelse(Auto$cylinders == 8, 1, 0)
Auto$cylinders <- as.factor(Auto$cylinders)

summary(lm(mpg~displacement + cylinders, data = Auto))
summary(lm(mpg~displacement + c4 + c5 + c6 + c8, data = Auto))

Call:
lm(formula = mpg ~ displacement + cylinders, data = Auto)

Residuals:
    Min      1Q  Median      3Q     Max 
-10.692  -2.694  -0.347   2.157  20.307 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  24.33811    2.25278   10.80  < 2e-16 ***
displacement -0.05225    0.00693   -7.54  3.3e-13 ***
cylinders4   10.67609    2.23296    4.78  2.5e-06 ***
cylinders5   10.60478    3.39198    3.13   0.0019 ** 
cylinders6    7.04473    2.46493    2.86   0.0045 ** 
cylinders8    8.65170    2.92786    2.95   0.0033 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 4.39 on 386 degrees of freedom
Multiple R-squared:  0.687, Adjusted R-squared:  0.683 
F-statistic:  170 on 5 and 386 DF,  p-value: <2e-16

> summary(lm(mpg~displacement + c4 + c5 + c6 + c8, data = Auto))

Call:
lm(formula = mpg ~ displacement + c4 + c5 + c6 + c8, data = Auto)

Residuals:
    Min      1Q  Median      3Q     Max 
-10.692  -2.694  -0.347   2.157  20.307 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  24.33811    2.25278   10.80  < 2e-16 ***
displacement -0.05225    0.00693   -7.54  3.3e-13 ***
c4           10.67609    2.23296    4.78  2.5e-06 ***
c5           10.60478    3.39198    3.13   0.0019 ** 
c6            7.04473    2.46493    2.86   0.0045 ** 
c8            8.65170    2.92786    2.95   0.0033 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 4.39 on 386 degrees of freedom
Multiple R-squared:  0.687, Adjusted R-squared:  0.683 
F-statistic:  170 on 5 and 386 DF,  p-value: <2e-16

两者都产生相同的输出,这在我看来并不奇怪。令我惊讶的是,我被教导要使用虚拟代码而不是转换为因子。虚拟代码是否有任何分析、计算或任何理由来使用因子变量?使用因子似乎要容易得多,需要的代码更少,而且您最终不会得到一堆额外的变量。我可以看到与使用因子相比,虚拟编码的唯一可能优势是您可以选择参考组,我猜您可能也可以使用因子。

【问题讨论】:

  • 您可以使用relevel 设置治疗对比的参考水平,您可以使用model.matrix 来检查所使用的确切编码。

标签: r


【解决方案1】:

使用dummies 包可以轻松完成虚拟编码。

library(dummies)

#sample data
auto <- tail(ISLR::Auto,10)

#dummy coding
auto_dummyCoded <- cbind(auto, dummy(c("cylinders"), data=auto))
auto_dummyCoded

在上述虚拟编码中,由于样本数据中有两个圆柱体类别,因此添加了两个新变量(即cylinders4cylinders6)。


现在,让我们将cylinders 列转换为“因子”,然后将其传递给lm,而不是虚拟编码

auto$cylinders <- as.factor(auto$cylinders)
fit <- lm(mpg ~ cylinders, data=auto, x=T)

让我们打印fit$x,看看cylinders 列是如何在内部编码的。 R 已将圆柱列转换为 cylinders6 和一个常量列 intercept(这比“圆柱”列中可用的类别数量少一个以及一个额外的常量变量。只是虚拟编码的另一种方法!)

    (Intercept) cylinders6
388           1          0
389           1          1
390           1          0
391           1          0
392           1          0
393           1          0
394           1          0
395           1          0
396           1          0
397           1          0

【讨论】:

  • 啊,太好了!我没有意识到lm 内部有自己的虚拟编码。那讲得通。我想我仍然感到惊讶的是,以前没有老师提到过这一点并依赖于虚拟编码新变量。谢谢你的解释。
猜你喜欢
  • 2021-07-29
  • 1970-01-01
  • 2018-03-29
  • 2014-01-27
  • 2021-01-01
  • 1970-01-01
  • 2018-11-16
  • 2018-10-13
  • 1970-01-01
相关资源
最近更新 更多