[R] Creating q and p functions from a self-defined distribut

S Ellison S.Ellison at lgc.co.uk
Thu Mar 15 17:26:48 CET 2007



>>> ted.harding at nessie.mcc.ac.uk 15/03/2007 13:26:52 >>>
On 15-Mar-07 12:09:42, Eli Gurarie wrote:
> Hello all,
> 
> I am fishing for some suggestions on efficient ways to make qdist and 
> pdist type functions from an arbitrary distribution whose probability 
> density function I've defined myself.

Ted Harding accurately said there is unliekly to be an efficient general answer.

However, if you want a dreadfully inefficient but very general answer, could you use a numerical integral to calculate the cumulative probabilities, and a root-finder to find quantiles from the integral function?  (strictly, the quantile would be the root of {cumulative integral} - p where p is where you want the quantile.

uniroot is the general-purpose root-finder in R; it isn't ideal for this purpose because it won't like an interval of +-In, which you will certainly need for general quantiles. But you should be able to make it work with minimal tinkering if you use it on logit(p) and apply it to the interval [0,1]Converting back from logit after uniroot should then give you your quantile. You may also want to give it a smaller tolerance; the default looks like its geared to a sum-of-squares minimiser, and may be a bit over-generous for your purpose.. 

Steve Ellison.

*******************************************************************
This email and any attachments are confidential. Any use, co...{{dropped}}



More information about the R-help mailing list