[英]Plotting confidence intervals from a dataframe
I have written some R code which produces the limits of confidence intervals as well as the information if each confidence interval covers the true parameter. 我编写了一些R代码,这些代码会生成置信区间的限制以及每个置信区间覆盖真实参数的信息。 I'd like to visualize this but have no idea how.
我想对此进行可视化,但不知道如何实现。
confInt <- function(runs){
result<-NULL
vleft<-NULL
vright<-NULL
for (i in 1:runs) {
data<-rnorm(1000)
n<-length(data)
a<-mean(data)
s<-sd(data)
error <- qnorm(0.975)*s/sqrt(n)
left <- a-error
right <- a+error
result[i] = left<0 & 0<right
vleft[i] = left
vright[i] = right
}
data.frame(result,vleft,vright)
}
confInt(100)
EDIT: I have found a way using ggplot2
编辑:我找到了一种使用
ggplot2
confInt <- function(runs){
x<-1:runs
mu<-NULL
sigma<-NULL
result<-NULL
vleft<-NULL
vright<-NULL
for (i in 1:runs) {
data<-rnorm(1000)
n<-length(data)
a<-mean(data)
mu[i]<-a
s<-sd(data)
sigma[i]<-s
error <- qnorm(0.975)*s/sqrt(n)
left <- a-error
right <- a+error
result[i] = left<0 & 0<right
vleft[i] = left
vright[i] = right
}
data.frame(x,mu,sigma,result,vleft,vright)
}
df<-confInt(100)
require(ggplot2)
myplot<-ggplot(df, aes(x = x, y = mu)) +
geom_point(size = 2) +
geom_errorbar(aes(ymax = vleft, ymin = vright,colour=result*3))
myplot + theme_bw()
summary(df)
There are many ways to approach this. 有很多方法可以解决此问题。 Below, I use
mapply
to feed the the starting and ending points of each confidence interval to segments
. 在下面,我使用
mapply
将每个置信区间的起点和终点馈送到segments
。
ci <- confInt(100)
plot(y = c(0, 100), x = c(-.15,.15), type = 'n', ylab = "",
xlab = "", yaxt = 'n')
with(ci, mapply(segments, x0 = vleft, x1 = vright,
y0 = 1:100, y1 = 1:100, col = (!result) + 1))
abline(v = 0, col = 'purple')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.