Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created October 11, 2025 10:09
Show Gist options
  • Select an option

  • Save abikoushi/e9ea449d2d2ab09351836705cf7953bf to your computer and use it in GitHub Desktop.

Select an option

Save abikoushi/e9ea449d2d2ab09351836705cf7953bf to your computer and use it in GitHub Desktop.
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)
}
Beta_p <- function(p,x,r){
pbeta(p, shape1 = r, shape2 = x+1, lower.tail = TRUE)
}
params = list(r = 10, x = 6)
pp = ggplot()+
stat_function(fun = B_p, args = params, aes(linetype="binomial", colour="binomial"))+
stat_function(fun = Beta_p, args = params, aes(linetype="beta", colour="beta"))+
stat_function(fun = NB_p, args = params, aes(linetype="negative binomial", colour="negative binomial"))+
stat_function(geom = "point", fun = B_p, args = params, aes(shape="binomial", colour="binomial"), n=7)+
stat_function(geom = "point", fun = Beta_p, args = params, aes(shape="beta", colour="beta"), n=8)+
stat_function(geom = "point", fun = NB_p, args = params, aes(shape="negative binomial", colour="negative binomial"), n=9)+
xlim(c(0.001,0.999))+
labs(x="p", y="probability", colour="function", linetype="function",shape="function")+
theme_bw()
print(pp)
ggsave(filename = "betacdf3.png", plot = pp, width = 7, height = 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment