I need to find as precisely as possible the peak of the kernel density estimation (modal value of the continuous random variable). I can find the approximate value:
If I understand your question, I think you are just wanting a finer discretisation of x
and y
. To do this, you can change the value of n
in the density
function (default is n=512
).
For example, compare
set.seed(1)
x = rlnorm(100)
d = density(x)
i = which.max(d$y)
d$y[i]; d$x[i]
0.4526; 0.722
with:
d = density(x, n=1e6)
i = which.max(d$y)
d$y[i]; d$x[i]
0.4525; 0.7228