在自信的间隔之间画一条垂直线

时间:2016-03-21 12:30:26

标签: r

采用下面的数据,并且长途跋涉,我用置信区间绘制概率图。

我正在寻找下两个在CI_U和CI_L点之间创建一条红色垂直线,这些点已经为每个X绘制。

除了手动为每个x提供abline命令外,这可以自动化

以下是一些数据:

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

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西:

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

已经有几个类似问题的答案,例如:

Scatter plot with error bars

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