问题
I am trying to find the best PDF of a continuous data that has unknown distribution, using the "density" function in R. Now, given a new data point, I want to find the probability density of this data point based on the kernel density estimator that I have from the "density" function result. How can I do that?
回答1:
If your new point will be within the range of values produced by density
, it's fairly easy to do -- I'd suggest using approx
(or approxfun
if you need it as a function) to handle the interpolation between the grid-values.
Here's an example:
set.seed(2937107)
x <- rnorm(10,30,3)
dx <- density(x)
xnew <- 32.137
approx(dx$x,dx$y,xout=xnew)
If we plot the density and the new point we can see it's doing what you need:

This will return NA
if the new value would need to be extrapolated. If you want to handle extrapolation, I'd suggest direct computation of the KDE for that point (using the bandwidth from the KDE you have).
回答2:
This is one year old, but nevertheless, here is a complete solution. Let's call
d <- density(xs)
and define h = d$bw
. Your KDE estimation is completely determined by
- the elements of
xs
, - the bandwidth
h
, - the type of kernel functions.
Given a new value t
, you can compute the corresponding y(t)
, using the following function, which assumes you have used Gaussian kernels for estimation.
myKDE <- function(t){
kernelValues <- rep(0,length(xs))
for(i in 1:length(xs)){
transformed = (t - xs[i]) / h
kernelValues[i] <- dnorm(transformed, mean = 0, sd = 1) / h
}
return(sum(kernelValues) / length(xs))
}
What myKDE
does is it computes y(t)
by the definition.
回答3:
See: docs
dnorm(data_point, its_mean, its_stdev)
来源:https://stackoverflow.com/questions/28077500/find-the-probability-density-of-a-new-data-point-using-density-function-in-r