【问题标题】:How to avoid the dummy variable trap when dummy coding several treatments plus control group for linear regression in R在 R 中对多个处理加对照组进行线性回归的虚拟编码时如何避免虚拟变量陷阱
【发布时间】:2020-11-30 03:16:00
【问题描述】:

我知道我需要的虚拟变量比虚拟变量的总数少一个。但是,我被卡住了,因为在 R 中运行 lm 时,我一直收到错误:“1 由于奇异性而未定义”。我在这里发现了一个类似的问题:What is causing this error? Coefficients not defined because of singularities,但它与我的问题略有不同。

我有两个处理(1)“收益”和(2)“历史”,每个有两个级别(1)“低”和“高”以及(2)“短”和“长”,即 4 种组合.此外,我有一个对照组,这两个组都没有接触过。因此,我编码了 4 个虚拟变量(比组 n=5 的总数少一个)。接下来,虚拟编码数据如下所示:

                               low benefit  high benefit  short history  long history
Control group                           0             o              0             0
low benefit, short history              1             0              1             0
low benefit, long history               1             0              0             1
high benefit, short history             0             1              1             0
high benefit, long history              0             1              0             1

当我运行我的 lm 时,我得到了这个:

Model: 
summary(lm(X ~ short history + high benefit + long history + low benefit + Control variables, data = df))

Coefficients: (1 not defined because of singularities)
                                         Estimate   Std. Error  t value  Pr(>|t|)
(Intercept)                           5.505398100  0.963932438  5.71139  4.8663e-08 ***
Dummy short history                   0.939025772  0.379091565  2.47704   0.0142196 *
Dummy high benefit                   -0.759944023  0.288192645 -2.63693   0.0091367 **
Dummy long history                    0.759352915  0.389085599  1.95163   0.0526152 .
Dummy low benefit                              NA           NA       NA          NA
Control Varibales                          xxx          xxx        xxx       xxx

第 4 位的虚拟变量总是会出现此错误。控制变量的计算都没有问题。

我已经尝试只包含两个具有两个级别的变量,这意味着我编码的“历史”,1 代表“长”,0 代表“短”,以及“收益”,1 代表“高”,0 代表“低的”。这样,lm 起作用了,但问题是,控制组和组合“历史短,收益低”的编码相同,即两个变量都为 0 和 0。

对不起,如果这是一个基本错误,但我无法弄清楚。如果您需要更多信息,请说出来。提前致谢。

【问题讨论】:

  • 你只有两个变量。它们是benefithistory,每个变量都具有三个级别(短、长、控制)和(高、低、控制)。对变量进行编码,以便在每种情况下,control 都是您通过对比进行比较的基本级别。

标签: r linear-regression dummy-variable


【解决方案1】:

正如我在 cmets 中输入的那样,您只有两个变量,如果您将它们作为因素并检查对比 r 将做正确的事情。另请参阅http://www.sthda.com/english/articles/40-regression-analysis/163-regression-with-categorical-variables-dummy-coding-essentials-in-r/

编造代表你的数据。

set.seed(2020)
df <- data.frame(
  X = runif(n = 120, min = 5, max = 15),
  benefit = rep(c("control", "low", "high"), 40),
  history = c(rep("control", 40), rep("long", 40), rep("short", 40))
)

制作benefithistory 因子,检查控制是否是每个因子的基本对比。

df$benefit <- factor(df$benefit)
df$history <- factor(df$history)
contrasts(df$benefit)
#>         high low
#> control    0   0
#> high       1   0
#> low        0   1
contrasts(df$history)
#>         long short
#> control    0     0
#> long       1     0
#> short      0     1

运行回归并获取摘要。 4个系数都与控制/控制相比。

lm(X ~ benefit + history, df)
#> 
#> Call:
#> lm(formula = X ~ benefit + history, data = df)
#> 
#> Coefficients:
#>  (Intercept)   benefithigh    benefitlow   historylong  historyshort  
#>      9.94474      -0.08721       0.11245       0.37021      -0.35675
summary(lm(X ~ benefit + history, df))
#> 
#> Call:
#> lm(formula = X ~ benefit + history, data = df)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -5.4059 -2.3706 -0.0007  2.4986  4.7669 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)   9.94474    0.56786  17.513   <2e-16 ***
#> benefithigh  -0.08721    0.62842  -0.139    0.890    
#> benefitlow    0.11245    0.62842   0.179    0.858    
#> historylong   0.37021    0.62842   0.589    0.557    
#> historyshort -0.35675    0.62842  -0.568    0.571    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 2.809 on 115 degrees of freedom
#> Multiple R-squared:  0.01253,    Adjusted R-squared:  -0.02182 
#> F-statistic: 0.3648 on 4 and 115 DF,  p-value: 0.8333

【讨论】:

  • 非常感谢您的帮助!我完全按照您的建议进行操作,并且在运行 contrasts 时收到与您相同的输出。但是,当使用两个变量(á 3 个级别)运行回归时,对于相同的位置,我仍然会收到相同的错误。我想可能是因为控制变量,但即使我省略它们,错误仍然存​​在。是否有可能发生错误,因为我每个级别“只有”31-38 个并且控制组被拆分(而您每个级别有 40 个)?
  • 对不起,我必须纠正自己。我的数据如下:&gt; table(df$history) # Ctr Long Short 35 76 70 &gt; table(df$Benefi) # Ctr High low 35 70 76
  • 能否附上您现在收到的错误消息?您能否也请做一个 dput(head(df, 20)). historybenefit 是否有可能混为一谈,也就是说人们倾向于按因素水平聚集?如果它们高度相关,则消息是准确的。当您像summary(lm(X ~ benefit, df))summary(lm(X ~ history, df)) 一样单独运行每个lm 时会发生什么。您本身没有完美平衡的设计这一事实并不重要。
  • 消息如下:“系数:(由于奇异性而未定义 1)”,其后跟 NA,如上所述。当我单独运行 lm 时,它工作正常。显然,治疗中的变量是高度相关的,因为例如当“short”被编码为 1,那么“long”必须为 0。奇怪的是,相关性测试显示“short”和“low”以及“long”和“high”在统计上显着相关(r = .22 , p
  • &gt; dput(head(df, 25)) structure(list(X = c(3, 7, 1, 2.5, 5.5, 4, 2, 5, 5, 5, 1, 1.5, 5.5, 6.5, 4, 6.5, 1, 3, 2, 4, 3.5, 1, 4, 2, 5), Benefit = structure(c(2L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 2L, 3L, 1L, 3L), .Label = c("Ctr", "High", "low"), class = "factor"), CSP = structure(c(3L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 2L), .Label = c("Ctr", "Long", "Short"), class = "factor")), row.names = c(NA, 25L), class = "data.frame")
猜你喜欢
  • 2018-08-19
  • 2019-07-14
  • 2019-02-28
  • 2018-04-17
  • 2021-08-20
  • 2018-07-19
  • 2019-01-25
  • 1970-01-01
  • 2015-06-24
相关资源
最近更新 更多