【问题标题】:How to draw confidence bounds to better demonstrate the confidence interval on ggplot2?如何绘制置信区间以更好地展示 ggplot2 上的置信区间?
【发布时间】:2018-11-08 13:05:33
【问题描述】:

此代码绘制回归线、置信区间和预测区间。置信区间为灰色。我需要添加一段代码来绘制置信区间,以更好地展示置信区间。

library(ggplot2)
x<-Allele<- c(4,4,5,5,5,7,9,11,11,11,13,14,15,15,16,17,17,19,19,19)
y<-SR<- c(5.5,5.6,5.7,6,6.1,8,8,12,12.1,13,14,14,16,16.5,17,17,
17.1,18,19,20)
D3S = data.frame(Allele, SR)
Mod <- lm(SR ~ Allele) 
pred.int <- predict(Mod, interval = "prediction")
mydata <- cbind(D3S, pred.int)
# Regression line + confidence interval
p <- ggplot(mydata, aes(Allele, SR)) +
geom_point(size=3) +
stat_smooth(method = lm, color="black")
# Add prediction intervals
myplot= p + geom_line(aes(y = lwr), color = "black", 
linetype = "dashed", size=0.8)+
geom_line(aes(y = upr), color = "black", 
linetype = "dashed", size=0.8)
myplot + theme_bw() + theme(panel.border = element_blank(), 
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), 
axis.line = element_line(colour = "black"))

【问题讨论】:

标签: r ggplot2 confidence-interval


【解决方案1】:

您几乎在原始代码中就有了它。使用 interval = "confidence" 重复预测并将这些行添加到您的绘图中。

# Right after you get the prediction intervals:
conf.int <- predict(Mod, interval = "confidence")

# Change the cbind line to this:
mydata <- data.frame(D3S, pred.int, conf.int) # checks names and adds .1 to dupes

# Right after you added prediction interval, do this:
myplot <- myplot + 
  geom_line(aes(y = lwr.1), color="red") + 
  geom_line(aes(y = upr.1), color="red")

现在像以前一样制作最终的情节。

【讨论】:

    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2023-04-07
    • 2020-05-01
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多