Skip to content

Instantly share code, notes, and snippets.

@abikoushi
abikoushi / Density_FMVNCHypergeo.R
Last active October 27, 2025 05:04
Calculate density of the multivariate Fisher's noncentral hypergeometric distribution
library(BiasedUrn)
softmax <- function(x){
mx = max(x)
ex = exp(x-mx)
ex/sum(ex)
}
MVNCH_Fisher <- function (x, m, weight) {
@abikoushi
abikoushi / Density_FMVNCHypergeo.R
Last active October 22, 2025 09:54
Check probability function of a multivariate Fisher's noncentral hypergeometric distribution
library(BiasedUrn)
softmax <- function(x){
mx = max(x)
ex = exp(x-mx)
ex/sum(ex)
}
MVNCH_Fisher <- function(K,M,OR){
CB = combn(K,M)
nr = ncol(CB)
@abikoushi
abikoushi / MVNCNHypergeo.R
Created October 21, 2025 03:53
Try multivariate noncentral hypergeometric distribution in BiasedUrn package (without replacement)
library(BiasedUrn)
library(dplyr)
K=10
set.seed(1234); OR = rexp(K)
M=3
ressim = replicate(100000, sort(sample.int(K, size = M, prob = OR, replace = FALSE))) %>%
t() %>%
as.data.frame() %>%
group_by_all() %>%
tally()
@abikoushi
abikoushi / MVNCNHypergeo.R
Created October 20, 2025 08:24
Try multivariate noncentral hypergeometric distribution in BiasedUrn package
library(BiasedUrn)
library(dplyr)
K=10
set.seed(1234); OR = rexp(K)
ressim = sample.int(K, size = 10000, prob = OR, replace = TRUE) |>
table() |>
as.data.frame()
ressim = mutate(ressim, prob = Freq/sum(Freq))
@abikoushi
abikoushi / try_brunnermunzel.R
Created October 20, 2025 07:20
Comparison Brunner-Munzel and Wilcoxon rank sum test (my first try)
library(brunnermunzel)
#https://cran.r-project.org/web/packages/brunnermunzel/vignettes/usage.html
## median of the difference: point a s.t. P(X-Y<a)+.5*P(X-Y=a) = 0.5
conv <- function(x){
#numerical convolution
integrate(function(y){dnorm(x+y,mu)*dexp(y)},-Inf,0)$value +
integrate(function(y){dnorm(x+y,mu)*dexp(y)},0,Inf)$value
}
conv <- Vectorize(conv)
@abikoushi
abikoushi / simIV.R
Last active October 13, 2025 14:38
A simulation of the method of Instrumental Variables
library(DiagrammeR)
library(DiagrammeRsvg)
library(rsvg)
library(dplyr)
library(dqrng)
library(ggplot2)
g <- grViz("digraph{
graph[rankdir = TB]
node[shape = none]
@abikoushi
abikoushi / DAG.R
Created October 13, 2025 05:23
Try `dagitty` and `ggdag`
library(dagitty)
library(ggdag)
library(ggplot2)
library(gridExtra)
g1 <- dagitty("dag{U -> X; U ->Y; X -> Y}")
exposures(g1) <- "X"
outcomes(g1) <- "Y"
plot(g1)
@abikoushi
abikoushi / binom_beta_nbinom.R
Created October 11, 2025 10:09
triplet of Beta, Binomial, and Negative Binomial distribution
library(ggplot2)
NB_p <- function(p,x,r){
pnbinom(x, size = r, prob = p,lower.tail = TRUE)
}
B_p <- function(p,x,r){
pbinom(r-1, size = r+x, prob = p, lower.tail = FALSE)
}
@abikoushi
abikoushi / hdi_beta.r
Created September 26, 2025 07:47
highest density interval of beta distribution
library(rootSolve)
findall_q = function(q_ini, a, b, ...){
dp = dbeta(q_ini, shape1 = a, shape2 = b)
sol = rootSolve::uniroot.all(function(X)dbeta(X, shape1 = a, shape2 = b)-dp, interval = c(0, 1), ...)
sort( sol )
}
prob_diff <- function(q, a, b){
p = pbeta(q, shape1 = a, shape2 = b)
diff( p )
@abikoushi
abikoushi / sankeychart.r
Created September 24, 2025 08:28
tried using ggalluvial
#install.packages("ggalluvial")
library("ggalluvial")
library("dplyr")
V_row=matrix(rexp(100*10),100,10)
V_col=matrix(rexp(100*10),100,10)
W_row = matrix(rexp(10*5),10,5)
cl_row = apply(V_row%*%W_row,1,which.max)
W_col = matrix(rexp(10*7),10,7)