Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / binomtest.R
Created August 17, 2024 15:59
Check `binom.test`
library(rootSolve)
pvfun0 <- function(p, x, n){
pu <- pbinom(x-1, n, p, lower.tail = FALSE)
pl <- pbinom(x, n, p, lower.tail = TRUE)
2*pmin(pl, pu)
}
sol0 <-uniroot.all(f = function(p){pvfun0(p,5,10)-0.05},
interval = c(0.1,0.9))
res_b <- binom.test(5, 10)
print(res_b$conf.int)
@abikoushi
abikoushi / confint_lr.R
Created August 7, 2024 07:33
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)
@abikoushi
abikoushi / pvalfun_prop.R
Last active August 6, 2024 14:19
P-value function of prop.test
set.seed(1234)
n <- 20
x <- rbinom(1,n,0.5)
print(x)
probbeta <- function(p,x,n){
ahat <- x + .5
bhat <- n - x + .5
pl <- pbeta(p, ahat, bhat, lower.tail=TRUE)
pu <- pbeta(p, ahat, bhat, lower.tail=FALSE)
@abikoushi
abikoushi / check_prop.R
Created August 6, 2024 03:13
Checking prop.test (confidence interval of the proportion)
set.seed(1234)
n <- 12
x <- rbinom(1,n,0.5)
res_p <- prop.test(x, n, p = 0.5, correct = FALSE)
CI_score <- function(x, n, level){
z <- qnorm(0.5*(1-level), lower.tail = FALSE)
phat <- x/n
t_1 <- phat+(z^2)/(2*n)
t_2 <- sqrt(z^2+4*n*phat*(1-phat))*(z/(2*n))
@abikoushi
abikoushi / miniLineTable.R
Created July 19, 2024 09:20
sparkline with dendrogram
plot.miniLineTable <- function(tab,...){
oldpar <- graphics::par(no.readonly = TRUE)
N <- length(tab$y)
graphics::par(mar = c(4, 10, 0, 2), oma = rep(1, 4))
graphics::layout(mat = cbind(N+1,1:N,1:N), respect = FALSE)
for (i in 1:N) {
tmpy <- tab$y[[i]]
matplot(tmpy, type = "l", lty=1, xaxt="n", yaxt="n",
xlab = "", ylab = "", frame.plot = FALSE,
col=1)
@abikoushi
abikoushi / kinetics.R
Created July 12, 2024 07:59
RNA kinetics
library(deSolve)
library(ggplot2)
RNAvelo <- function(Time, State, Pars, input) {
with(as.list(c(State, Pars)), {
import <- input(Time)
du <- import - beta * U
ds <- beta * U - gamma * S
return(list(c(du, ds)))
})
@abikoushi
abikoushi / pplogOR.jl
Last active July 9, 2024 09:32
Chi-square vs Fisher exact test
using Distributions
using Plots
function Chisqtest(tab, beta0)
beta = log(tab[2,2]) - log(tab[2,1]) - (log(tab[1,2])-log(tab[1,1]))
V = sum(inv.(tab))
return ccdf(Chisq(1.0), (beta-beta0)^2/V)
end
function Fishertest(X, phi)
@abikoushi
abikoushi / fisherOR.jl
Created July 8, 2024 15:48
p-values of fisher exact test (and likelihood)
using Distributions
using Plots
function Fishertest(X, phi)
cs = sum(X, dims=1)
rs = sum(X, dims=2)
f(x, phi) = pdf(FisherNoncentralHypergeometric(cs[1],cs[2],rs[1], exp(phi)), x)
prob = f.(0:cs[1], phi)
return sum(prob[prob .<= prob[X[1,1]+1]])
@abikoushi
abikoushi / pfun_fisher.R
Created June 28, 2024 13:07
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))}
@abikoushi
abikoushi / pvfun_wilcox.R
Created June 28, 2024 06:24
An example of P-value function
library(ggplot2)
set.seed(123)
x <- rweibull(50,1.5) - log(2)^(1/1.5)
qweibull(0.5,1.5)- log(2)^(1/1.5) #中央値が0の分布
sx <- jitter(sort(x)) #ちょうど0をなくすため
pv_w <- sapply(sx,function(m)wilcox.test(x, mu=m)$p.value)
pv_s <- sapply(sx,function(m)binom.test(sum(x>m), length(x))$p.value)
df4p <- data.frame(x=x,sx=sx,sign=pv_s,wilcox=pv_w)
m <- median(x)