Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2中glm和stat_smooth的逻辑回归预测值不同_R_Ggplot2 - Fatal编程技术网

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

R ggplot2中glm和stat_smooth的逻辑回归预测值不同,r,ggplot2,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, 0

我正试图在
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)

问题
  • glm
    stat\u smooth
    的预测值看起来不同。是这两种方法产生不同的结果,还是我遗漏了一些东西
  • 我的ggplot2图形与基本R图形不完全相同
  • 如何为ggplot2中的线段使用不同的颜色
  • 如何在ggplot2中添加图例

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

    修改您的
    LD.summary
    以包含带有
    (或适当标签)的新列

    此外,为了避免必须始终执行
    LD.summary$xxx
    ,请将
    data=LD.summary
    输入到您的
    geom\u段中:

    geom_segment(data=LD.summary, aes(x=0, y=Pi,xend=LD, yend=Pi, colour=group) )
    
    至于为什么图形不完全相同,在基本R图形中,x轴从~20开始,而在
    ggplot
    中,它从零开始。这是因为第二个
    geom_段开始于
    x=0
    。 要修复此问题,您可以将
    x=0
    更改为
    x=min(df$x)

    要获得y轴标签,请使用
    +缩放y连续(“估计概率”)

    总之:

    LD.summary$group <- c('LD25','LD50','LD75')
    p <- ggplot(data = df, aes(x = x, y = y/n)) +
                geom_point() +
                stat_smooth(method = "glm", family = "binomial") +
                scale_y_continuous('Estimated probability')    # <-- add y label
    p <- p + geom_segment(data=LD.summary, aes( # <-- data=Ld.summary
                                x = LD
                              , y = 0
                              , xend = LD
                              , yend = Pi
                              , col = group     # <- colours
                             )
                           )    
    p <- p + geom_segment(data=LD.summary, aes( # <-- data=Ld.summary
                                x = min(df$x)   # <-- don't plot all the way to x=0
                              , y = Pi
                              , xend = LD
                              , yend = Pi
                              , col = group     # <- colours
                             )
                           )
    print(p)
    

    LD.summary$group只是@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")
    

    df$pred您的基本R图片中没有图例(虽然命令很好)-我会更新它以避免混淆。@mathematic.coffee:谢谢您的评论。请看左下角的图例。是的,那是因为我更新了图片以包含图例。oops,谢谢@mathematic.coffee为什么在作业
    Pi@mathematic.coffee中将变量称为“Pi”:谢谢你的热情回答。一个观察:为什么LD25、LD50没有像在基本R图中那样接触预测线?任何想法。Thanks@MYaseen208这与
    stat\u smooth
    有关,它不会生成与你的
    pi.hat
    公式相同的数字:试着绘制第一个
    p
    ,然后做
    行(x,pi.hat,lty=1,col='red')
    ,看看我的意思。不幸的是,我对统计数据了解不够,无法帮助您(例如,您的
    pi.hat
    计算是否错误,或者
    stat\u smooth
    是否正在进行一些您不知道的其他计算)。我所能建议的就是查看一下关于
    stat\u smooth
    的在线帮助,看看它是否提供了关于如何计算平滑度的任何信息。虽然我确信调整现有答案是微不足道的,但以目前的形式,它并没有回答这个问题。也就是说,由于线段的角点不在曲线上,因此图形不会被复制。@MYaseen208因为
    stat\u smooth
    没有传递与您在
    glm
    调用mod.fit中传递相同的选项。特别是,
    weight
    选项不会被传递。尝试将
    weight=n
    添加到
    ggplot
    呼叫中的
    aes
    。回答优雅。感谢您的帮助。在上述代码中,变量“Pi”和“LD”代表(代表)什么?@ErdoganCEVHER为了让这个特定的代码示例工作,变量的调用是否会有所不同?(通常,“LD50”是我见过的一个术语,指的是50%人口的致死剂量,但我认为它与这个问题没有太大关系。)绝对不是!我认为LD是“日志差异”,当我试图将代码与理论联系起来时,我遇到了麻烦。谢谢你的解释。也许,OP asker中的一些简单注释将在理论代码连接方面证明是有用的。
    geom_segment(data=LD.summary, aes(x=0, y=Pi,xend=LD, yend=Pi, colour=group) )
    
    LD.summary$group <- c('LD25','LD50','LD75')
    p <- ggplot(data = df, aes(x = x, y = y/n)) +
                geom_point() +
                stat_smooth(method = "glm", family = "binomial") +
                scale_y_continuous('Estimated probability')    # <-- add y label
    p <- p + geom_segment(data=LD.summary, aes( # <-- data=Ld.summary
                                x = LD
                              , y = 0
                              , xend = LD
                              , yend = Pi
                              , col = group     # <- colours
                             )
                           )    
    p <- p + geom_segment(data=LD.summary, aes( # <-- data=Ld.summary
                                x = min(df$x)   # <-- don't plot all the way to x=0
                              , y = Pi
                              , xend = LD
                              , yend = Pi
                              , col = group     # <- colours
                             )
                           )
    print(p)
    
    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")