gpt4 book ai didi

r - 添加置信区间ggplot

转载 作者:行者123 更新时间:2023-12-04 12:34:18 29 4
gpt4 key购买 nike

我想绘制拟合值的置信区间。我读了一些相关的帖子,但我仍然卡住了..这些是日期的样本:

pd <-structure(list(date = 1:5, obs = c(44.6651011845397, 62.3441339250369, 
52.8968240161506, 51.7795930633478, 63.1284636561025), pred = c(47.2643891039645,
55.7996450577325, 52.9566469533233, 51.3393289316, 59.0011440099732)),
.Names = c("date", "obs", "pred"), row.names = c(NA, 5L), class = "data.frame")

pd2 <- structure(list(date = 1:5, lwr = c(44.8529592578518, 54.9926370476338,
51.7358955911624, 49.401869166722, 58.1674619447108), upr = c(49.6758189500772,
56.6066530678312, 54.1773983154842, 53.2767886964779, 59.8348260752356
)), .Names = c("date", "lwr", "upr"), row.names = c(NA, 5L), class = "data.frame")

dd <- melt(pd, id=c("date")) #Data
dd2 <- melt(pd2,id=c("date")) #Intervals of conf.
p <- ggplot(dd) + geom_line(aes(x=date, y=value, colour=variable))
p <- p + geom_smooth(aes(x=date, y=value, ymax=lwr, ymin=upr), #1 way
colour='grey', data=dd2, stat='identity')

也试过...

# p+ geom_ribbon(data=dd2,aes(ymin=lwr,ymax=upr),alpha=0.3) #2.

我收到错误: Error in eval(expr, envir, enclos) : object 'lwr' not found ....我错过了什么?

我也尝试在不使用 melt 的情况下做到这一点......但后来我也遇到了传说的问题!

最佳答案

在第一种方式中,您使用的 dd 对象没有 lwr/upr 列。所以你不能真正地绘制它们。

你能做一个:

dd<-merge(dd,pd2,by='date')

就在融化之后,然后:
p <- ggplot(dd) + geom_line(aes(x=date, y=value, colour=variable)) 
p + geom_ribbon(data=dd,aes(x=date, y=value, ymin=lwr,ymax=upr, group=variable),alpha=0.3)

这有帮助吗?

关于r - 添加置信区间ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27017013/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com