使用“密度"查找新数据点的概率密度R中的函数 [英] Find the probability density of a new data point using "density" function in R

查看:42
本文介绍了使用“密度"查找新数据点的概率密度R中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到分布未知的连续数据的最佳 PDF,使用 R 中的密度"函数.现在,给定一个新数据点,我想根据以下条件找到该数据点的概率密度我从密度"函数结果中得到的核密度估计量.我该怎么做?

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?

推荐答案

如果你的新点将在 density 产生的值范围内,这很容易做到 -- 我建议使用 approx (或 approxfun 如果你需要它作为一个函数)来处理网格值之间的插值.

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.

这是一个例子:

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:

如果需要外推新值,这将返回 NA.如果您想处理外推,我建议直接计算该点的 KDE(使用您拥有的 KDE 的带宽).

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).

这篇关于使用“密度"查找新数据点的概率密度R中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆