Reputation:
I need to add binomial confidence intervals to my plot.
Here are my steps:
library(binom)
library(plotrix)
x <- c(1:6)
y <- c(68, 69, 70, 75, 75, 87)
CI <- binom.confint(y, 265, conf.level = 0.95, methods = "exact")
plot(x, y)
plotCI(x, y, ui = CI$upper, li = CI$lower, add = TRUE)
I think I did everything correctly but my output plot doesn't seem right:
Do you have any suggestion?
Upvotes: 1
Views: 1065
Reputation: 226332
binom.confint
returns confidence intervals on the proportions, not on the total numbers (if you'd inspected the CI
object by printing it, you might have noticed this). Try
plotCI(x,y,ui=CI$upper*CI$n,li=CI$lower*CI$n)
(This combines your two plotting statements to plot the points and the error bars at the same time.)
Alternatively you could plot the proportions and their CIs:
plotCI(x,y/CI$n,ui=CI$upper,li=CI$lower)
Upvotes: 2
Reputation: 291
Have you taken into account the option of using ggplot2?
geom_smooth
gives you the 95% confidence level interval for predictions from a linear model ("lm").
data<-data.frame(y=c(20.7, 18, 21.4, 15.3, 27.3, 20),x=c(1:6))
library(ggplot2)
g<-ggplot(data,aes(x,y))
g+geom_point()+geom_smooth(method="lm")
The output will be:
Upvotes: 0