r - Does geom_smooth() of ggplot2 show pointwise confidence bands, or simultaneous confidence bands? -


i unsure whether question more appropriate here or on cross validated. hope made right choice.

consider example:

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)) 

enter image description here default, ggplot "displays confidence interval around smooth" (see here), given gray area around regression curve. i've assumed these simultaneous confidence bands regression curve, not pointwise confidence bands. ggplot2 documentation refers predict function details on how standard errors computed. however, reading doc predict.lm, doesn't explicitly simultaneous confidence bands computed. so, correct interpretation here?

one way check predict.lm() computes inspect code (predict multiplies standard errors qt((1 - level)/2, df), , not appear make adjustments simultaneous inference). way construct simultaneous confidence intervals , compare them against predict's intervals.

fit model , construct simultaneous confidence intervals:

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) 

reshape , plot:

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") 

it appears predict(.., interval = "confidence") not produce simultaneous confidence intervals:

enter image description here


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -