Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created August 18, 2024 02:31
Show Gist options
  • Save abikoushi/f62c81cd57639822ce5f706d32bf6d49 to your computer and use it in GitHub Desktop.
Save abikoushi/f62c81cd57639822ce5f706d32bf6d49 to your computer and use it in GitHub Desktop.
Check `poisson.test`
library(rootSolve)
pvfun0 <- function(r, x, tau){
pu <- ppois(x-1, r*tau, lower.tail = FALSE)
pl <- ppois(x, r*tau, lower.tail = TRUE)
2*pmin(pl, pu)
}
sol0 <-uniroot.all(f = function(p){pvfun0(p,5,10)-0.05},
interval = c(0.1,3))
sol0
res_p
res_p <- poisson.test(5, 10, r=1)
print(res_p$conf.int)
print(sol0)
#confidence interval from p-value
pvfun <- function(r){
sapply(r, function(r)poisson.test(5,10,r=r)$p.value)
}
sol <- uniroot.all(f = function(p){pvfun(p)-0.05},
interval = c(0.1,3))
print(sol)
###
#plot p-value fun
rvec <- seq(0.01,3,by=0.01)
pval <- sapply(rvec, function(r)poisson.test(5, 10, r = r)$p.value)
png("confint1.png")
plot(rvec, pval, type="s", xlab="param", ylab="p-value")
segments(x0 = res_p$conf.int[1], x1 = res_p$conf.int[2],
y0 = 0.05, y1 = 0.05, col="royalblue")
dev.off()
png("confint2.png")
plot(rvec, 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