Search code examples
rprobabilityprobability-density

R: how to get the conditional probability out of a cdplot for x?


Reproducible dataset:

a <- sample(45:3600, 500)
b <- sample(1:2, 500, replace=TRUE)
b <- factor(b, levels=c(1,2), labels=c("one", "two"))
CDP <- cdplot(b ~ a)

I would like to get the conditional probability (in the form of a number between 0 and 1) for any x (any "a" in this case).


Solution

  • If we assign the cdplot to an object (like to CDP in the question), this object becomes a list of 1 and the content is a function for the second level of b ("two", in the above case). The function can be accessed using $, and the conditional probability for any x can be obtained by putting x in the brackets, e.g.:

    CDP$two(1500)
    [1] 0.5388164