简体   繁体   English

两个密度图之间的差异

[英]Difference between two density plots

Is there a simple way to plot the difference between two probability density functions? 是否有一种简单的方法可以绘制两个概率密度函数之间的差异?

I can plot the pdfs of my data sets (both are one-dimensional vectors with roughly 11000 values) on the same plot together to get an idea of the overlap/difference but it would be more useful to me if I could see a plot of the difference. 我可以将同一数据集上的数据集的pdfs(均是一维向量,均具有大约11000个值)绘制在一起,以了解重叠/差异,但是如果我能看到一个的散点图,对我来说将更有用。区别。

something along the lines of the following (though this obviously doesn't work): 遵循以下内容(尽管显然不起作用):

> plot(density(data1)-density(data2))

I'm relatively new to R and have been unable to find what I'm looking for on any of the forums. 我是R的新手,在任何论坛上都找不到我想要的东西。

Thanks in advance 提前致谢

This should work: 这应该工作:

plot(x =density(data1, from= range(c(data1, data2))[1], 
                       to=range(c(data1, data2))[2] )$x, 
  y=  density(data1, from= range(c(data1, data2))[1], 
                     to=range(c(data1, data2))[2] )$y-
       density(data2,  from= range(c(data1, data2))[1], 
                      to=range(c(data1, data2))[2] )$y )

The trick is to make sure the densities have the same limits. 诀窍是确保密度具有相同的极限。 Then you can plot their differences at the same locations.My understanding of the need for the identical limits comes from having made the error of not taking that step in answering a similar question on Rhelp several years ago . 然后,您可以在相同的位置绘制它们的差异。我对相同限制的必要性的理解来自几年前在Rhelp上回答类似问题的错误。 Too bad I couldn't remember the right arguments. 太糟糕了,我不记得正确的论点。

It looks like you need to spend a little time learning how to use R (or any other language, for that matter). 看来您需要花费一些时间来学习如何使用R(或其他任何语言)。 Help files are your friend. 帮助文件是您的朋友。 From the output of ?density : ?density输出:

Value [ie the data returned by the function] 值[即函数返回的数据]

If give.Rkern is true, the number R(K), otherwise an object with class "density" whose underlying structure is a list containing the following components. 如果give.Rkern为true,则为R(K),否则为“密度”类的对象,其基础结构是包含以下组件的列表。

x the n coordinates of the points where the density is estimated. x估计密度的点的n坐标。

y the estimated density values. y估计的密度值。 These will be non-negative, but can be zero [remainder of "value" deleted for brevity] 这些值非负数,但可以为零[为简洁起见,删除了“值”的其余部分]

So, do: 因此,请执行以下操作:

foo<- density(data1) 
bar<- density(data2)
plot(foo$y-bar$y) 

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

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