Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting probability from density object

Tags:

r

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.

like image 943
Mike.Gahan Avatar asked Mar 19 '23 06:03

Mike.Gahan


1 Answers

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)

enter image description here

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.

like image 84
MrFlick Avatar answered Apr 02 '23 00:04

MrFlick