1
$\begingroup$

I am confused with the answer from https://stackoverflow.com/questions/23282048/logistic-regression-defining-reference-level-in-r

It said if you want to predict the probability of "Yes", you set as relevel(auth$class, ref = "YES"). However, in my experiment, if we have a binary response variable with "0" and "1". We only get the estimation for probability of "1" when we set relevel(factor(y),ref="0").

n <- 200
x <- rnorm(n)
sumx <- 5 + 3*x
exp1 <- exp(sumx)/(1+exp(sumx))
y <- rbinom(n,1,exp1) #probability here is for 1
model1 <- glm(y~x,family = "binomial")
summary(model1)$coefficients
            Estimate Std. Error  z value     Pr(>|z|)
(Intercept) 5.324099  1.0610921 5.017565 5.233039e-07
x           2.767035  0.7206103 3.839849 1.231100e-04
model2 <- glm(relevel(factor(y),ref="0")~x,family = "binomial")

summary(model2)$coefficients
            Estimate Std. Error  z value     Pr(>|z|)
(Intercept) 5.324099  1.0610921 5.017565 5.233039e-07
x           2.767035  0.7206103 3.839849 1.231100e-04

I think if we want to get probability of "Yes", we should set relevel(auth$class, ref = "No"), am I correct? And what is reference level here means? Actually, what is glm() to predict in default if we use response other than "0" and "1"?

$\endgroup$

1 Answer 1

4
$\begingroup$

The reference level is the base-line. If you wanted to predict probability of 'Yes', you'd set the base-line (i.e. reference level) "No". So you are correct, I think the answer in the other thread is incorrect.

I prefer to set up the levels of variables explicitly using the factor function. i.e.

    y = factor(y, levels=c(0,1), labels=c("No","Yes") 
    # Reference level is listed first
    # I prefer to have character labels rather than 0,1.

Sorry, but I am unsure about what you're asking about for default behavior for the glm function when you've specified family="binomial" -- the function will expect a two-level factor as the response variable.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.