在 R 中绘制数据集的概率密度/质量函数

发布于 2024-11-28 02:36:46 字数 416 浏览 1 评论 0原文

我有一个数据集,我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-12-05 02:36:46

对我来说,你的数据看起来远非离散。在处理连续数据时期望概率是完全错误的。 密度() 为您提供一个经验密度函数,它近似于真实的密度函数。为了证明它是正确的密度,我们计算曲线下的面积:

energy <- rnorm(100)
dens <- density(energy)
sum(dens$y)*diff(dens$x[1:2])
[1] 1.000952

给定一些舍入误差。曲线下的面积总和为 1,因此 密度() 的结果满足 PDF 的要求。

使用 histprobability=TRUE 选项或函数 密度() (或两者),

例如:

hist(energy,probability=TRUE)
lines(density(energy),col="red")

给出

在此处输入图像描述

如果您确实需要离散变量的概率,请使用:

 x <- sample(letters[1:4],1000,replace=TRUE)
 prop.table(table(x))
x
    a     b     c     d 
0.244 0.262 0.275 0.219 

编辑:说明为什么天真的 count(x)/sum( count(x)) 不是 解决方案。事实上,曲线下面积并不是因为各箱的值之和为 1。为此,您必须乘以“垃圾箱”的宽度。采用正态分布,我们可以使用 dnorm() 计算 PDF。以下代码构造正态分布,计算密度,并与简单的解决方案进行比较:

x <- sort(rnorm(100,0,0.5))
h <- hist(x,plot=FALSE)
dens1 <-  h$counts/sum(h$counts)
dens2 <- dnorm(x,0,0.5)

hist(x,probability=TRUE,breaks="fd",ylim=c(0,1))
lines(h$mids,dens1,col="red")
lines(x,dens2,col="darkgreen")

给出:

在此处输入图像描述


累积分布函数

如果@Iterator是正确的,那么从密度构造累积分布函数是相当容易的。 CDF 是 PDF 的积分。对于离散值,这只是概率的总和。对于连续值,我们可以利用经验密度估计的区间相等的事实,并计算:

cdf <- cumsum(dens$y * diff(dens$x[1:2]))
cdf <- cdf / max(cdf) # to correct for the rounding errors
plot(dens$x,cdf,type="l")

给出:

在此输入图像描述

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 :

energy <- rnorm(100)
dens <- density(energy)
sum(dens$y)*diff(dens$x[1:2])
[1] 1.000952

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 of hist or the function density() (or both)

eg :

hist(energy,probability=TRUE)
lines(density(energy),col="red")

gives

enter image description here

If you really need a probability for a discrete variable, you use:

 x <- sample(letters[1:4],1000,replace=TRUE)
 prop.table(table(x))
x
    a     b     c     d 
0.244 0.262 0.275 0.219 

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 using dnorm(). Following code constructs a normal distribution, calculates the density, and compares with the naive solution :

x <- sort(rnorm(100,0,0.5))
h <- hist(x,plot=FALSE)
dens1 <-  h$counts/sum(h$counts)
dens2 <- dnorm(x,0,0.5)

hist(x,probability=TRUE,breaks="fd",ylim=c(0,1))
lines(h$mids,dens1,col="red")
lines(x,dens2,col="darkgreen")

Gives :

enter image description here


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 :

cdf <- cumsum(dens$y * diff(dens$x[1:2]))
cdf <- cdf / max(cdf) # to correct for the rounding errors
plot(dens$x,cdf,type="l")

Gives :

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文