[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
Z
# Step 2: Find the .975 percentile
x975<-uniroot(function(t) integrate(function(x) sqrt(1+x^-1), 1,
t)$value/Z-.975,
               lower=1, upper=2, tol=5e-4 )$root
x975

# 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),
0,10)$value
                                     )
                         }
# 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
http://www.mail-archive.com/r-help@r-project.org/msg121420.html



More information about the R-help mailing list