代码之家  ›  专栏  ›  技术社区  ›  Zmnako Awrahman beloncfy

绘制预测。具有置信区间的lm结果

  •  -1
  • Zmnako Awrahman beloncfy  · 技术社区  · 8 年前

    我有一个df,它是 predict.lm 附加到另一个带有两列的df label actual

    df <- data.frame(
    label = c('A', 'B', 'C', 'D', 'E', 'F', 'G'), 
    actual = c(13.9, 13.4, 13.8, 14.3, 14.2, 13.6, 14.1),
    fit = c(13.8, 13.9, 14.1, 14.0, 13.9, 14.3, 14.1),
    lwr = c(13.6, 13.7, 13.8, 13.8, 13.7, 14.0, 13.9),
    upr = c(14.3, 14.2, 14.7, 14.3, 14.1, 14.9, 14.9)
    )
    

    我想策划 真实的 vs fit 哪里 lwr upr 与下图类似,没有线条,点是基于颜色或图案 标签 柱 这个数字是一个例子,我希望我的数字看起来像什么。x和y不是来自我的数据。 plot

    2 回复  |  直到 8 年前
        1
  •  2
  •   Roman LuÅ¡trik    8 年前

    像这样的?

    library(ggplot2)
    
    df <- data.frame(
      label = c('A', 'B', 'C', 'D', 'E', 'F', 'G'), 
      actual = c(13.9, 13.4, 13.8, 14.3, 14.2, 13.6, 14.1),
      fit = c(13.8, 13.9, 14.1, 14.0, 13.9, 14.3, 14.1),
      lwr = c(13.6, 13.7, 13.8, 13.8, 13.7, 14.0, 13.9),
      upr = c(14.3, 14.2, 14.7, 14.3, 14.1, 14.9, 14.9)
    )
    
    ggplot(df, aes(x = actual, y = fit)) +
      theme_bw() +
      geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = 0.5) +
      geom_line(color = "blue") +
      geom_point(aes(color = label))
    

    enter image description here

        2
  •  1
  •   G5W    8 年前

    你可以用 plot 然后使用 polygon .

    ORD = order(df$actual)
    plot(df[ORD, 2:3],type="l", ylim=c(min(df$lwr), max(df$upr)), col="blue")
    polygon(c(df$actual[ORD], df$actual[rev(ORD)]), border=NA,
        c(df$upr[ORD], df$lwr[rev(ORD)]), col="#88888844")
    

    Plot with error limits

    如果想要更平滑的版本,可以使用样条曲线。

    ## Smoothed version
    plot(spline(df[ORD, 2:3]), type="l", 
        ylim=c(min(df$lwr), max(df$upr)), col="blue")
    
    UpperFun = splinefun(df[ORD, c(2,5)])
    LowerFun = splinefun(df[ORD, c(2,4)])
    ACT      = seq(min(df$actual), max(df$actual), 0.02)
    UPP      = UpperFun(ACT)
    LOW      = LowerFun(ACT)
    
    polygon( c(ACT, rev(ACT)), c(UPP, rev(LOW)), 
        border=NA, col="#88888844")
    

    Smoothed curve

    推荐文章