Skip to content

Instantly share code, notes, and snippets.

@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)
@abikoushi
abikoushi / comp_hclustward.r
Created June 22, 2024 08:58
What is the difference between `ward.D` and `ward.D2`?
X <- as.matrix(iris[,-5])
d <- dist(X)
h1 <- hclust(d^2, method = "ward.D")
h2 <- hclust(d, method = "ward.D2")
h1$height <- sqrt(h1$height)
#png("dendro2.png")
par(mfrow=c(1,2))
plot(h1)
plot(h2)
@abikoushi
abikoushi / iris_svd.R
Created June 19, 2024 06:34
SVD in the Parallel coordinates plot
Y<-iris[,-5]
svdout <- svd(log(Y))
cname <- hcl.colors(3, alpha = 0.1)
#png("iris_svd.png")
matplot(t(svdout$u),
type = "l",lty=1,
col = cname[iris$Species],
xlab = "PC", ylab = "value")
legend("topleft", legend = levels(iris$Species),
col=hcl.colors(3), lty=1)