# Developing a Bayesian model in R (rephrased)

I wanted to rephrase an earlier question to try and be less vague. I'm essentially looking for help understanding basic Bayesian inference.

Let's say I want to predict the transfer rate of a high school in its first year of existence. To do this I feel like the ideal set up would be looking at it from a Bayesian perspective.

Since I don't have any historical information from the school, I want to take the transfer rates from other schools in the district (labeled rates).

rates <- c(.08, .11, .02, .04, .03, .06, .05, .02, .06)


I believe that these rates would be my prior. The issue is I'm not sure what my next step would be. Without data from the school, I'm not sure where the likelihood and posterior distribution would come from.

Any guidance or help would be appreciated. I feel like I'm looking at the question wrong, which if I am, feel free to say.

• posterior distribution is what you will be calculating based on your prior and likelihood. your next step would be to identify the prior from the data you have of other schools (which distribution do they form). Next, with a likelihood in mind, go ahead and form the posterior. – Bach Jan 23 '17 at 22:23
• When a question is closed, the preferred action is that you edit it. This puts the question in the reopen queue, where it will be adjudicated. Asking the question again, when it may not be on-topic or answerable, is not preferred because asking many questions which are quickly closed can eventually lock you out of asking questions at all. But, really, please read the resources that I suggested in your last post. No one can build a model for you -- we don't know what kinds of questions you're interested in answering. – Sycorax Jan 23 '17 at 22:35

Apparently, whatever this "transfer rate of a high school" thing is, it seems to be a result of a binomial process and you are trying to find a model for each students "chance of a transfer", which I'll call $$\theta$$. Now the KISS approach would be to assume an identical $$\theta$$ in every school. A more sensible way would be to assume, that each school has a different $$\theta$$, taken out of a common distribution and the most sensible approach to assume, that each student has a different probability, which we cannot measure.

mef has written about the more sensible approach, named a hierarchical model and how you really need to learn more before you approach it. If this is just a self-study introductional example as to how Bayes works (please add self-study tag in that case), we can go the KISS way and assume, every student in the world has the same chance $$\theta$$.

Now it comes in handy, that there is a conjugate prior to the binomial distribution in the beta distribution. First find a prior distribution, i. e. what do you know about $$\theta$$ before you look at data. Let's say, you do know, that $$\theta$$ is not going to be 100% and else nothing. The least informative prior to state that would be a beta distribution with $$a=1$$ and $$b=2$$.

Updating: For each student without a transfer, increase $$a$$ by one and for each with a transfer, increase $$b$$ by one:

transfers <- c(8, 12, 2, 10, 19, 3, 42, 8, 19, 11)
totals <- c(111, 108, 79, 122, 130, 95, 201, 117, 122, 103)

nontransfers <- totals - transfers


Now through the wonder of conjugate priors, our posterior distribution of $$\theta$$ becomes this:

curve(dbeta(x, 1+sum(transfers), 2+sum(nontransfers)))
curve(dbeta(x, 1+sum(transfers), 2+sum(nontransfers)), xlim=c(0.05,0.15))


Thus, we have build an easy Bayes-Model to start with and explain the basics. The only downside is: It is far to easy and not acceptable. With the same beta-conjugate-magic we can easily see, that not all highschools have identical $$\theta$$. Just plot our $$\theta$$ distributions for all the highschools and you can see, that our model is far from acceptable:

curve(dbeta(x, 1+transfers[1], 2+nontransfers[1]), ylim=c(0,25), xlim=c(0,0.4))
for(i in 2:(length(transfers)))