Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created August 7, 2024 07:33
Show Gist options
  • Save abikoushi/e461607b5a99bd3f22f90acfac3226df to your computer and use it in GitHub Desktop.
Save abikoushi/e461607b5a99bd3f22f90acfac3226df to your computer and use it in GitHub Desktop.
Example: Getting confidence interval numerically
library(rootSolve)
LRtest <- function(p,x,n){
phat <- x/n
lp <- 2*(dbinom(x, n, phat ,log = TRUE)-
dbinom(x, n, p ,log = TRUE))
pchisq(lp, 1, lower.tail = FALSE)
}
set.seed(123)
n <- 20
x <- rbinom(1, n, 0.5)
print(x)
sol <- uniroot.all(function(p)LRtest(p,x,n)-0.05, c(0.1,0.8))
print(sol)
#cf
res_p <- prop.test(x,n)
print(res_p$conf.int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment