我在 R 中运行序数逻辑回归,当我包含虚拟变量时遇到了麻烦。我的模型适用于我的第一组预测变量。接下来,我想为数据集中表示的每一年添加虚拟变量。
car:recode我以这种方式创建了虚拟变量(11 年中的每一年都有这样的声明)
fsd$admityear2000 <- recode(fsd$ApplicationYear ,"2000=1;else=0")
lrm模型指定如下
library(Design)
ddist<- datadist(fsd)
options(datadist='ddist')
m4 <- lrm(Outcome ~ relGPA + mcAvgGPA + Interview_Z + WorkHistory_years + GMAT + UGI_Gourman + admityear1999 + admityear2000 + admityear2001 + admityear2002 + admityear2003 + admityear2004 + admityear2005 + admityear2006 + admityear2007 + admityear2008 + admityear2009, data=fsd)
(对不起所有其他随机变量,但我不想通过更改我的代码来引入混淆)
我得到错误
singular information matrix in lrm.fit (rank= 22 ). Offending variable(s):
admityear2009 admityear2000 admityear1999
Error in lrm(Outcome ~ relGPA + mcAvgGPA + Interview_Z + WorkHistory_years + :
Unable to fit model using “lrm.fit”
我知道包含虚拟变量的所有选项会过度定义模型,但无论我包含所有 11 年还是仅包含 10 年,我都会收到错误。
我在这里找到了将惩罚参数设置lrm为一个小的正值的建议。将其设置为 1 或 5 会更改错误,使其仅将其中一个变量命名为违规。即使使用 . 错误也不会消失penalty=100。
我对 R 很陌生,但到目前为止我很喜欢这种自由。谢谢你的帮助!
回应和教训
- 因素很棒,我不敢相信我之前没有注意到它们。经常清理我的代码的人。谢谢!
- 我的 DV,“结果”确实是序数,在将其设为因子()之后,我也将其设为有序()。
- str() 命令也很棒,这就是我的数据现在的样子(省略了一些不相关的变量)
输出:
str(fsd)
Outcome : Ord.factor w/ 3 levels "0"<"1"<"2"
relGPA : num
mcAvgGPA : num
admitschool : Factor w/ 4 levels "1","2","3","4"
appyear : Factor w/ 11 levels "1999","2000",..
- lrm() 和 polr() 现在都成功运行,并且它们都通过删除因子的一些值来处理 appyear。lrm() 删除 1999、2000 和 2001 年,而 polr() 仅删除 1999 年和 2000 年。 lrm() 没有给出警告,而 polr() 说“设计似乎排名不足,因此删除了一些系数。” 这是一种改进,但我仍然不明白为什么需要删除多个值。xtabs 显示没有完全分离对吗?
输出:
xtabs(~fsd$appyear + fsd$Outcome)
fsd$Outcome
fsd$appyear 0 1 2
1999 1207 123 418
2000 1833 246 510
2001 1805 294 553
2002 1167 177 598
2003 4070 158 1076
2004 2803 106 1138
2005 3749 513 2141
2006 4429 519 2028
2007 6134 670 1947
2008 7446 662 1994
2009 4411 86 1118