Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exact kernel density value for any point in R [duplicate]

I was wondering if there is a R base way to obtain the exact kernel density at any point desired? As an example, how can I get the exact kernel density at the 3 following points -2, 0, +2 on X-Axis in a plot like below?

set.seed(2937107)
plot( density(rnorm(1e4)) )

enter image description here

like image 277
rnorouzian Avatar asked Oct 15 '25 18:10

rnorouzian


1 Answers

Use linear interpolation to find it.

d <- density(rnorm(10000))
approx(d$x, d$y, xout = c(-2, 0, 2))

The precision of interpolation can be higher if you set a larger n in density. By default n = 512 so interpolation is based on 512 points.

like image 174
Zheyuan Li Avatar answered Oct 18 '25 10:10

Zheyuan Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!