[R] Computing a CDF or many quantiles

Jerome Asselin jerome at hivnet.ubc.ca
Thu Sep 11 01:24:34 CEST 2003


On September 10, 2003 04:03 pm, Kevin S. Van Horn wrote:
>
> Your method looks like a naive reimplementation of integration, and
> won't work so well for distributions that have the great majority of the
> probability mass concentrated in a small fraction of the sample space.
>  I was hoping for something that would retain the adaptability of
> integrate().

Yesterday, I've suggested to use approxfun(). Did you consider my 
suggestion? Below is an example.

N <- 500
x <- rexp(N)
y <- rank(x)/(N+1)
empCDF <- approxfun(x,y)
xvals <- seq(0,4,.01)
plot(xvals,empCDF(xvals),type="l",
xlab="Quantile",ylab="Cumulative Distribution Function")
lines(xvals,pexp(xvals),lty=2)
legend(2,.4,c("Empirical CDF","Exact CDF"),lty=1:2)


It's possible to tune in some parameters in approxfun() to better match 
your personal preferences. Have a look at help(approxfun) for details.

HTH,
Jerome Asselin




More information about the R-help mailing list