1

Taking the data below, and going the long way round, I make a plot of probabilities with confidence intervals.

I am looking next two create a red vertical line between the CI_U and CI_L points that have been plotted for each X.

Other than manually giving a abline command per x, can this be automated

Here is some data to follow:

set.seed(123)

x <- sample(1:10, 2000, replace = T)

y <- c(rep(0.1, times = 500),rep(0.25, times = 500),rep(0.4, times = 500),rep(0.85, times = 500))

z <- rbinom(n=2000, size=1, prob=y)

df1 <- data.frame(x,z)
df1$x <- as.factor(df1$x)



# ---------------------------------------------------------------------------------------------------------------------------------
# 1. we want the counts of success per interval
success<-(1:10)
for (i in 1:10) {
  success[i] <- (sum(df1$z[df1$x==success[i]]))
}
success

# 1. we want the counts of population in each two year interval
population<-(1:10)
for (i in 1:10) {
  population[i] <- length(df1$z[df1$x==population[i]])
}
population

X <- c(1:10)

df2 <- data.frame(X, success, population)
df2$pred <- df2$success/df2$population

# in a similiar manner, calculate the ratio of those injured to not, per vehicle year
CI_L<-(1:10)
for (i in 1:10) {
  CI_L[i] <- binom.test(df2$success[i], df2$population[i], df2$pred[i])[[4]][[1]]
}

# in a similiar manner, calculate the ratio of those injured to not, per vehicle year
CI_U<-(1:10)
for (i in 1:10) {
  CI_U[i] <- binom.test(df2$success[i], df2$population[i], df2$pred[i])[[4]][[2]]
}

df2$CI_U <- CI_U
df2$CI_L <- CI_L

# create our plot
plot(df2$X, df2$pred, ylim = c(0,1))
points(df2$X, df2$CI_L, pch='-',, add = T, col='red')
points(df2$X, df2$CI_U, pch='-', add = T, col='red')
lukeg
  • 1,327
  • 3
  • 10
  • 27

1 Answers1

0

I think this is what you're looking for:

plot(df2$X, df2$pred, ylim = c(0,1))
arrows(df2$X, df2$CI_L, df2$X, df2$CI_U, length=0.05, angle=90, code=3, col = 'red')

There are several answers to similar questions already, for example:

Scatter plot with error bars

Add error bars to show standard deviation on a plot in R

Community
  • 1
  • 1
KMcL
  • 76
  • 4