Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rjags error message: Dimension mismatch

Tags:

r

bayesian

jags

I'm trying to study Bayesian analysis based on book "Doing Bayesian Data Analysis: A Tutorial with R, JAGS, and Stan (2015)".

In this book, there are examples. So, I'm trying to replicate this example in R. However, I have got an error message in this example.

To be specific, this is the data of example.

data
   y        s
1  1 Reginald
2  0 Reginald
3  1 Reginald
4  1 Reginald
5  1 Reginald
6  1 Reginald
7  1 Reginald
8  0 Reginald
9  0     Tony
10 0     Tony
11 1     Tony
12 0     Tony
13 0     Tony
14 1     Tony
15 0     Tony

y<-data$y
s<-as.numeric(data$s)
Ntotal=length(y)
Nsubj=length(unique(s))

dataList=list(y=y, s=s, Ntotal=Ntotal, Nsubj=Nsubj)

Also, this is my model.

modelString=" 
model{
  for(i in 1:Ntotal){
    y[i] ~ dbern(theta[s[i]])
  }
  for(s in 1:Nsubj){
    theta[s] ~ dbeta(2,2)
  }
}
"
writeLines(modelString, con="TEMPmodel.txt")

library(rjags)
library(runjags)
jagsModel=jags.model(file="TEMPmodel.txt",data=dataList)

In this case, I got an error message.

Error in jags.model(file = "TEMPmodel.txt", data = dataList) : 
  RUNTIME ERROR:
Cannot insert node into theta[1...2]. Dimension mismatch

I don't know what I have made mistake in this code. Please give me advice.

Thanks in advance.

like image 997
kmangyo Avatar asked Oct 24 '15 08:10

kmangyo


1 Answers

As suggested by @nicola, the problem is that you're passing s as data to your model, but also using s as the counter iterating over 1:Nsubj. As JAGS indicates, this causes confusion about the dimension of theta... does it have length 15, or 2?

The following works:

model{
  for(i in 1:Ntotal){
    y[i] ~ dbern(theta[s[i]])
  }
  for(j in 1:Nsubj){
    theta[j] ~ dbeta(2,2)
  }
}
like image 59
jbaums Avatar answered Oct 31 '22 14:10

jbaums