r - ggplot2 中 glm 和 stat_smooth 的逻辑回归预测值不同

标签 r ggplot2

我正在尝试在 ggplot2 中制作此逻辑回归图.

df <- structure(list(y = c(2L, 7L, 776L, 19L, 12L, 26L, 7L, 12L, 8L,
24L, 20L, 16L, 12L, 10L, 23L, 20L, 16L, 12L, 18L, 22L, 23L, 22L,
13L, 7L, 20L, 12L, 13L, 11L, 11L, 14L, 10L, 8L, 10L, 11L, 5L,
5L, 1L, 2L, 1L, 1L, 0L, 0L, 0L), n = c(3L, 7L, 789L, 20L, 14L,
27L, 7L, 13L, 9L, 29L, 22L, 17L, 14L, 11L, 30L, 21L, 19L, 14L,
22L, 29L, 28L, 28L, 19L, 10L, 27L, 22L, 18L, 18L, 14L, 23L, 18L,
12L, 19L, 15L, 13L, 9L, 7L, 3L, 1L, 1L, 1L, 1L, 1L), x = c(18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 59L,
62L, 63L, 66L)), .Names = c("y", "n", "x"), class = "data.frame", row.names = c(NA,
-43L))


mod.fit <- glm(formula = y/n ~ x, data = df, weight=n, family = binomial(link = logit),
        na.action = na.exclude, control = list(epsilon = 0.0001, maxit = 50, trace = T))
summary(mod.fit)

Pi <- c(0.25, 0.5, 0.75)
LD <- (log(Pi /(1-Pi))-mod.fit$coefficients[1])/mod.fit$coefficients[2]
LD.summary <- data.frame(Pi , LD)
LD.summary


plot(df$x, df$y/df$n, xlab = "x", ylab = "Estimated probability")

lin.pred <- predict(mod.fit)
pi.hat <- exp(lin.pred)/(1 + exp(lin.pred))
lines(df$x, pi.hat, lty = 1, col = "red")


segments(x0 = LD.summary$LD, y0 = -0.1, x1 = LD.summary$LD, y1 = LD.summary$Pi,
         lty=2, col=c("darkblue","darkred","darkgreen"))
segments(x0 = 15, y0 = LD.summary$Pi, x1 = LD.summary$LD, y1 = LD.summary$Pi,
         lty=2, col=c("darkblue","darkred","darkgreen"))
legend("bottomleft", legend=c("LD25", "LD50", "LD75"), lty=2, col=c("darkblue","darkred","darkgreen"), bty="n", cex=0.75)
enter image description here
这是我对 ggplot2 的尝试
library(ggplot2)

p <- ggplot(data = df, aes(x = x, y = y/n)) +
            geom_point() +
            stat_smooth(method = "glm", family = "binomial")

p <- p + geom_segment(aes(
                            x = LD.summary$LD
                          , y = 0
                          , xend = LD.summary$LD
                          , yend = LD.summary$Pi
                         )
                         , colour="red"
                       )

p <- p + geom_segment(aes(
                            x = 0
                          , y = LD.summary$Pi
                          , xend = LD.summary$LD
                          , yend = LD.summary$Pi
                         )
                         , colour="red"
                       )

print(p)
enter image description here
问题
  • glm 的预测值和 stat_smooth看起来不同。这两种方法会产生不同的结果还是我在这里遗漏了一些东西。
  • 我的 ggplot2 图与基础 R 图并不完全相同。
  • 如何在ggplot2中为线段使用不同的颜色?
  • 以及如何在 ggplot2 中放置图例?

  • 提前感谢您的帮助和时间。谢谢

    最佳答案

    只是对@mathetmatical.coffee 的回答做了一些小补充。通常,geom_smooth不应该取代实际的建模,这就是为什么有时当您想要使用从 glm 获得的特定输出时它看起来不方便的原因。诸如此类。但实际上,我们需要做的就是将拟合值添加到我们的数据框中:

    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

    最后一个小技巧是使用 Inf-Inf使虚线一直延伸到情节边界。

    这里的教训是,如果您只想为绘图添加平滑,而绘图中的任何其他内容都不依赖于它,请使用 geom_smooth .如果要引用拟合模型的输出,通常将模型拟合在 ggplot 之外更容易然后绘图。

    关于r - ggplot2 中 glm 和 stat_smooth 的逻辑回归预测值不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8845279/

    相关文章:

    r - 报告 R 包中的 tex2docx 函数出错

    python - 在用于 Python 的 ggplot 中,将离散 X 标度与 geom_point() 一起使用?

    r - 找不到函数 ggplot2

    r - 使用 DT 包的数据表中的排序因素

    r - 可以强制不出现的元素显示在 ggplot 图例中吗?

    r - 如何更改特定线条的粗细和/或在多线图中添加形状?

    r - 示例需要 : Using arrow() with ggplot2

    r - R 中具有多个解释变量的细菌生长曲线(逻辑/S 形)

    r - ggplot2 Tufte 线与轴刻度相同

    r - 将 'observeEvent' 输出传递给操作按钮