0

This is my first time using JAGS and I ran into some errors when modeling my data.

Here is a brief description of my data: A total of n people (e.g., 2) each solved m problems (e.g., 6). All problems have 3 answers, each of a certain value V.

Here is the graphic model (some variable names are different: IG and y are the same as V and answer, respectively; x is not included in my model).

The probability that Person i chooses each answer for Problem j follows the "softmax decision rule": exp(V[j,]/tau[i])/sum(exp(V[j,]/tau[i])). Here, tau is the noise in decision with a Gamma distribution: tao → 0, a person chooses the answer of the highest value; tao → ∞, a person chooses randomly among 3 answers.

This is my model file model.txt:

model{
    # data
    for(i in 1:n) # for each person
    {
        for (j in 1:m) # for each problem
        {
            # answer chosen
            answer[i,j] ~ dcat(exp(V[j,]/tau[i])/sum(exp(V[j,]/tau[i])))
        }
    }
    # priors
    for (i in 1:n)
    {
        tau[i] ~ dgamma(0.001,0.001)
    }
}

Here's my R script:

library(R2jags)
m <- 6 # number of problems
n <- 2 # number of people
V <- matrix(c(0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)), nrow=m, ncol=3, byrow = T)
answer <- matrix(c(2,3,1,1,2,3,1,1,1,1,3,2), nrow=n, ncol=m, byrow = T) 

data <- list("m", "n", "V", "answer")
myinits <- list(list(tau = rep(1,n)))
parameters <- c("tau")

samples <- jags(data, inits=myinits, parameters,
                model.file ="model.txt", n.chains=1, n.iter=1000, 
                n.burnin=1, n.thin=1, DIC=T)

After running the script in R, I got the error message below:

Compiling model graph Resolving undeclared variables Allocating nodes Deleting model

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, : RUNTIME ERROR: Invalid vector argument to exp

What might be the problem? Many thanks for your help!

ramund
  • 185
  • 11

1 Answers1

2

You are supplying a column vector from V to exp. In JAGS, inverse link functions can only be given scalar values. Basically, to properly code out softmax regression you will need to iterate through each element of V. Additionally, V must be a three dimensional vector (person x subject x 3 choices).

model{
    # data
    for(i in 1:n) # for each person
    {
        for (j in 1:m) # for each problem
        {
            # answer chosen
            answer[i,j] ~ dcat(mu[i,j,1:3])
            mu[i,j,1:3] <- exp_v[i,j,1:3] / sum(exp_v[i,j,1:3])
              for (k in 1:3) {
                exp_v[i,j,k] <- exp(V[i,j,k]/tau[i])
        }
    }
    # priors
    for (i in 1:n)
    {
        tau[i] ~ dgamma(0.001,0.001)
    }
}

Unlike your model, V is indexed to i,j,k instead of what I would assume to be j and k. You would need to restructure your V array in order to fit this. However, fitting the model this way allows you to input a scalar to exp, which you can then sum afterwards.

mfidino
  • 3,030
  • 1
  • 9
  • 13
  • It worked! Thank you for your answer! (I didn't know JAGS can't exponentiate vectors before!) – ramund Aug 22 '17 at 23:58