在 R 中绘制数据集的概率密度/质量函数
我有一个数据集,我想用 R 中的概率密度函数或概率质量函数来分析这些数据。我使用了密度函数,但它没有给我概率。
我的数据是这样的:
"step","Time","energy"
1, 22469 , 392.96E-03
2, 22547 , 394.82E-03
3, 22828,400.72E-03
4, 21765, 383.51E-03
5, 21516, 379.85E-03
6, 21453, 379.89E-03
7, 22156, 387.47E-03
8, 21844, 384.09E-03
9 , 21250, 376.14E-03
10, 21703, 380.83E-03
我想要获取energy
向量的PDF/PMF;我们考虑的数据本质上是离散的,所以我没有任何特殊的数据分布类型。
I have a dataset and I want to analyse these data with a probability density function or a probability mass function in R. I used a density function but it didn't gave me the probability.
My data are like this:
"step","Time","energy"
1, 22469 , 392.96E-03
2, 22547 , 394.82E-03
3, 22828,400.72E-03
4, 21765, 383.51E-03
5, 21516, 379.85E-03
6, 21453, 379.89E-03
7, 22156, 387.47E-03
8, 21844, 384.09E-03
9 , 21250, 376.14E-03
10, 21703, 380.83E-03
I want to the get PDF/PMF for the energy
vector ; the data we take into account are discrete in nature so I don't have any special type for the distribution of the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,你的数据看起来远非离散。在处理连续数据时期望概率是完全错误的。
密度()
为您提供一个经验密度函数,它近似于真实的密度函数。为了证明它是正确的密度,我们计算曲线下的面积:给定一些舍入误差。曲线下的面积总和为 1,因此
密度()
的结果满足 PDF 的要求。使用
hist
的probability=TRUE
选项或函数密度()
(或两者),例如:
给出
如果您确实需要离散变量的概率,请使用:
编辑:说明为什么天真的
count(x)/sum( count(x))
不是 解决方案。事实上,曲线下面积并不是因为各箱的值之和为 1。为此,您必须乘以“垃圾箱”的宽度。采用正态分布,我们可以使用 dnorm() 计算 PDF。以下代码构造正态分布,计算密度,并与简单的解决方案进行比较:给出:
累积分布函数
如果@Iterator是正确的,那么从密度构造累积分布函数是相当容易的。 CDF 是 PDF 的积分。对于离散值,这只是概率的总和。对于连续值,我们可以利用经验密度估计的区间相等的事实,并计算:
给出:
Your data looks far from discrete to me. Expecting a probability when working with continuous data is plain wrong.
density()
gives you an empirical density function, which approximates the true density function. To prove it is a correct density, we calculate the area under the curve :Given some rounding error. the area under the curve sums up to one, and hence the outcome of
density()
fulfills the requirements of a PDF.Use the
probability=TRUE
option ofhist
or the functiondensity()
(or both)eg :
gives
If you really need a probability for a discrete variable, you use:
Edit : illustration why the naive
count(x)/sum(count(x))
is not a solution. Indeed, it's not because the values of the bins sum to one, that the area under the curve does. For that, you have to multiply with the width of the 'bins'. Take the normal distribution, for which we can calculate the PDF usingdnorm()
. Following code constructs a normal distribution, calculates the density, and compares with the naive solution :Gives :
The cumulative distribution function
In case @Iterator was right, it's rather easy to construct the cumulative distribution function from the density. The CDF is the integral of the PDF. In the case of the discrete values, that simply the sum of the probabilities. For the continuous values, we can use the fact that the intervals for the estimation of the empirical density are equal, and calculate :
Gives :