0
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
yeeen
  • 4,911
  • 11
  • 52
  • 73
  • 1
    Per the `geom_contour`/`stat_contour` docs: "To be a valid surface, the data must contain only a single row for each unique combination of the variables mapped to the x and y aesthetics. Contouring tends to work best when x and y form a (roughly) evenly spaced grid. If your data is not evenly spaced, you may want to interpolate to a grid before visualising." – camille Sep 12 '18 at 14:21
  • @camille How to interpolate to a grid?? – yeeen Sep 12 '18 at 14:55
  • 1
    What is `default_plot`? – Anonymous coward Sep 12 '18 at 15:23
  • Not yet going to vote to close as a dupe, but see [here](https://stackoverflow.com/q/19339296/5325862) for one question on interpolation. I don't think this is a regression question as you've tagged it, just a question on interpolating to get data in the shape needed for contouring – camille Sep 12 '18 at 15:51
  • I'm also unclear on how you'd be making the sort of contour plot that `geom_contour` creates with a discrete value (`default`) – camille Sep 12 '18 at 15:54
  • Sorry forgot to include the codes for default_plot – yeeen Sep 12 '18 at 16:46
  • @camille I added in the below codes and got error "Error in interp.old(x, y, z, xo, yo, ncp = 0, extrap = FALSE, duplicate = duplicate, : duplicate data points: need to set 'duplicate = ..' " ////////////// If i add in the parameter "duplicate="strip"/"mean", i get all NAs //////////// library(akima) Default$default_num <- ifelse(Default$default == "Yes", 1, 0) default_interp <- with(Default, interp(x = balance, y = default_num, z = glm_prob)) – yeeen Sep 12 '18 at 16:59
  • Is alright. I was told that this is not possible to use contour cos it is a 2D not 3D plot. I have gotten what i want: Default$balance[which.min(abs(glm1$fitted.values - 0.7))] #decision boundary – yeeen Sep 16 '18 at 07:20

1 Answers1

-1

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

yeeen
  • 4,911
  • 11
  • 52
  • 73