Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created June 1, 2024 08:46
Show Gist options
  • Save abikoushi/0ed0866718a3eb3d0ff96103bd432771 to your computer and use it in GitHub Desktop.
Save abikoushi/0ed0866718a3eb3d0ff96103bd432771 to your computer and use it in GitHub Desktop.
confidence intervals via sign test
binomfun <- function(c,n){
sum(choose(n,n/2+seq(-c,c))/(2^n))
}
signCI <- function(x,level){
n <- length(x)
sx <- sort(x)
alpha_c = sapply(1:(n/2), binomfun, n=n)
k <- which.max(alpha_c-(level)>0)
lower <- sx[k]
upper <- sx[n-k+1]
return(c(lower,upper))
}
set.seed(1)
x <- rweibull(50,2,1)
#png("sign_ci.png")
curve(pweibull(x,2,1, lower.tail = FALSE), 0, 4)
points(x,numeric(50))
levs <- c(0.1,0.25,0.5,0.6,0.7,0.8,0.9,0.95,0.99)
for(i in 1:length(levs)){
ci <- signCI(x,level=levs[i])
arrows(ci[1],levs[i],ci[2],levs[i],
col = "royalblue",
code=3,length = .05,
angle=90)
}
#dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment