Created
May 24, 2016 18:02
-
-
Save sdchasalow/b8927e5dc75019145f9684dac3aeaf7f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
n <- 50 | |
pvals <- 0.5 | |
nosim <- 100 | |
coverage <- lapply(pvals, function(p) { | |
phats <- (rbinom(nosim, prob = p, size = n) + 2)/(n + 4) | |
ll <- phats - qnorm(0.975) * sqrt(phats * (1 - phats)/n) | |
ul <- phats + qnorm(0.975) * sqrt(phats * (1 - phats)/n) | |
cbind(lower = ll, upper = ul) | |
}) | |
png("binomialCI-simulation.png") | |
plot( 0:1, c(0, 101), type = 'n', xlab = "95% CI for p", ylab = "Replicate Number") | |
abline(v = 0.5, lty = 2) | |
segments(coverage[[1]][,1], 1:100, coverage[[1]][, 2], 1:100) | |
title("Simulation of 100 coin flips with true p = 0.5") | |
dev.off() |
Author
sdchasalow
commented
May 24, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment