Created
July 11, 2017 08:14
-
-
Save twolodzko/a6e75faf6b37235f44fadcaf52bb6448 to your computer and use it in GitHub Desktop.
Interactive visualizations for common distributions in RStudio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(manipulate) | |
# Beta | |
x <- seq(0, 1, by=0.01) | |
manipulate( | |
{ ci <- qbeta(c(0.05, .5, 0.95), alpha, beta) | |
plot(x, dbeta(x, alpha, beta), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 5.5), | |
main="Beta distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
alpha=slider(0.001, 10, step=0.001, initial=1), | |
beta=slider(0.001, 10, step=0.001, initial=1), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Gamma | |
x <- seq(0, 20, by=0.01) | |
manipulate( | |
{ ci <- qgamma(c(0.05, .5, 0.95), alpha, beta) | |
plot(x, dgamma(x, alpha, beta), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 0.5), | |
main="Gamma distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
alpha=slider(0.001, 10, step=0.001, initial=.001), | |
beta=slider(0.001, 2, step=0.001, initial=.001), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Poisson | |
x <- seq(0, 20, by=1) | |
manipulate( | |
{ ci <- qpois(c(0.05, .5, 0.95), lambda) | |
plot(x, dpois(x, lambda), | |
col="blue", lwd=2, type="o", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 0.27), | |
main="Poisson distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
lambda=slider(1, 20, step=1, initial=10), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Binomial | |
x <- seq(0, 20, by=1) | |
manipulate( | |
{ ci <- qbinom(c(0.05, .5, 0.95), n, p) | |
plot(x, dbinom(x, n, p), | |
col="blue", lwd=2, type="o", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 0.95), | |
main="Binomial distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
n=slider(1, 20, step=1, initial=10), | |
p=slider(0.001, 1, step=0.001, initial=0.5), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Exponential | |
x <- seq(0, 5, by=0.01) | |
manipulate( | |
{ ci <- qexp(c(0.05, .5, 0.95), rate) | |
plot(x, dexp(x, rate), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 2), | |
main="Exponential distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
rate=slider(0.001, 5, step=0.001, initial=2.5), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Normal | |
x <- seq(-6, 6, by=0.01) | |
manipulate( | |
{ sigma <- 1/sqrt(lambda) | |
ci <- qnorm(c(0.05, .5, 0.95), mu, sigma) | |
plot(x, dnorm(x, mu, sigma), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, .65), | |
main="Normal distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
legend("top", sprintf("lambda=%s, sigma=%s", round(lambda, 2), round(sigma, 2)), cex=0.8, box.lwd=0) | |
}, | |
mu=slider(-2, 2, step=0.01, initial=0), | |
lambda=slider(0.001, 2, step=0.001, initial=1), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Log-Normal | |
x <- seq(0, 3, by=0.01) | |
manipulate( | |
{ sigma <- 1/sqrt(lambda) | |
ci <- qlnorm(c(0.05, .5, 0.95), mu, sigma) | |
plot(x, dlnorm(x, mu, sigma), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 2), | |
main="Log-Normal distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
legend("top", sprintf("lambda=%s, sigma=%s", round(lambda, 2), round(sigma, 2)), cex=0.8, box.lwd=0) | |
}, | |
mu=slider(0, 2, step=0.001, initial=0), | |
lambda=slider(0.001, 20, step=0.001, initial=0.001), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Student t | |
x <- seq(-6, 6, by=0.01) | |
manipulate( | |
{ ci <- qt(c(0.05, .5, 0.95), df, delta) | |
plot(x, dt(x, df, delta), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 0.4), | |
main="Student t distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
df=slider(0.001, 10, step=0.001, initial=5), | |
delta=slider(-4, 4, step=0.001, initial=0), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Cauchy | |
x <- seq(-4, 4, by=0.01) | |
manipulate( | |
{ ci <- qcauchy(c(0.05, .5, 0.95), location, scale) | |
plot(x, dcauchy(x, location, scale), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 2), | |
main="Cauchy distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
location=slider(-2, 2, step=0.001, initial=0), | |
scale=slider(0.001, 4, step=0.001, initial=0.2), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Logistic | |
x <- seq(-6, 20, by=0.01) | |
manipulate( | |
{ ci <- qlogis(c(0.05, .5, 0.95), mu, s) | |
plot(x, dlogis(x, mu, s), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 0.5), | |
main="Logistic distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
mu=slider(-6, 6, step=0.001, initial=0), | |
s=slider(0.5, 10, step=0.001, initial=1), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Geometric | |
x <- seq(0, 10, by=1) | |
manipulate( | |
{ ci <- qgeom(c(0.05, .5, 0.95), p) | |
plot(x, dgeom(x, p), | |
col="blue", lwd=2, type="o", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 0.5), | |
main="Geometric distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
p=slider(0.001, 1, step=0.001, initial=0.5), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Hypergeometric | |
x <- seq(0, 10, by=1) | |
manipulate( | |
{ m <- min(N, m) | |
n <- N-m | |
k <- min(N, k) | |
ci <- qhyper(c(0.05, .5, 0.95), n, m, k) | |
plot(x, dhyper(x, n, m, k), | |
col="blue", lwd=2, type="o", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 1), | |
main="Hypergeometric distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
m=slider(0, 10, step=1, initial=2), | |
N=slider(0, 10, step=1, initial=7), | |
k=slider(0, 10, step=1, initial=5), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
# Weibull | |
x <- seq(0, 2.5, by=0.01) | |
manipulate( | |
{ ci <- qweibull(c(0.05, .5, 0.95), lambda, k) | |
plot(x, dweibull(x, lambda, k), | |
col="blue", lwd=2, type="l", las=1, bty="n", | |
ylab="density", xlab="", ylim=c(0, 2.2), | |
main="Weibull distribution") | |
box() | |
mtext(paste(c("95% CI:", round(ci, 2)), | |
collapse=" "), cex=0.8, side=3) | |
if (printci) abline(v=ci, lty=c(3,2,3)) | |
}, | |
lambda=slider(0, 5, step=0.001, initial=2.5), | |
k=slider(0, 2.5, step=0.001, initial=1), | |
printci=checkbox(TRUE, "Show 95% CI")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment