Search code examples
rggplot2regressionlogistic-regression

Contour requires single `z` at each combination of `x` and `y`


Warning message:
Computation failed in `stat_contour()`:
Contour requires single `z` at each combination of `x` and `y`. 

Why am I having the error in the below codes? I just want to plot a boundary using the logit model..

library("ISLR")
glm1 <- glm(default ~ balance, data=Default, family=binomial())
summary(glm1)

    Default$glm_prob <- predict(glm1, newdata=data.frame(balance=Default$balance), type="response")
    default_plot <- ggplot() + geom_point(data=Default, aes(x=balance, y=default, color=default), size=4) + 
      scale_color_manual(values=c("green", "red")) + theme_bw()
default_plot
glm1.contour <- default_plot + stat_contour(
      data=Default, aes(x=balance, y=default, z=glm_prob), breaks=c(0.7))
    glm1.contour

Solution

  • Default$balance[which.min(abs(glm1$fitted.values - 0.7))] #decision boundary