# Simulated single value based on multiple chains in RJAGS

I am using RJAGS to simulate the posterior distribution of event that a certain candidate will win the presidential election. I need to find the actual percentage that one of the candidates will have. What I do have is the values of each iteration for each of my three chains for both the proportion of the first candidate winning and the second candidate winning. I don't know how to obtain a single result since I have all of these values for each iteration in the the chains. Should I calculate just an average of all those values for each candidate?

Thanks for the help

I don't exactly how you modeled your data, but considering that what you have is a database with intentions of vote for candidate A and B, and you modeled those intentions as beta distribution, such that:

$Y = 1$, if the person intends to vote for A
$Y = 0$, if the person intends to vote for B

With $Y_i \sim Bern(\theta)$ and the prior $\theta \sim Beta(\alpha, \beta)$ (or can be any other prior)

The you will get a chain of values of $\theta$, that is, you have a distribution of probabilities for $\theta$. I think that's what you meant you have right now in your chains, right?

Now, to get the proportion of $Y=1$ as you want, you just need to do a<-rbinom(length(chain),1,chain) for each chain, the you get a distribution of probabilities for your Y: you will get chains with only 1 and 0 and by doing sum(a)/length(a) (or mean(a)) you'll get the proportion for candidate A.

If you have more than 2 candidates, you can also do this, but your likelihood will be categorical, and you'll have to generate random values for a categorical distribution instead of using rbinom.

EDIT: the answer above was assuming you were using a cross section design, but now I realiaze your are doing a time series using the proportion of votes for the candidate in the preceding elections. So, as you said you are using a dynamic linear model, I supposed your likelihood is something like this:

$Y_t \sim Dist(f(\beta_0 + beta_1*Y_{t-1} + beta_2*Y_{t-2}), otherParameters)$

Where the $Dist$ is the distribution you are using to model it (e.g.: normal), $f(.)$ is the link function - you said you are using $f(x)=logit(x/(1-x))$, I think.

So, if you are using a model like that, and you want to predict a value for an year $Y_t$ that have $Y_{t-1}=0.8$ and $Y_{t-2}=0.6$, for example, you'll need to generate to a random value for $Dist$ for each iteration of your chain:

$Random\_from\_Dist(f(\beta_0 + beta_1*0.8 + beta_2*0.6), otherParameters)$

So, if your chain have 1000 iteractions (that is, 1000 values for $\beta_1$, $\beta_2$ and so on), you'll also have 1000 iteractions for your variable, that is, you have an approximation of the sample of probabilities of your variables, if you want the predicted average of votes this candidate will have, you can then do get mean of that sample.

You have 3 chains? You do the above for each of then and then you'll have 3 approximations for the sample of your variable and then you may want to combine those sample into one.

You model may be specified in another way, pretty different than that, but it's important that you get the ideia behind it so you can easily apply it to your model.

• Also I do get the actual probability that candidate A will win the state I do that by prob1wins <- (sum(as.numeric(Diff>0))/nsim), where Diff <- p1 -p2 – Eva Mar 22 '13 at 15:33
• I use Dynamic Linear model where I have theta=logit(p1/1-p1) (p1 is the proportion for candidate A). theta has a multivariate normal prior and thetahat ( the actual known estimates for theta) are also normally distributed. In order to check for convergence I have three chains from overly dispersed starting points. At the end after I have simulated estimates for theta I convert back to get the wanted values of p1. Then if I want to see what are the values for p1, I get estimates from each iteration for all chains. Which chains estimates do I use? – Eva Mar 22 '13 at 15:43
• I don't really get why should I randomly generate from binomial? Thanks for your help – Eva Mar 22 '13 at 15:48
• I answered that because I thought you were using a cross-section (I just realized later the Time series tag lol), so I expanded my answer, as you said you are using a dynamic linear model, take a look. – random_user Mar 22 '13 at 21:54