ypdfRange = function(integerMonth,simflow,obsflow) { library(sm) test = matrix(obsflow, ncol=12,byrow=T) imon = integerMonth data=test[,imon] #data=log(data) xx=data #bandd=hsj(xx) #sheather Jones approach for bandwidth bandd=hnorm(xx) #for the Normal bandwidth.. #bandd = bandd*2.214 #for EP kernel #band=bandd #go one bandwidth from the min and max of the data.. xlow=min(data)-bandd #xlow=max(0,xlow) xhigh=max(data)+bandd #create 50 points equally spaced between xlow and xhigh.. xeval=seq(xlow,xhigh,length=50) neval=length(xeval) zz=sm.density(data,eval.points=xeval,display="none") # When you estimate PDF in the log space you have to divide by # exp(of evaluation points) to get the pdf in the original space # See Lall et al. (1996) paper for the formula.. #pdf of the observed data.. #xpdfo=zz$estimate/exp(xeval) xpdfo=zz$estimate #now read the simulations.. xsim=matrix(scan(simflow), ncol=nsim, byrow=T) xpdf=matrix(0,ncol=nsim, nrow=neval) #get pdf of the simulations.. for(i in 1:nsim){ xx=xsim[,i] #xx=log(xx) #bandd=hsj(xx) #bandd=hnorm(xx) #bandd = bandd*2.214 #for EP kernel #band=bandd zz=sm.density(xx,eval.points=xeval,display="none") #xpdf[,i]=zz$estimate/exp(xeval) xpdf[,i]=zz$estimate } # the following will produce the boxplot of the PDFs along # with the PDF of the original data.. xs=1:neval yrange=c(yrange,(range(xpdf,xpdfo))) assign("yrange", yrange, env = .GlobalEnv) }