Difference between two density plots

前端 未结 2 1042
暖寄归人
暖寄归人 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.

提交回复
热议问题