Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created June 28, 2024 13:07
Show Gist options
  • Save abikoushi/c0cd2acfb7d47a693f4d76d070a9377b to your computer and use it in GitHub Desktop.
Save abikoushi/c0cd2acfb7d47a693f4d76d070a9377b to your computer and use it in GitHub Desktop.
P-value function via Fisher's exact test
library(MCMCpack)
library(exact2x2)
library(ggplot2)
library(patchwork)
X <-matrix(c(12,5,6,12), nrow=2)
fpfun <- function(phi){exact2x2::exact2x2(x=X, or=exp(phi))$p.value}
rs <- rowSums(X)
cs <- colSums(X)
flfun <- function(phi){MCMCpack::dnoncenhypergeom(x = X[1,1], cs[1],cs[2],rs[1], exp(phi))}
phi <- seq(-10,10,by=0.1)
pv <-sapply(phi, fpfun)
lv <-sapply(phi, flfun)
df <- data.frame(logOR =phi, pvalue=pv, likelihood=lv)
p1 <- ggplot(df,aes(x=logOR))+
geom_line(aes(y=pvalue))+
theme_bw()
p2 <- ggplot(df,aes(x=logOR))+
geom_line(aes(y=likelihood))+
theme_bw()
p1/p2
#ggsave("pfun_fisher.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment