# Error in JAGS code [closed]

I'm trying to run the following code in JAGS

model {
for (i in 1:n){
z[i] ~ dnorm(X[i], tau)
X[i] <- D1*x1[i] + D2*x2[i] + D3*x3[i] + D4*x4[i]
}

denom <- 1 + sum(exp(p))
D1 <- (exp(p[1]))/denom
D2 <- (exp(p[2]))/denom
D3 <- (exp(p[3]))/denom
D4 <- 1/denom

# Prior for Fixed Effects
p[1] ~ dnorm(0,.01)
p[2] ~ dnorm(0,.01)
p[3] ~ dnorm(0,.01)

# Prior for precision of Delta
tau2 <- pow(sigma2, -2)
sigma2 ~ dunif(0, 100)

# Prior for Variance of Z
tau <- pow(sigma, -2)
sigma ~ dunif(0, 100)

}


In R I run

# Data
z <- d1$transfers.cap x1 <- ifelse(d1$LM==1, 1, 0)
x2 <- ifelse(d1$LM==2, 1, 0) x3 <- ifelse(d1$LM==3, 1, 0)
x4 <- ifelse(d1\$LM==4, 1, 0)
n <- dim(d1)[1]

ex.data <- list(z=z, x1=x1, x2=x2, x3=x3, x4=x4, n=n)
out <- jags.model(file="SEM.bug", data=ex.data, n.chains=5, n.adapt=1000)


But I get the error message

Compiling model graph
Resolving undeclared variables
Allocating nodes
Deleting model

Error in jags.model(file = "SEM.bug", data = ex.data, n.chains = 5, n.adapt = 1000) : RUNTIME ERROR:
Invalid vector argument to exp


How can I solve this?

• @glen_b and other question closers: Just found this question via google. Totally helpful as its the same error message I am getting. – colin Jul 11 '16 at 15:27

(most likely) in one of these lines:

denom <- 1 + sum(exp(p))
D1 <- (exp(p[1]))/denom
D2 <- (exp(p[2]))/denom
D3 <- (exp(p[3]))/denom


When the code is running those, part of the contents of p isn't something you want to pass to an exp function. It says "invalid vector argument". Can exp take a vector argument?

If it can, maybe that's a message about its contents - maybe it's a really large number, or maybe it's not a number at all.

The firs problem might be mathematically solved (but the result might still not necessarily be numerically meaningful) by dividing numerators and denominators by a large to middling element of exp(p).

• Thanks for the comment, Glen. But I don't understand what you're suggesting. Can you provide a solution, maybe? – user1172558 Mar 21 '13 at 5:50
• Can I provide a solution to the likely fact that the exp function doesn't take a vector? If that's really your question, its a purely code-question ("How do I exponentiate a vector in JAGS?"), and clearly off topic here. Would you like to flag it for moderator attention, so it can be moved to stackoverflow where it belongs? – Glen_b -Reinstate Monica Mar 21 '13 at 5:59
• exp should just be e^{p[1]}, so it's not a vector. – user1172558 Mar 21 '13 at 7:42
• in the line where you define denom? – Glen_b -Reinstate Monica Mar 21 '13 at 7:45
• I think Glen is entirely correct and the problem is your definition of denom. You either need to spell it out since you have just 3 elements anyway to exp(p[1])+exp(p[2])+exp(p[3]) or as a more general solution use a loop to define a additional vector like exponential.of.p[i] <- exp(p[i]) and the sum over that. – Erik Mar 21 '13 at 8:07