I am currently building density objects for an ecology project I am working on. The density
function in R
works quite nicely for fitting density functions to my data.
An example of how I am using the density
function is shown below:
dens.iris <- density(iris$Sepal.Length, bw = "bcv")
This works very well. However, the predict
function does not seem to work with density
objects. Does anyone know a way to extract the density value for a specific point (e.g. in the iris
dataset, extract a Sepal.Length of 6.432)? It is important that I use a biased cross validation
technique for this.
You could use the approxfun
function to linearly interpolate points between those given by the density result. Thus you could use
diris <- with(dens.iris, approxfun(x, y, rule=1))
diris(6.432)
# [1] 0.349344
also
curve(diris(x), from=4.0, to=7.9)
Of course you have to remember that values of a density curve are not the same thing as probabilities. As with any continuous distribution, the probability that the sepal length is exactly 6.432 is 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With