geom_smooth,stat_smooth置信区间不起作用?

时间:2014-08-13 15:47:06

标签: r ggplot2

我有这些数据:

structure(list(Run = c("A013", "A015", "A023", "A024", "A031", 
"A032", "A035", "A040", "A045", "A046", "A049", "A013", "A015", 
"A023", "A024", "A031", "A032", "A035", "A040", "A045", "A046", 
"A013", "A015", "A023", "A024", "A031", "A032", "A035", "A040", 
"A013", "A015", "A023", "A024", "A031", "A032", "A035", "A040", 
"A013", "A015", "A023", "A024", "A031", "A032", "A013", "A015", 
"A023", "A024", "A013", "A015", "A023", "A024"), Step = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 
7L, 7L), .Label = c("1", "e", "k", "2", "q", "b", "m"), class = "factor"), 
    Weight = c(87.4064, 79.5822, 117.0674, 102.6384, 134.0752, 
    111.2398, 107.8464, 111.2576, 104.2428, 110.2848, 28.7292, 
    41.65656, 73.9356, 84.18504, 89.4845, 71.55106, 86.04072, 
    76.27296, 92.8749, 85.203, 91.92112, 39.5009258, 58.6035081, 
    75.13589946, 83.43157667, 88.8993795, 68.85183559, 64.77081269, 
    77.56733054, 32.5025, 51.45329, 66.29101, 73.79125, 79.95483, 
    60.9573, 58.34856, 68.83193, 29.65289, 40.74267, 56.97243, 
    61.48708, 70.24226, 54.79253, 22.8231064, 38.9966088, 55.2736576, 
    62.6077916, 20.7458048, 38.306526, 54.7937568, 61.1417148
    )), .Names = c("Run", "Step", "Weight"), row.names = c(NA, 
-51L), class = "data.frame")

我试图使用

以0.99的信心获得漂亮的geom_smooth()
require(ggplot2)
require(directlabels)
g1 <- ggplot(m1,
             aes(x=Step,y=Weight,label=Run,group=Run,color=Run)) + 
  geom_point() + geom_line()
g2 <- g1 +  geom_dl(method="first.bumpup")
g2 + geom_smooth(aes(group=1),level=0.99)

以下是我的问题:

  1. 错误功能区看起来没有99%的可信度,图表中的很多点都在它之外。

  2. 当我展开数据集时,错误功能区会折叠到非常狭窄的位置,而大多数点位于其外部。

  3. 我在这里做错了吗? 谢谢,

    编辑:这是我在运行时看到的内容: Narrow ribbon 当我查看更大的数据集时,色带变得更窄,几乎位于平滑线的顶部。

1 个答案:

答案 0 :(得分:3)

置信区间和预测区间是两种不同的野兽。前者是关于数据的平均值(拟合值),而后者是未来观察的位置。

这是我RPubs repository的代码。

set.seed(357)
library(ggplot2) # for ggplot()
library(gridExtra) 

x <- rnorm(20)
y <- x * rnorm(20, mean = 3, sd = 1)
xy <- data.frame(x, y)

mdl <- lm(y ~ x, data = xy)

# Predict these data for...
predx <- data.frame(x = seq(from = -2, to = 3, by = 0.1))

# ... confidence interval
conf.int <- cbind(predx, predict(mdl, newdata = predx, interval = "confidence", level = 0.95))

# ... prediction interval
pred.int <- cbind(predx, predict(mdl, newdata = predx, interval = "prediction", level = 0.95))
man <- predict(mdl, newdata = predx, se = TRUE)

# Manual calculation of confidence interval, tolerance of 0.95 (1.96).
lvl <- qt(1-(1 - 0.95)/2, mdl$df.residual) # Thank you, @Roland (http://chat.stackoverflow.com/transcript/message/10581408#10581408)
conf.int.man <- cbind(predx, fit = man$fit, lwr = man$fit - lvl * man$se.fit, upr = man$fit + lvl * man$se.fit)

g.conf <- ggplot(conf.int, aes(x = x, y = fit)) +
  theme_bw() +
  ggtitle("Confidence interval of estimated parameters from predict()") +
  geom_point(data = xy, aes(x = x, y = y)) +
  geom_smooth(data = conf.int, aes(ymin = lwr, ymax = upr), stat = "identity") 

g.pred <- ggplot(pred.int, aes(x = x, y = fit)) +
  theme_bw() +
  ggtitle("Prediction interval for future observations from predict()") +
  geom_point(data = xy, aes(x = x, y = y)) +
  geom_smooth(data = pred.int, aes(ymin = lwr, ymax = upr), stat = "identity")

grid.arrange(g.conf, g.pred, ncol = 2)

enter image description here