Help! How to do sum over subset of factors (new to JAGS also)

Help
VelocideX
2013-08-23
2013-08-24
  • VelocideX
    VelocideX
    2013-08-23

    Hello all,

    I am starting to learn JAGS because I have a problem that was too complex for me to reliably implement via my own code.

    I have described the problem here in pseudocode, pending my learning the proper JAGS format.

    My problem is as follows. Suppose I have N observations, y[1:N], which are assumed to be normally distributed.

    My prediction is based on a series of other independent variables (regressors), x[1:M] where M is somewhat larger than N.

    For each y[i], only a small subset of the x values relate to it. This number is not constant - y[1] may only relate to x[1], for instance, but y[2] may relate to x[2], x[3] and x[4], and y[3] might relate to x[5] and x[6].

    The functional relationship between the y[i]s and x values is: y[i] = sum_j(a_j b_j c_j x[j]), where the sum is taken over the subset of the x values which relate to y[i].

    For instance, the relationships above might be:
    y[1] = a[1]b[1]c[1]x[1]
    y[2] = a[1]b[2]c[2]x[2] + a[2]b[1]c[2]x[3] + a[2]b[1]c[1]x[3]
    y[3] = a[2]b[2]c[2]x[4] + a[1]b[2]c[1]x[5]
    etc etc

    Is it possible to easily define such a model in JAGS? What I really need is a "sum if" construction, where I can loop over a large number of x values and make them contribute to y[i] only if they meet certain conditions (which set out the relationship between the y values and x values similar to what I have described above). However I don't think this is possible.

    Normally I would have a matrix indexing the relevant variables.

    Eg a matrix with columns
    Y a b c
    The row for x[2] would read:
    2 1 2 2
    The row for x[3] would read:
    3 2 2 2

    I don't see how to implement the sum though.

    From my reading I suspect that this will require nested indexing.

    Many thanks!

     
    Last edit: VelocideX 2013-08-24
  • VelocideX
    VelocideX
    2013-08-24

    I'm currently thinking that doing this via an inner product may be the way to go? I.e.

    Step 1) Create a matrix A of dimension [1..n][1..m], with entries either 0 or 1, such that A[i][j]=1 means that x[j] contributes to y[i]
    Step 2) Calculate xpred[j] = a_i b_i c_i x[j]
    Step 3) Calculate vec[i] = A[i,1..m]
    Step 4) Calculate innerprod(xpred, vec)