【问题标题】:Confidence Interval/Band for a Loess to Replicate geom_smooth黄土复制 geom_smooth 的置信区间/带
【发布时间】:2017-06-24 12:54:26
【问题描述】:

我想获取 loess 函数中每个观察值的置信区间的上限和下限,以复制 ggplotgeom_smooth() 中所做的事情

library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = hp)) +
  geom_point() +
  geom_smooth(method = 'loess')

我知道我可以从线性模型中获得上限和下限,但这不适用于 loess:

lm_mod <- lm(hp ~ mpg, mtcars)
predict(lm_mod, mtcars, interval="confidence", level=0.95)

loess_mod <- loess(hp ~ mpg, mtcars)
predict(loess_mod, mtcars, interval="confidence", level=0.95)

【问题讨论】:

    标签: r ggplot2 machine-learning


    【解决方案1】:

    想通了!置信区间可以从标准误差中计算出来,可以使用se = TRUE 参数添加预测对象。 1.96 个标准差相当于 95% 的置信区间(具有正态分布,因此假设误差为正态)。

    loess_mod <- loess(hp ~ mpg, mtcars)
    pred <- predict(loess_mod, mtcars, se=TRUE)
    mtcars1$lwl <- pred$fit-1.96*pred$se.fit
    mtcars1$upl <- pred$fit+1.96*pred$se.fit
    
    library(ggplot2)
    ggplot(mtcars1, aes(x = mpg, y = hp)) +
      geom_point() +
      geom_smooth(method = 'loess') +
      geom_line(aes(y = lwl), color = "red") +
      geom_line(aes(y = upl), color = "red")
    

    希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 2012-05-18
      • 2018-01-26
      • 2013-10-09
      相关资源
      最近更新 更多