I'm using logistic regression to predict a binary outcome variable (Group, 0/1). So I've noticed something: I have two variable representing the same outcome, one is coded simply as "0" or "1".
> df$Group
>[1] 0 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1
> 0 0 0 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1
> [59] 1 1 1 1 1 1 0 1 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 0 1 0 1 0 1 1 1 1
> 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 0
>[117] 0 0 0 1 1 1 1
> 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 0
> 0 1 1 1 1 0 1 1 0 1 1 1 1 0 0 1
>[175] 1 0 1
>Levels: 0 1
> is.factor(df$Group)
> [1] TRUE
Same story for the other one which represents the same thing, but has "names" labels:
> df$Group
>[1] CON CI CON CI CI CON CI
> CI CON CI CI CI CON CI
>[15] CI ecc.. ecc..
> Levels: CI CON
> is.factor(df$Group2)
> [1] TRUE
> contrasts(df$Group2)
> CI 0
> CON 1
In which 0 in the first variable =CON, whereas 1=CI. I created that first numerical variable because I wanted CI to be my "1" group, and CON the 0 reference group, but when I was transforming from the dataset, each time I tried to do "as.factor" what happened was CI=level 1, CON = level 2.
I thought they were the same thing, but when I tried to plot the odds ratio using sjPlot package, and just checked to be sure, I noticed that the OR were quite different, although by inspecting the coefficients of summary(glm model), everything seemed the same(apart from -or + of estimates, which makes sense as the two groups are coded differently). Specifically, when using the numerical variable the plotted OR are definitely bigger, whereas when using the "name" variable, the OR are smaller.
Am I missing something in the understanding of r (I'm self-thought) or in computation of logistic regression? Which one of the variables should I use in logistic regression? And how could I change the fact that in the "name" variables r uses "CI" as 0 reference group instead of CON? Thank you.
glm
's and the different outputs. e.g. have you specified the familiy? check?family
– Roman