Compare Histograms

Enter two datasets containing the probabilities for two probabilitiy distributions. Chist draws the two overlaid histograms.


"CHist" <-
function(P0, P1, col=c("blue","red"),lwd=2)
{
# P0 and, if present, P1 are vectors of probabilities Pr(X=0:n)
m = max(P0)
m=max(c(m,P1))
n = length(P1)
if(length(P0) != n) stop("Probability vectors must be same length")
x = 0:(n+1)
Y0 = c(0,P0,0)
Y1 = c(0,P1,0)
yl = c(0,m*1.1)
plot(x-.5,Y0,xlim=c(-.5,n+.5),type="s",xlab="Outcome",
     ylab="Probability",ylim=yl,col=col[1],lwd=lwd)
X0 = x-.5
X1 = x + .5
segments(X0,0,X0,Y0,col=col[1],lwd=lwd)
segments(X1,0,X1,Y0,col=col[1],lwd=lwd)
abline(h=0)
lines(x-.5,Y1,type="s",lty=2,col=col[2],lwd=lwd)
segments(X0,0,X0,Y1,col=col[2],lty=2,lwd=lwd)
segments(X1,0,X1,Y1,col=col[2],lty=2,lwd=lwd)
}