Last active
December 30, 2015 14:29
-
-
Save clayford/7842136 to your computer and use it in GitHub Desktop.
Plotting confidence intervals to see which contain true mean.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a blank plot | |
plot(x=c(10,20),y=c(0,40), type="n") | |
abline(v=15,lty=2) | |
# plot line segments to represent confidence intervals | |
# add red lines for CI's that do not capture true mean of 15 | |
# add dots for means | |
for(i in 1:40){ | |
x <- rnorm(30,15,2) | |
res <- t.test(x,mu=15) | |
if(res$conf.int[1]>15 | res$conf.int[2]<15){ | |
segments(x0=res$conf.int[1],y0=i,x1=res$conf.int[2],y1=i,col="red") | |
points(x=res$estimate, y=i, pch=19, col="red") | |
} else { | |
segments(x0=res$conf.int[1],y0=i,x1=res$conf.int[2],y1=i) | |
points(x=res$estimate, y=i, pch=19) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment