Bayes Theorem defined as:
P(A|B) = P(A)*P(B|A) / P(B)
where:
P(A|B): Given event B, the probability of event A has occurred.
P(B|A): Given that event A has occurred, the probability of occurrence B.
P(A): The probability of event A.
P(B): The probability of event B.
Example: Assume that there is a 30% chance of rainfall in a day. Assume that the probability that I walk outside is 50% and that the probability that I walk on a rainy day is 10%. What are the chances that it will be a rainy day given I walk outside?
This problem can be solved by using the Bayes Theorem. Wrapping this data around on the head is quite tough. Let’s change it into probability notation.
Let’s assume:
The probability of raining in a day P(R) = 30% = 0.30
The probability that I walk outside P(W) = 50% = 0.50
The Probability that I walk in a (or given) rainy day P(W|R) = 10% = 0.10
The probability of rain when (or given) I walk P(R|W) = ?

In Bayes Theorem [P(A|B) = P(A)*P(B|A) / P(B)] Notation:
P(R|W) = P(R)*P(W|R) / P(R)
P(R|W) = 0.30*0.10 / 0.50 = 0.06
P(R|W) = 0.06
Lets write a function to compute Bayes Theorem in R:
BayesTheorem = function(P_EventA, P_EventB, P_EventBGivenEventA) {
P_EventAGivenEvenB = P_EventA * P_EventBGivenEventA / P_EventB
return(P_EventAGivenEvenB)
}
PRain = 0.30
PWalk = 0.50
PWalkGRain = 0.10
BayesTheorem(PRain, PWalk, PWalkGRain)
This should give a value of P(R|W) = 0.06. Try this code in R.
Note: This post is inspired by the Application of the Bayes Theorem in R by FINNSTAT.