Search code examples
rbinarylogistic-regressionnumeric

How to use a binary variable to build a logistic regression model?


As you can see, this is the structure of my dependent variable (G3):

structure information of G3

G3 is student's final period grade. It is a binary variable, if G3<10, students fail; if G3>=10, students pass. It is represented by "1" means fail, "2" means pass.

Now I'm going to build a logistic regression model. I need to convert this binary variable into a numeric variable, and we assumed that if the dependent variable G3 is equal to 1 if students failed, if G3 is equal to 0 if students passed. What should I do?

And I checked the structure of G3 again:

try to convert G3 into numeric variable first

It turned into numeric variable, but "fail" or "pass" still represent by "1" and "2". How can i change them to "1" and "0"?


Solution

  • How about

    performance$G3 <- 2-performance$G3 
    

    ?

    Alternatively, you could have started at the beginning with

    performance$G3 <- ifelse(performance$G3=="fail",0,1)
    

    Finally, you can use a factor variable as a response. From ?binomial, if the response variable is a factor,

    ... ‘success’ is interpreted as the factor not having the first level (and hence usually of having the second level).

    You'd have to change the order of the levels, e.g.

     performance$G3 <- factor(performance$G3, levels=c("pass", "fail"))