Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot data with confidence intervals?

Tags:

plot

r

intervals

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?

enter image description here

like image 879
Kazo Avatar asked Dec 28 '12 12:12

Kazo


People also ask

Can you plot confidence intervals?

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.

How do you plot a confidence interval in Excel?

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.


1 Answers

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) 

enter image description here

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

enter image description here

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

enter image description here

like image 68
EDi Avatar answered Oct 15 '22 01:10

EDi