Skip to content

Instantly share code, notes, and snippets.

@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)
@abikoushi
abikoushi / biased_urn.R
Last active October 2, 2025 06:27
Multivariate Wallenius’ Non-Central Hypergeometric distribution in R
# https://en.wikipedia.org/wiki/Wallenius%27_noncentral_hypergeometric_distribution#Multivariate_distribution
# https://markheckmann.github.io/notes/wallenius-distribution.html
library(dplyr)
library(tibble)
library(BiasedUrn)
set.seed(1234); R = rexp(10)
ressim = t(replicate(1e5L,sort(sample.int(10, size = 3, prob = R))))
pmf = data.frame(ressim) %>%
group_by_all() %>%
@abikoushi
abikoushi / case1.tex
Created September 12, 2025 14:53
TikZ bayesnet example
\documentclass[tikz, border=14pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{tikzpicture}
\node[obs] (x) {$X$};
\node[obs, above right = of x] (z) {$Z$};
\node[obs, below right = of z] (y) {$Y$};
\edge{x}{y};
\edge{z}{y};
@abikoushi
abikoushi / weibull_median.R
Last active September 13, 2025 01:41
相対的貧困率の例
#browseURL("https://en.wikipedia.org/wiki/Weibull_distribution")
median_weibull <- function(shape, scale=1){
ln2 = log(2)
scale * (ln2^(1/shape))
}
relativepoverty <- function(shape, scale=1){
pweibull(0.5*median_weibull(shape, scale), shape, scale)
}
@abikoushi
abikoushi / shimarisu_ch1_reproduce.R
Created September 8, 2025 09:39
佐藤俊哉『宇宙怪人しまりす統計よりも重要なことを学ぶ』第1話の最初の表の数値例 (ver.2)
########
## 佐藤俊哉『宇宙怪人しまりす統計よりも重要なことを学ぶ』(朝倉書店)
## Chapter 1: 再現性
########
library(dplyr)
library(ggplot2)
TPratio = function(true_ratio,
beta = 0.8,
alpha = 0.05){
study1 =cbind(true_ratio*c(beta,1-beta),
@abikoushi
abikoushi / shimarisu_ch1_repro.R
Created September 3, 2025 10:59
佐藤俊哉『宇宙怪人しまりす統計よりも重要なことを学ぶ』第1話の最初の表の数値例
########
## 佐藤俊哉『宇宙怪人しまりす統計よりも重要なことを学ぶ』(朝倉書店)
## Chapter 1: 再現性
########
library(dplyr)
library(ggplot2)
reproducibility = function(true_ratio,
beta = 0.8,
alpha = 0.05){
study1 =cbind(true_ratio*c(beta,1-beta),