# Calculating posterior probabilities in R?

I asked a question earlier on here.

Essentially, I am trying to evaluate a warning system that consists of a light bulb (of a specific color) being switched on, to indicate a predicted warning threat level. Srikant came up with a simple solution which involved calculating the posterior probabilities of the warning system, using bayes theorem.

I am now contemplating implementing this in R. However, I am not quite sure how to proceed, since I am an R newbie.

The data looks like this:

warning light, event level occured?
red, yes
none, yes
green, no

etc ...


I would like to write a simple R script that will help me to calculate:

a). the posterior probablity of an event occuring, for a given state of the light bulb b). a C.I to attach to the posterior probability obtained in (a) above

Since I am new to R, I would be grateful for the steps (and commands) required to do the above. My data will be in a simple csv file in the format described, so I can simply scan() it into R. How to proceed from there, is what I need help with..

This should actually be a fairly simple operation to do in R.

bulbs <- read.csv("bulbs.csv")
bulbs <- table(bulbs)


Now just sum along the rows or columns to get the marginal values.

sum(bulbs[,1]) # counts of no event occurring
sum(bulbs[1,]) # counts of green light


Now just take these frequencies and plug them to get any posterior you want.

bulbs[1,1]/sum(bulbs[,1]) # probability of green light given no event occurred


By the tone of your question, it may be handy to read a little bit about generating draws from a posterior. Two introductory springer books (full of R codes) comes to mind (in order of preference):

a) Bayesian computation with R. Springer, 2007. J. Albert.

b) Introducing Monte Carlo Methods with R. Springer 2010. C. P. Robert, G. Casella

You will find more detailed explanations and ways to circumvent important captcha's in these that you could possibly get from a 15 lines answer on here.

• Thanks Kwak. I have ordered the first book from Amazon. I could still do with a few lines to get me started though ... – morpheous Oct 5 '10 at 16:36