If I have 10 values, each of which has a fitted value F
, and an upper and lower confidence interval U
and L
:
set.seed(0815) F <- runif(10, 1, 2) L <- runif(10, 0, 1) U <- runif(10, 2, 3)
How can I show these 10 fitted values and their confidence intervals in the same plot like the one below in R?
An interval plot is used to compare groups similar to a box plot or a dot plot. It is used when the data is continuous. Instead of plotting the individual data point, an interval plot shows the confidence interval for the mean of the data.
Creating confidence bars in Excel is relatively easy. First create your line chart. Then with the series selected, click on Chart Tools > Layout > Error bars > More error bar options. In the pop up menu that appears, you can either choose to have positive or negative error bars, or both.
Here is a plotrix solution:
set.seed(0815) x <- 1:10 F <- runif(10,1,2) L <- runif(10,0,1) U <- runif(10,2,3) require(plotrix) plotCI(x, F, ui=U, li=L)
And here is a ggplot solution:
set.seed(0815) df <- data.frame(x =1:10, F =runif(10,1,2), L =runif(10,0,1), U =runif(10,2,3)) require(ggplot2) ggplot(df, aes(x = x, y = F)) + geom_point(size = 4) + geom_errorbar(aes(ymax = U, ymin = L))
UPDATE: Here is a base solution to your edits:
set.seed(1234) x <- rnorm(20) df <- data.frame(x = x, y = x + rnorm(20)) plot(y ~ x, data = df) # model mod <- lm(y ~ x, data = df) # predicts + interval newx <- seq(min(df$x), max(df$x), length.out=100) preds <- predict(mod, newdata = data.frame(x=newx), interval = 'confidence') # plot plot(y ~ x, data = df, type = 'n') # add fill polygon(c(rev(newx), newx), c(rev(preds[ ,3]), preds[ ,2]), col = 'grey80', border = NA) # model abline(mod) # intervals lines(newx, preds[ ,3], lty = 'dashed', col = 'red') lines(newx, preds[ ,2], lty = 'dashed', col = 'red')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With