Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created August 6, 2024 03:13
Show Gist options
  • Save abikoushi/ca9ff8d6826b9ee496e3b84bef642ed1 to your computer and use it in GitHub Desktop.
Save abikoushi/ca9ff8d6826b9ee496e3b84bef642ed1 to your computer and use it in GitHub Desktop.
Checking prop.test (confidence interval of the proportion)
set.seed(1234)
n <- 12
x <- rbinom(1,n,0.5)
res_p <- prop.test(x, n, p = 0.5, correct = FALSE)
CI_score <- function(x, n, level){
z <- qnorm(0.5*(1-level), lower.tail = FALSE)
phat <- x/n
t_1 <- phat+(z^2)/(2*n)
t_2 <- sqrt(z^2+4*n*phat*(1-phat))*(z/(2*n))
c((t_1 - t_2)/(1+(z^2)/n),
(t_1 + t_2)/(1+(z^2)/n))
}
print(CI_score(x,n,0.95))
print(res_p$conf.int)
#cf.
res_b <- binom.test(x, n, p = 0.5)
print(res_b$conf.int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment