5
$\begingroup$

If I have a parameter with a prior $X\sim N(\mu,\sigma)$ and three data points, $x_1,x_2,x_3$, how can I calculate the posterior in R or BUGS?

Here is an attempt at doing this in BUGS, that I have gotten stuck on:

data:
list(x=1.4, 2.1, 1.1, mu = 10, tau=1/10)

model {
  for (i in 1:3) { 
      x[i] ~ dnorm(mu, tau)
  }
}

Any help appreciated

$\endgroup$
1

1 Answer 1

3
$\begingroup$

(Disclaimer: I am by no means very experienced in Bayesian stats or using BUGS!)

What parameters would you like to estimate? What does "mu = 10, tau=1/10" mean? Are these numbers considered to be priors?

Let us assume your interested in estimating $\mu$ and $\sigma^2$. (Keep in mind that Win/OpenBUGS use the precision, i.e prec = 1/$\sigma^2$). So, your BUGS model could look like this:

model {
  for (i in 1:3) { 
      x[i] ~ dnorm(mu, prec)
  }

## priors (will have strong impact on the parameter estimation)  
prec ~ dgamma(0.1, 0.001)
mu ~ dnorm(2.0, 0.0001)
var <- 1/prec
}

## data vector (BUGS follows the S/R notation, i.e. use the c() function)
list(x=c(1.4, 2.1, 1.1))

I am using OpenBUGS (10,000 iterations, burn-in: 5000; starting values were generated by OpenBUGS) and here are my results:

        mean    sd      MC_error    val2.5pc    median  val97.5pc   start   sample
mu      1.544   0.6666      0.01039     0.3894  1.535       2.728   5000    5002
prec    4.155   3.942       0.06772     0.1263  3.024       14.38   5000    5002
var     1.31    5.893       0.1191      0.06952 0.331       7.917   5000    5002

You might realize that the variance estimator has been heavily affected by the choice of the prior.

As @whuber already mentioned, I strongly recommend that you check out the many examples that come with any of the BUGS packages. You also might be interested in "Bayesian Methods for Ecology" or "Bayesian Modeling Using WinBUGS: An introduction".

$\endgroup$
5
  • $\begingroup$ thanks for the explanation. If I understand correctly, what I was missing is that my my priors needed hyperpriors? $\endgroup$
    – Abe
    Commented Jan 26, 2011 at 19:30
  • $\begingroup$ @Abe Your BUGS code was syntactically wrong and you didn't define any priors. $\endgroup$ Commented Jan 26, 2011 at 19:38
  • $\begingroup$ I see that the syntax was wrong, but is it not possible to have a single $N(\mu,\sigma)$ prior? or do $\mu$ and $\sigma$ need priors? $\endgroup$
    – Abe
    Commented Jan 26, 2011 at 19:59
  • $\begingroup$ @Abe What do you mean by "single $N(\mu, \sigma^2)$ prior"? Assuming that you want to estimate both parameters, $\mu$ and $\sigma^2$, one has to assign appropriate prior distributions, e.g. dnorm and dgamma. $\endgroup$ Commented Jan 26, 2011 at 20:08
  • $\begingroup$ sorry. I think I lost this in the translation between paper and code. $\endgroup$
    – Abe
    Commented Jan 26, 2011 at 22:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.