Created
August 17, 2024 15:59
-
-
Save abikoushi/a687eaee8787f103903881eaaff234fd to your computer and use it in GitHub Desktop.
Check `binom.test`
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
| library(rootSolve) | |
| pvfun0 <- function(p, x, n){ | |
| pu <- pbinom(x-1, n, p, lower.tail = FALSE) | |
| pl <- pbinom(x, n, p, lower.tail = TRUE) | |
| 2*pmin(pl, pu) | |
| } | |
| sol0 <-uniroot.all(f = function(p){pvfun0(p,5,10)-0.05}, | |
| interval = c(0.1,0.9)) | |
| res_b <- binom.test(5, 10) | |
| print(res_b$conf.int) | |
| print(sol0) | |
| pvfun <- function(p){ | |
| sapply(p, function(p)binom.test(5,10,p)$p.value) | |
| } | |
| sol <- uniroot.all(f = function(p){pvfun(p)-0.05}, | |
| interval = c(0.1,0.9)) | |
| print(sol) | |
| ### | |
| pvec <- seq(0.01,0.99,by=0.01) | |
| pval <- sapply(pvec, function(p)binom.test(5, 10, p = p)$p.value) | |
| png("confint1.png") | |
| plot(pvec, pval, type="s", xlab="param", ylab="p-value") | |
| segments(x0 = res_b$conf.int[1], x1 = res_b$conf.int[2], | |
| y0 = 0.05, y1 = 0.05, col="royalblue") | |
| dev.off() | |
| png("confint2.png") | |
| plot(pvec, pval, type="s", xlab="param", ylab="p-value") | |
| segments(x0 = sol[1], x1 = sol[2], | |
| y0 = 0.05, y1 = 0.05, col="orangered") | |
| dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment