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

时间:2021-09-29 22:47:39

I have data set and i want to analysis this data by probability density function or probability mass function in R ,i used density function but it didn't gave me a probability.

我有数据集,我想通过R中的概率密度函数或概率质量函数来分析这些数据,我使用密度函数,但它没有给我一个概率。

my data 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 get PDF/PMF to energy vector ,the data we take into account are discrete by nature so i don't have special type for distribution the data.

我想将PDF / PMF转换为能量矢量,我们考虑的数据本质上是离散的,所以我没有特殊的类型来分配数据。

1 个解决方案

#1


28  

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 :

您的数据远非离散。期望处理连续数据的可能性是完全错误的。 density()为您提供经验密度函数,该函数近似于真实密度函数。为了证明它是正确的密度,我们计算曲线下的面积:

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.

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

Use the probability=TRUE option of hist or the function density() (or both)

使用hist的probability = TRUE选项或函数密度()(或两者)

eg :

例如:

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

gives

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

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)/总和(计数(x))不是解决方案。实际上,并不是因为箱子的值总和为1,曲线下的面积就是这样。为此,您必须乘以'bins'的宽度。采用正态分布,我们可以使用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")

Gives :

给:

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


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 :

如果@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")

Gives :

给:

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

#1


28  

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 :

您的数据远非离散。期望处理连续数据的可能性是完全错误的。 density()为您提供经验密度函数,该函数近似于真实密度函数。为了证明它是正确的密度,我们计算曲线下的面积:

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.

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

Use the probability=TRUE option of hist or the function density() (or both)

使用hist的probability = TRUE选项或函数密度()(或两者)

eg :

例如:

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

gives

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

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)/总和(计数(x))不是解决方案。实际上,并不是因为箱子的值总和为1,曲线下的面积就是这样。为此,您必须乘以'bins'的宽度。采用正态分布,我们可以使用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")

Gives :

给:

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


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 :

如果@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")

Gives :

给:

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