percentiles of a variable in jags

Jun Xu
  • Jun Xu

    Jun Xu - 2014-01-05


    Hope you all had a wonderful break. I am working on a Bayesian model that involves missing data. What I want to do in the BUGS/JAGS code is to model some missing data (an x variable), and then create two dummies for say 50 and 75 percentiles of that variable with imputed missing and put them into the main model. But I haven't been able to find ways to compute these two percentiles of the covariate with missing. Here are some model codes that I have been experimenting with:

    for( i in 1 : nData ) {
      y[i] ~ dbern(mu[i])
      logit(mu[i]) <- b0[Group[i]] + b1*x1[i] + b2*x2 + b3[countryR[i]]*x3dum[i]
    for( i in 1: nData) {
      x3[i] ~ dnorm([i], x3.tau)[i] <- g0[Group[i]] + g1*x1[i]
    for (i in 1: nData) {
      x3dum[i] <- ifelse([i] > mean(x3[]), 1, 0)

    Note that the x variable with missing is x3 and is modeled in the second loop. Then I create a dummy variable based off the mean of x3 in the third loop, which is fed back to the first loop. But here the dummy is created using mean as the threshold. What if I want to use some percentile of x3 as the benchmark for creating dummy? Is this the right way? Should I use the data step block? Any help would be appreciated.

    Jun Xu, PhD
    Department of Sociology
    Ball State University
    Muncie, IN 47306

    • Martyn Plummer

      Martyn Plummer - 2014-01-14

      This should give you the quantile Q of variable x3

         index.Q <- round(nData*Q)
         x3.sorted <- sort(x3)
         x3.Q <- x3.sorted[index.Q]

      So, for example Q <- 0.5 gives you the median.

  • Jun Xu

    Jun Xu - 2014-01-18

    Thanks a lot!


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks