Difference between two density plots

前端 未结 2 1041
暖寄归人
暖寄归人 2021-01-15 12:04

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 r

相关标签:
2条回答
  • 2021-01-15 12:47

    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. Too bad I couldn't remember the right arguments.

    0 讨论(0)
  • 2021-01-15 12:50

    It looks like you need to spend a little time learning how to use R (or any other language, for that matter). Help files are your friend. From the output of ?density :

    Value [i.e. 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.

    x the n coordinates of the points where the density is estimated.

    y the estimated density values. 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) 
    
    0 讨论(0)
提交回复
热议问题