calculate area of overlapping density plot by ggplot using R

前端 未结 2 1357
暖寄归人
暖寄归人 2021-01-13 05:02

How can I get the area under overlapping density curves?

How can I solve the problem with R? (There is a solution for python here: Calculate overlap area of two func

2条回答
  •  再見小時候
    2021-01-13 05:22

    I was looking for a way to do this for empirical data, and had the problem of multiple intersections as mentioned by user5878028. After some digging I found a very simple solution, even for a total R noob like me:

    Install and load the libraries "overlapping" (which performs the calculation) and "lattice" (which displays the result):

    library(overlapping)
    library(lattice)
    

    Then define a variable "x" as a list that contains the two density distributions you want to compare. For this example, the two datasets "data1" and "data2" are both columns in a text file called "yourfile":

    x <- list(X1=yourfile$data1, X2=yourfile$data2)
    

    Then just tell it to display the output as a plot which will also display the estimated % overlap:

    out <- overlap(x, plot=TRUE)
    

    I hope this helps someone like it helped me! Here's an example overlap plot

    overlapping plot

提交回复
热议问题