logistic regression - Error in glm() in R -
i perform logistic regression errors - don't know mistake might be.
the structure of data:
'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 ...
the regression command returns following error:
summary(glm(prefmerkel~angst+crisismerkel+leadership+trustworthiness+ideology+pid+agegroups+gender+region,data=gles)) error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, : na/nan/inf in 'y' in addition: warning messages: 1: in ops.factor(y, mu) : ‘-’ nicht sinnvoll für faktoren 2: in ops.factor(eta, offset) : ‘-’ nicht sinnvoll für faktoren 3: in ops.factor(y, mu) : ‘-’ nicht sinnvoll für faktoren
you can't have factor/categorical response variables.
illustration:
> 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 factors 2: in ops.factor(eta, offset) : - not meaningful factors 3: in ops.factor(y, mu) : - not meaningful factors
if want logistic regression should change them 0 , 1, or false , true, , use family=binomial
:
# recode d$f==2 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
Comments
Post a Comment