Reader 123
Reader 123

Reputation: 357

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).

Upvotes: 0

Views: 56

Answers (1)

Reader 123
Reader 123

Reputation: 357

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

Upvotes: 1

Related Questions