Predicted values for logistic regression from glm and stat_smooth in ggplot2 are different

后端 未结 2 1929
抹茶落季
抹茶落季 2020-12-05 05:48

I\'m trying to make this logistic regression graph in ggplot2.

df <- structure(list(y = c(2L, 7L, 776L, 19L, 12L, 26L, 7L, 12L, 8L,
24L, 20L,          


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 06:32

    Just a couple of minor additions to @mathetmatical.coffee's answer. Typically, geom_smooth isn't supposed to replace actual modeling, which is why it can seem inconvenient at times when you want to use specific output you'd get from glm and such. But really, all we need to do is add the fitted values to our data frame:

    df$pred <- pi.hat
    LD.summary$group <- c('LD25','LD50','LD75')
    
    ggplot(df,aes(x = x, y = y/n)) + 
        geom_point() + 
        geom_line(aes(y = pred),colour = "black") + 
        geom_segment(data=LD.summary, aes(y = Pi,
                                          xend = LD,
                                          yend = Pi,
                                          col = group),x = -Inf,linetype = "dashed") + 
        geom_segment(data=LD.summary,aes(x = LD,
                                         xend = LD,
                                         yend = Pi,
                                         col = group),y = -Inf,linetype = "dashed")
    

    enter image description here

    The final little trick is the use of Inf and -Inf to get the dashed lines to extend all the way to the plot boundaries.

    The lesson here is that if all you want to do is add a smooth to a plot, and nothing else in the plot depends on it, use geom_smooth. If you want to refer to the output from the fitted model, its generally easier to fit the model outside ggplot and then plot.

提交回复
热议问题