Home > Software design >  R: calculate area under a density curve until a cutoff value
R: calculate area under a density curve until a cutoff value

Time:09-29

I have the following problem.

The following code

library(tidyverse)

set.seed(1234)
df1 <- data.frame(x = rnorm(1000), group = "a")
df2 <- data.frame(x = rnorm(1000, mean = -1), group = "b")
df <- bind_rows(df1, df2)

df %>%
  ggplot(aes(x = x, fill = group))  
  geom_density(alpha = 0.5)  
  geom_vline(aes(xintercept = quantile(x[group=="a"], probs = 0.05, names = FALSE)), color = "firebrick")  
  theme_light()

produces this plot:

enter image description here

The red line is the 0.05 quantile of the curve A. So if I understand it correctly, the red line separates the A in 5% of the area is left and 95% of the area is right of the vertical line.

My question is: Given the 0.05 quantile of A, how much of B is left and right of the red vertical line? How can I calculate these numbers?

I hope it is clear what I am trying to achieve. Thank you for your help!

CodePudding user response:

Create an empirical cumulative distribution function:

q <- quantile(df[df$group=="a", "x"], probs = 0.05)
ecdf(df[df$group=="b", "x"])(q)
#[1] 0.255
  • Related