Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I made a logistic regression model using glm in R. I have two independent variables. How can I plot the decision boundary of my model in the scatter plot of the two variables. For example, how can I plot a figure like: http://onlinecourses.science.psu.edu/stat557/node/55

Thanks.

share|improve this question

1 Answer

up vote 8 down vote accepted
set.seed(1234)

x1 <- rnorm(20, 1, 2)
x2 <- rnorm(20)

y <- sign(-1 - 2 * x1 + 4 * x2 )

y[ y == -1] <- 0

df <- cbind.data.frame( y, x1, x2)

mdl <- glm( y ~ . , data = df , family=binomial)

slope <- coef(mdl)[2]/(-coef(mdl)[3])
intercept <- coef(mdl)[1]/(-coef(mdl)[3]) 

library(lattice)
xyplot( x2 ~ x1 , data = df, groups = y,
   panel=function(...){
       panel.xyplot(...)
       panel.abline(intercept , slope)
       panel.grid(...)
       })

alt text

I must remark that perfect separation occurs here, therefore the glm function gives you a warning. But that is not important here as the purpose is to illustrate how to draw the linear boundary and the observations colored according to their covariates.

share|improve this answer
I hope I am not old fashioned if I use lattice :-) – suncoolsu Jan 13 '11 at 2:47
2  
I also hope that if this is a HW problem, you will not simply copy paste. – suncoolsu Jan 13 '11 at 2:54
Thanks. This is not a HW question and the answer is helpful for me to understand my model. – user2755 Jan 13 '11 at 4:25
oh yes you are :) – mpiktas Jan 13 '11 at 8:09
Can someone explain me the logic behind the slope and intercept? (regarding the logistic model) – Fernando Jan 9 at 12:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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