【发布时间】: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。
对不起,如果这是一个基本错误,但我无法弄清楚。如果您需要更多信息,请说出来。提前致谢。
【问题讨论】:
-
你只有两个变量。它们是
benefit和history,每个变量都具有三个级别(短、长、控制)和(高、低、控制)。对变量进行编码,以便在每种情况下,control都是您通过对比进行比较的基本级别。
标签: r linear-regression dummy-variable