简体   繁体   中英

Extracting probability from density object

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM