简体   繁体   English

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

[英]Plotting Probability Density / Mass Function of Dataset in R

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转换为能量矢量,我们考虑的数据本质上是离散的,所以我没有特殊的类型来分配数据。

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. density()为您提供经验密度函数,该函数近似于真实密度函数。 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. 曲线下面积总和为1,因此density()的结果满足PDF的要求。

Use the probability=TRUE option of hist or the function density() (or both) 使用histprobability=TRUE选项或函数density() (或两者)

eg : 例如:

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

gives

在此输入图像描述

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. 编辑:插图为什么天真count(x)/sum(count(x))不是解决方案。 Indeed, it's not because the values of the bins sum to one, that the area under the curve does. 实际上,这并不是因为箱子的值总和为1,即曲线下面积的值。 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() . 采用正态分布,我们可以使用dnorm()计算PDF。 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 : 给:

在此输入图像描述


The cumulative distribution function 累积分布函数

In case @Iterator was right, it's rather easy to construct the cumulative distribution function from the density. 如果@Iterator是正确的,那么从密度构造累积分布函数相当容易。 The CDF is the integral of the PDF. CDF是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 : 给:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM