On Friday, I gave several examples of Bayes’ rule in class. I’ll do a slight generalization of the testing for a disease example to illustrate using a special R function bayes to do the calculations.
This special function can be read into R directly from my web site.
This function has three inputs prior, likelihood, and data.
1. The prior. Here there are two models — either we have the disease (D) or we don’t (not D) — and suppose that the probability of D is only 0.0001. prior is a vector of probabilities, where we give names to the two values:
> prior = c(D = .0001, not.D = 1 - .0001)
2. The likelihood. Here the data is the test result (pos or neg) where the test result is incorrect with probability 0.01. We’ll specify the likelihood as a matrix where each row of the matrix gives the probability of pos and neg for each model.
> like.D = c(pos=.99, neg=.01)
> like.not.D = c(pos=.01, neg=.99)
> likelihood = rbind(D = like.D, not.D = like.not.D)
D 0.99 0.01
not.D 0.01 0.99
3. data is a vector of data values. Suppose we get a positive test result (pos). Then
So we enter as inputs prior, likelhood, and data — the output is a matrix — the first row gives the prior probs and the second row gives the posterior probabilities.
> bayes(prior, likelihood, data)
prior 0.000100000 0.9999000
pos 0.009803922 0.9901961
Although the positive blood test result is “bad news”, it is still very unlikely that you have the disease.