回归给出输入变量之一的错误 "contrasts can be applied only to factors with 2 or more levels"

Regression gives error on one of the input variables "contrasts can be applied only to factors with 2 or more levels"

我运行在 R 中使用大量输入变量进行逻辑回归。

newlogit <- glm(install. ~ SIZES + GROSSCONSUMPTION.... + 
                NETTCONSUMPTION..... + NETTGENERATION....... + 
                GROSSGENERATION.... + Variable. + Fixed + 
                Cost.of.gross.cons + Cost.of.net.cons + Cons.savings + 
                generation.gains + Total.savings + Cost.of.system + 
                Payback + Self.consumption + Total.consumption.as.solar + 
                Owner.occupied + postcode + Suburb + Market.penetration + 
                X..green.vote, data = newdata, family = "binomial")

我收到这个错误:

Error in contrasts<-(tmp, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

能否请您告知为什么会出现此错误以及是否有办法 运行 此回归?

当模型右侧的变量之一是具有单一水平的因子时,我已经看到这个问题发生了。

如果是这种情况,您需要做的就是从模型中删除该因素。

它反对你的变量之一(如 gvrocha 所说);您可能只有一个级别或一个字符串的因素。

快速追踪有问题的变量的一个技巧是进行区间二等分和 increase/decreasecol 索引,直到触发错误。

最好使用数值(列索引)接口来 glm(glm(data[,'install.'] ~ data[,2:40]),参见 [1])

而不是公式界面glm(install. ~ var1 + var2 + ...:

[1]