Creating an ordering vector from the result of rank()

tds151
2013-02-10
2013-02-25
  • tds151
    tds151
    2013-02-10

    I have a simplex, z, of dimension "D" under a dirichlet prior from which I compose ranks, "r". I want to convert the ranks to an ordering to permute the rows of a parameter matrix. The code snippet to implement the intent is:

    z ~ ddirch(alpha) ## D x 1. "alpha" = rep(1,D) input as data
    r <- rank(z)
    pos[r] <- ord_dim ## ordering of dimensions conforming to ranking, "r". ord_dim = 1:D

    For example, if D = 10 and rank(z) = c(4, 5, 8, 2, 10, 9, 6, 7, 1, 3)
    then pos = c(9, 4, 10, 1, 2, 7, 8, 3, 6, 5)

    I receive the compile step error message "Missing values in subset expression of pos". I've tried placing 'pos' in a loop, but receive the same error. This comes from a large model, which otherwise runs without this piece. I can create a toy example if that would be helpful, but I believe i've hit something fundamental to how JAGS works such that someone may readily tell me that it is not possible to create an ordering vector from another integer vector derived from parameters or, alternatively, point me to how this may be done.

     
  • Martyn Plummer
    Martyn Plummer
    2013-02-25

    Any indices on the left hand side of a relation must be fixed. The relation pos[r] <- ord_dim is not allwed because r is calculated from the unobserved stochastic node z, and so is not fixed. This is what the ``missing values'' error message is referring to.

    What you want is the permutation that sorts z into ascending order, as returned by the R function order(). This is the inverse of he permutation retuned by rank(). You can't do the inversion by hand due to the above syntactic restriction.

    I have just added an order() function to the JAGS source and it will be in the next release. In the mean time, I don't think you can do this.