[R] The Percentile of a User-Defined pdf

Nissim Kaufmann tk725472 at albany.edu
Mon Jan 17 19:58:31 CET 2011

I got it to work:
# To get a percentile of a single-variable function:
# Step 1: Integrate over the domain to ge the normalization constant:
Z<-integrate(function(x) sqrt(1+x^-1), 1,2)$value
# Step 2: Find the .975 percentile
x975<-uniroot(function(t) integrate(function(x) sqrt(1+x^-1), 1,
               lower=1, upper=2, tol=5e-4 )$root

# To get a percentile of a marginal of a bivariate function of (x,y):
# Define and compute the marginal x distribution:
fx<-function(x) {sapply(x, function(x)
                                integrate( function(y)  sqrt(sin(x)+1/y),
# Then proceed as above in the singe-variable case.

Thank you Dieter and David.

Nissim Kaufmann
Dept. of Mathematics and Statistics
University at Albany

In reply to

More information about the R-help mailing list