Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Last active August 6, 2024 14:19
Show Gist options
  • Save abikoushi/27388bbb9edf477e043407dcfe5b905d to your computer and use it in GitHub Desktop.
Save abikoushi/27388bbb9edf477e043407dcfe5b905d to your computer and use it in GitHub Desktop.
P-value function of prop.test
set.seed(1234)
n <- 20
x <- rbinom(1,n,0.5)
print(x)
probbeta <- function(p,x,n){
ahat <- x + .5
bhat <- n - x + .5
pl <- pbeta(p, ahat, bhat, lower.tail=TRUE)
pu <- pbeta(p, ahat, bhat, lower.tail=FALSE)
2*pmin(pl,pu)
}
z <- seq(0.01, 0.99, by=0.005)
pv_p <- sapply(z, function(p)prop.test(x, n, p = p, correct = FALSE)$p.value)
pv_b <- sapply(z, function(p)binom.test(x, n, p = p)$p.value)
p_beta <- probbeta(z,x,n)
library(ggplot2)
ggplot(data = NULL)+
geom_line(aes(x=z, y=pv_p, colour="score test", linetype = "score test"))+
geom_line(aes(x=z, y=pv_b, colour="binomial test", linetype = "binomial test"))+
geom_line(aes(x=z, y=p_beta, colour="Bayesian (conjugate beta prior)", linetype = "Bayesian (conjugate beta prior)"))+
scale_color_brewer(palette = "Set2")+
theme_bw(16)+theme(legend.position = "bottom")+
labs(x="param.", y="p-value", colour="method", linetype="method")
ggsave("proptest.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment