绘制从正态分布中抽取的均值的95%置信区间

时间:2017-09-19 23:48:35

标签: r ggplot2

我从正态分布中抽取了100个10号样本,平均值为10,标准差为2。代码如下:

n <- 10
nreps<-100
sample.mean<-numeric(nreps)
for (i in 1:nreps) {
    sample <- rnorm(n=n, mean = 10, sd = 2)
    sample.mean[i] <- mean(sample)
    a <- qnorm(0.95*2/sqrt(n))
    ci <- a
}
plot(sample.mean, 1:100)

我想创建一个看起来像这样的图表

这就是我目前所拥有的

我知道我需要解释每个均值的左手边和右手边界,然后在它们之间插入一条水平线。超出95%置信区间的手段应该与其他手段的颜色不同。我刚刚开始学习R,所以非常感谢有用的演练。

1 个答案:

答案 0 :(得分:1)

以这种方式尝试:

library(ggplot2)
set.seed(1321)
n <- 10
sd <- 2
n.reps <- 100
my.mean <- 10
alpha <- 0.05

mydata <- matrix(rnorm(n = n.reps * n, mean = my.mean, sd =sd), ncol = n.reps)

sample.means <- apply(mydata, 2, mean)

error <- apply(mydata, 2, function(x) qt(p=1-alpha/2,df=length(x)-1)*sd (x)/sqrt(length(x)))


dfx <- data.frame(sample.means, error, lcl = sample.means-error, ucl = sample.means+error, trial = 1:n.reps)
dfx$miss <- dfx$ucl < my.mean | dfx$lcl > my.mean
ggplot(dfx, aes(x = sample.means, y = trial, xmin = lcl, xmax = ucl, color = miss)) + geom_errorbarh() + geom_point(pch = 1) +
  geom_vline(aes(xintercept=my.mean), lty=2) +
  xlab("True Mean in Blue and 95% Confidence Intervals") + ylab ("Trial") + ggtitle(paste("Successful CI's:", 100*mean(!dfx$miss), "%")) + scale_color_manual(values = c("green", "red")) +
  theme_bw()

或使用base:

oldpar <- par(xpd=FALSE)
par(mar=c(8.1, 3.1, 3.1, 4.1))

with(subset(dfx, !miss), plot(sample.means, trial, 
                              xlab = "Sample Mean", 
                              ylab = "Trial",
                              col = "forestgreen",
     xlim=c(min(dfx$lcl), max(dfx$ucl))))
with(subset(dfx, miss), points(sample.means, trial,
                              col = "red"))

with(subset(dfx, miss), segments(lcl, trial, ucl, trial, col = "red"))
with(subset(dfx, !miss), segments(lcl, trial, ucl, trial, col = "forestgreen"))
abline(v = my.mean, lty = 2, lwd = 2, col = "blue")

par(xpd=TRUE)
legend("bottomright", c("Successful CI", "Miss"), lty = c(1,1), col = c("forestgreen", "red"),
       inset=c(-0.1,-0.45))

title(main = paste("Successful CI's:", 100*mean(!dfx$miss), "%"),
      sub = "True mean (in blue) and CI's")
par(oldpar)

HTH 詹姆斯