eleg23
eleg23

Reputation: 1

R JAGS: Dimension mismatch

I am trying to run a model in JAGS but I got the following error:

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
RUNTIME ERROR:
Cannot insert node into m[1]. Dimension mismatch

The model I specified is

model = "model
    {
      for (i in 1:N) {
        z[i] ~ dnorm(m[i],tau)
        m[i] <- beta0 + beta1*x + beta2*y
      }
      beta0 ~ dnorm(0.0,1.0E-3)
      beta1 ~ dnorm(0.0,1.0E-3)
      beta2 ~ dnorm(0.0,1.0E-3)
      tau ~ dgamma(0.01,0.01)
    }"

Then I specify the data and the initial values. Finally, I run the model.

data = list(z = wolfcamp$data, x = wolfcamp$coords[,1], y = wolfcamp$coords[,2], N = length(wolfcamp$data))

parameters=c("beta0","beta1","beta2","tau")
init1 = list(beta0 = 0, beta1 = 0, beta2 = 0, tau = 0)
init2 = list(beta0 = 1, beta1 = 1, beta2 = 1, tau = 1)
initial.values=list(init1, init2)

model1=jags(data=data, inits=initial.values,
            parameters.to.save=parameters,
            model.file=textConnection(model),
            n.chains=2,
            n.burnin=1000,
            n.iter=11000)

Can you help me? Do you know why I get this error? Thanks in advance.

Upvotes: 0

Views: 1471

Answers (1)

mfidino
mfidino

Reputation: 3055

It looks like you are not indexing through the vectors x and y, which causes a dimension mismatch.

Change this line:

m[i] <- beta0 + beta1*x + beta2*y

to this:

m[i] <- beta0 + beta1*x[i] + beta2*y[i]

and you should be good to go (so long as x and y are of length N)

Upvotes: 2

Related Questions