0

I am having a very hard time trying to subtract two vectors in my OpenBUGS model. The last line of the code below keeps giving the error "expected right parenthesis error":

model { 
  for ( i in 1:N) {
    for(j in 1:q) {
      vv[i,j] ~ dnorm(vz[i,j],tau.eta[j])
    }
    vz[i,1:q] ~ dmnorm(media.z[i,], K.delta[,])
    for(j in 1:q) {
      mean.z[i,j] <- inprod(K[i,] , vbeta[j,])
    }
    K[i,1] <- 1.0
    for(j in 1:N) {
      K[i,j+1] <- sum(ve[,i] - ve[,j])
    }
  }

If I change that line to K[i,j+1] <- sum(ve[,i]) - sum(ve[,j]), then the model works fine, but that is not what I want to do. I would like to subtract element-wise.

I searched SO for OpenBUGS, but there are only a few unrelated topics:

OpenBUGS - Variable is not defined

OpenBUGS: missing value in Bernoulli distribution

In Stats Stack Exchange there is this post which is close, but I still could not make how to implement this in my model:

https://stats.stackexchange.com/questions/20653/vector-multiplication-in-bugs-and-jags/20739#20739

I understand I have to write a for loop, but this thing is sure giving me a big headache. :)

I tried changing that line to:

for(k in 1:p) { temp [k] <- ve[k,i] - ve[k,j] }
K[i,j+1] <- sum(temp[])

where 'p' is the number of rows in each 've'. Now I keep getting the error "multiple definitions of node temp[1]".

I could definitely use some help. It will be much appreciated.

Best regards to all and thanks in advance!

PS: I wanted to add the tag "OpenBUGS" to this question but unfortunately I couldn't because it would be a new tag and I do not have enough reputation. I added "winbugs" instead.

Community
  • 1
  • 1
bomgaroto
  • 95
  • 1
  • 1
  • 6
  • Really sorry. I just realised I should have searched for "WinBUGS" too. Although I haven't solved the problem yet, I found this post which is closer to my problem: http://stackoverflow.com/questions/14509546/multiple-definition-of-node-a-error-in-winbugs?rq=1 – bomgaroto Aug 09 '13 at 23:34

1 Answers1

1

The "multiple definitions" error is because temp[k] is redefined over and over again within a loop over i and another loop over j - you can only define it once. To get around that, use i and j subscripts like

for(k in 1:p) { temp[k,i,j] <- ve[k,i] - ve[k,j] } 
K[i,j+1] <- sum(temp[,i,j])

Though if that compiles and runs, I'd check the results to make sure that's exactly what you want mathematically.

Chris Jackson
  • 871
  • 5
  • 7
  • Thank you Chris. That is very helpful. It compiles and runs now. This is a non-linear regression model and I wanted to implement the gaussian kernel with the input vectors (ve). That is why I needed to subtract them. I put the 'k' index as the third one and it works too as `temp[i,j,k] <- ve[k,i] - ve[k,j]`. I am guessing it is the same thing. – bomgaroto Aug 12 '13 at 18:48
  • Just an update to let you all know that this answer works very well. I've checked the results and they agree with what is expected. – bomgaroto Aug 15 '13 at 23:04