Skip to content

Instantly share code, notes, and snippets.

@Akiyah
Created December 10, 2014 10:31
Show Gist options
  • Save Akiyah/e8e0e3be7e98ae0eaabf to your computer and use it in GitHub Desktop.
Save Akiyah/e8e0e3be7e98ae0eaabf to your computer and use it in GitHub Desktop.
二項分布と正規分布とポアソン分布のずれをアニメーションにする
s <- 100
index_h <- 500^(((1:s)-1)/(s-1))
index <- as.integer(c(index_h, rev(index_h)))
for (i in 1:length(index)) {
png(sprintf("plot_%04d.png", i), width=500, height=500)
np <- index[i]
n <- 1000
p <- np/n
q <- 1 - p
x <- 0:n
xlim <- c(np-n*p*q -10,np+n*p*q +10)
ylim <- c(0,max(dbinom(x, size=n, prob=p))*1.5)
plot(dbinom(x, size=n, prob=p), xlab="", ylab="", xlim=xlim, ylim=ylim, type="l", col="blue")
par(new=T)
plot(dnorm(x, mean=n*p, sd=sqrt(n*p*q)), xlab="", ylab="", xlim=xlim, ylim=ylim, type="l", col="green")
par(new=T)
plot(dpois(x, lambda=np), xlab="", ylab="", xlim=xlim, ylim=ylim, type="l", col="red")
legend("topleft", legend=c("dbinom", "dnorm", "dpois"), col=c("blue", "green", "red"), lty=1)
text(np, 0, paste0("np=", np))
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment