I used R to find kernel density estimates of my dataset (for experiment I used 1000 samples generated from a known distribution in this step).
I used code density()
to find the kernel density estimate. Now I need to find the cdf of this pdf in order to find quantiles of the distribution. I used these codes for normal:
x=rnorm(1000,8,3)
pdf=density(x)
f=approxfun(pdf$x,pdf$y,yleft=0,yright=0)
FF2=function(x) integrate(f,-Inf,x)$v-0.25
uniroot(FF2,c(2,10))
By the last code I could find the first quantile of the distribution and this is what I want. But my question is about another distribution like gamma. These codes do not run for this distribution as here:
x1=rgamma(1000,shape=2,scale=1)
pdf1=density(x1)
f1=approxfun(pdf1$x,pdf1$y,yleft=0,yright=0)
FF1=function(x) integrate(f1,-Inf,x)$value-0.25
The result of the last code is not logical!