簡體   English   中英

ggplot2的geom_smooth()是否顯示逐點置信帶或同時置信帶?

[英]Does geom_smooth() of ggplot2 show pointwise confidence bands, or simultaneous confidence bands?

我不確定這個問題在此處還是在交叉驗證時是否更合適。 我希望我做出了正確的選擇。

考慮示例:

library(dplyr)
setosa <- iris %>% filter(Species == "setosa") %>% select(Sepal.Length, Sepal.Width, Species)
library(ggplot2)
ggplot(data = setosa, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point() +
geom_smooth(method ="lm", formula = y ~ poly(x,2))

在此處輸入圖片說明 默認情況下, ggplot “顯示平滑周圍的置信區間”(請參見此處 ),由回歸曲線周圍的灰色區域給出。 我一直假設這些是回歸曲線的同時置信帶 ,而不是點狀置信帶。 ggplot2文檔參考了predict函數,以獲取有關如何計算標准誤差的詳細信息。 但是,在閱讀有關predict.lm的文檔時,並沒有明確說要同時計算置信帶。 那么,這里的正確解釋是什么?

檢查predict.lm()計算的一種方法是檢查代碼( predict將標准誤差乘以qt((1 - level)/2, df) ,因此似乎無法為同時推斷進行調整)。 另一種方法是構造同時置信區間,並將其與predict的區間進行比較。

擬合模型並構造同時置信區間:

setosa <- subset(iris, Species == "setosa")
setosa <- setosa[order(setosa$Sepal.Length), ]
fit <- lm(Sepal.Width ~ poly(Sepal.Length, 2), setosa)

K <- cbind(1, poly(setosa$Sepal.Length, 2))
cht <- multcomp::glht(fit, linfct = K)
cci <- confint(cht)

重塑和繪制:

cc <- as.data.frame(cci$confint)
cc$Sepal.Length <- setosa$Sepal.Length
cc <- reshape2::melt(cc[, 2:4], id.var = "Sepal.Length")

library(ggplot2)
ggplot(data = setosa, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point() +
  geom_smooth(method ="lm", formula = y ~ poly(x,2)) +
  geom_line(data = cc, 
            aes(x = Sepal.Length, y = value, group = variable),
            colour = "red")

似乎predict(.., interval = "confidence")不會同時產生置信區間:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM