# Functions to graph density functions. gexp = function(endpoints,rate) { require(lattice) if( !(is.vector(endpoints)) || (length(endpoints) != 2) ) stop("endpoints must be a vector of length 2") if(endpoints[1] > endpoints[2]) endpoints = rev(endpoints) x = seq(endpoints[1],endpoints[2],length=101) y = dexp(x,rate=rate) r = max(y)*0.05 gplot = xyplot(y ~ x, panel=function(x,y,...) { panel.mathdensity(dmath=dexp,args=list(rate=rate),n=501,lwd=2,col="red") panel.abline(h=0) }, xlab="x", ylab="density", ylim=c(-r,max(y)+r)) print(gplot) return(invisible(gplot)) } ggamma = function(endpoints,shape,rate) { require(lattice) if( !(is.vector(endpoints)) || (length(endpoints) != 2) ) stop("endpoints must be a vector of length 2") if(endpoints[1] > endpoints[2]) endpoints = rev(endpoints) x = seq(endpoints[1],endpoints[2],length=101) y = dgamma(x,shape=shape,rate=rate) r = max(y)*0.05 gplot = xyplot(y ~ x, panel=function(x,y,...) { panel.mathdensity(dmath=dgamma,args=list(shape=shape,rate=rate),n=501,lwd=2,col="red") panel.abline(h=0) }, xlab="x", ylab="density", ylim=c(-r,max(y)+r)) print(gplot) return(invisible(gplot)) } gbeta = function(endpoints,shape1,shape2) { require(lattice) if( !(is.vector(endpoints)) || (length(endpoints) != 2) ) stop("endpoints must be a vector of length 2") if(endpoints[1] > endpoints[2]) endpoints = rev(endpoints) if(endpoints[1]==0 && shape1<1) stop("the Beta density is infinite at x=0 when alpha (shape1) < 1") if(endpoints[2]==0 && shape2<1) stop("the Beta density is infinite at x=1 when beta (shape2) < 1") x = seq(endpoints[1],endpoints[2],length=101) y = dbeta(x,shape1=shape1,shape2=shape2) r = max(y)*0.05 gplot = xyplot(y ~ x, panel=function(x,y,...) { panel.mathdensity(dmath=dbeta,args=list(shape1=shape1,shape2=shape2),n=501,lwd=2,col="red") panel.abline(h=0) }, xlab="x", ylab="density", ylim=c(-r,max(y)+r)) print(gplot) return(invisible(gplot)) }