我想进行逻辑回归但却出错 - 不知道错误可能在哪里。
我的数据结构:
'YYYY-MM-DD,HH',value
regression命令返回以下错误:
'data.frame': 3911 obs. of 29 variables:
$ vn1 : Factor w/ 2 levels "maennlich","weiblich": 1 1 2 1 1 2 1 1 1 1 ...
$ vn2c : int 1976 1943 1927 1949 1965 1977 1986 1976 1944 1994 ...
$ vn35 : Factor w/ 7 levels "keine Angabe",..: 6 4 5 3 3 5 7 6 5 5 ...
$ v39 : Factor w/ 8 levels "keine Angabe",..: 8 4 5 8 7 7 5 6 6 6 ...
$ n39 : Factor w/ 9 levels "keine Angabe",..: 4 4 4 4 4 4 4 4 4 4 ...
$ v41 : Factor w/ 7 levels "keine Angabe",..: 6 5 5 2 7 7 5 5 6 6 ...
$ n41 : Factor w/ 7 levels "keine Angabe",..: 4 4 4 4 4 4 4 4 4 4 ...
$ vn42a : Factor w/ 8 levels "keine Angabe",..: 8 4 8 8 5 5 6 6 6 4 ...
$ vn42b : Factor w/ 8 levels "keine Angabe",..: 5 4 7 5 5 5 6 7 6 5 ...
$ vn43a : Factor w/ 8 levels "keine Angabe",..: 7 5 8 6 2 6 6 2 7 7 ...
$ vn43b : Factor w/ 8 levels "keine Angabe",..: 7 4 6 4 4 7 6 2 6 5 ...
$ vn62 : Factor w/ 14 levels "keine Angabe",..: 8 11 9 2 3 3 8 6 5 7 ...
$ vn119a : Factor w/ 15 levels "keine Angabe",..: 6 3 8 14 10 8 14 8 6 6 ...
$ ostwest : Factor w/ 2 levels "Ost","West": 2 2 2 2 2 2 2 2 2 2 ...
$ prefmerkel : Factor w/ 2 levels "Steinbrueck",..: 1 2 2 NA NA NA 2 2 1 1 ...
$ angst : num 1 3 2 4 4 2 0 1 2 2 ...
$ crisismerkel : num 0 4 3 0 1 1 3 2 2 2 ...
$ leadership42 : Factor w/ 5 levels "trifft ueberhaupt nicht zu",..: 5 1 5 5 2 2 3 3 3 1 ...
$ leadership43 : Factor w/ 5 levels "trifft ueberhaupt nicht zu",..: 4 2 5 3 NA 3 3 NA 4 4 ...
$ leadership : num 1 -1 0 2 NA -1 0 NA -1 -3 ...
$ trustworthiness42: Factor w/ 5 levels "trifft ueberhaupt nicht zu",..: 2 1 4 2 2 2 3 4 3 2 ...
$ trustworthiness43: Factor w/ 5 levels "trifft ueberhaupt nicht zu",..: 4 1 3 1 1 4 3 NA 3 2 ...
$ trustworthiness : num -2 0 1 1 1 -2 0 NA 0 0 ...
$ ideology : num 5 8 6 NA NA NA 5 3 2 4 ...
$ pid : Factor w/ 10 levels "none","CDU/CSU",..: 3 2 5 1 7 5 1 5 3 3 ...
$ age : num 37 70 86 64 48 36 27 37 69 19 ...
$ agegroups : Factor w/ 7 levels "bis 25 Jahre",..: 3 6 7 5 4 3 2 3 6 1 ...
$ gender : Factor w/ 2 levels "male","female": 1 1 2 1 1 2 1 1 1 1 ...
$ region : Factor w/ 2 levels "west","east": 1 1 1 1 1 1 1 1 1 1 ...
答案 0 :(得分:10)
您不能拥有因子/分类响应变量。
插图:
> d=data.frame(f=factor(c(1,2,1,2,1,2)),x=runif(6))
> glm(f~x,data=d)
Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 0.351715633412823, 0.449422287056223, :
NA/NaN/Inf in 'y'
In addition: Warning messages:
1: In Ops.factor(y, mu) : - not meaningful for factors
2: In Ops.factor(eta, offset) : - not meaningful for factors
3: In Ops.factor(y, mu) : - not meaningful for factors
如果您真的想要进行逻辑回归,则应将其更改为0和1,或者为FALSE和TRUE,并使用family=binomial
:
# recode d$f==2 as TRUE, else FALSE
d$f=d$f=="2"
# fit
glm(f~x,data=d,family=binomial)
Call: glm(formula = f ~ x, family = binomial, data = d)
Coefficients:
(Intercept) x
-0.9066 1.8922
Degrees of Freedom: 5 Total (i.e. Null); 4 Residual
Null Deviance: 8.318
Residual Deviance: 8.092 AIC: 12.09
答案 1 :(得分:7)
如果您的变量是二项式提及' family = binomial'。这将解决问题
答案 2 :(得分:2)
此页面在搜索此错误时显示出来很高,因此想添加另一个与线性回归和逻辑回归无关的原因。我遇到了与Yulia相同的问题,在该问题中,我对某些预测变量进行了 log转换,导致此处讨论的错误。
发生错误的原因是,如果在对数转换之前有任何行的值为0,则这些行变为-Inf
,这将导致回归引发错误。解决方案是避免对此类变量进行对数转换,或确保您没有0值的行(例如,见discussion on stack exchange)。
答案 3 :(得分:0)
在我的情况下,它不属于上述情况。我对右倾变量进行了对数转换,当我使用它时,log-regression导致了这个错误。当我使用原始(未转换的)变体时 - 它完美无缺。