BetaDistributions.R

From Organic Design wiki

Code snipits and programs written in R, S or S-PLUS postscript("test.ps", horizontal=T)

  1. symmetric

par(mfrow=c(3,2)) a <- 2^rnorm(10000,0,1) hist(log2(a), breaks=50) hist(a, breaks=50) x <- par()$xaxp[2]*0.3 y <- par()$yaxp[2]*0.9

text(x,y, labels=paste("mean(x)=",round(mean(a),3)), pos=4) text(x,y*0.8, labels=paste("median(x)=",round(median(a),3)), pos=4) text(x,y*0.6, labels=paste("geometric mean(x)=",round(exp(mean(log(a))),3)), pos=4)

  1. right skew

a <- rgamma(10000,2.5) a <- ((a-mean(a))/var(a)) a <- 2^a hist(log2(a), breaks=50) hist(a, breaks=50) x <- par()$xaxp[2]*0.3 y <- par()$yaxp[2]*0.9

text(x,y, labels=paste("mean(x)=",round(mean(a),3)), pos=4) text(x,y*0.8, labels=paste("median(x)=",round(median(a),3)),pos=4) text(x,y*0.6, labels=paste("geometric mean(x)=",round(exp(mean(log(a))),3)), pos=4)

  1. left skew

a <- rbeta(10000,10,2) a <- ((a-mean(a))/sqrt(var(a))) a <- 2^a hist(log2(a), breaks=50) hist(a, breaks=50) x <- par()$xaxp[2]*0.5 y <- par()$yaxp[2]*0.9

text(x,y, labels=paste("mean(x)=",round(mean(a),3)), pos=4) text(x,y*0.8, labels=paste("median(x)=",round(median(a),3)), pos=4) text(x,y*0.6, labels=paste("geometric mean(x)=",round(exp(mean(log(a))),3)), pos=4)

dev.off()

rm(a,x,y)