Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created June 6, 2024 07:49
Show Gist options
  • Save abikoushi/3afd168d468116089cf0c31e8c4c6585 to your computer and use it in GitHub Desktop.
Save abikoushi/3afd168d468116089cf0c31e8c4c6585 to your computer and use it in GitHub Desktop.
P-value function of sign test
library(ggplot2)
library(dplyr)
sign_p <- function(mu, x, prob=0.5){
x2 <- x-mu
x2 <- x2[x2 != 0]
pos <- sum(x2>0)
p1 = pbinom(pos, length(x2), prob = prob, lower.tail = FALSE)
p2 = pbinom(pos, length(x2), prob = prob, lower.tail = TRUE)
data.frame(p = 2*min(p1, p2), location=mu)
}
curve(pweibull(x+log(2)^(1/1.5),1.5),-log(2)^(1/1.5),4)
abline(h=0.5, lty=2)
abline(v=0, lty=2)
set.seed(123);x <- rweibull(50,1.5) - log(2)^(1/1.5)
dfps <- bind_rows(lapply(sort(x), sign_p, x=x))
ggplot(dfps,aes(x=location))+
geom_rug()+
geom_step(aes(y=p))+
labs(y="p-value")+
theme_bw(18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment