llewmills
llewmills

Reputation: 3568

Translate JAGS != logical operator into Stan

I am trying to adapt some of Kruschke's code into Stan and cannot reproduce the following.

# JAGS code
for ( i in 1:NxBetweenLvl ) { 
     for ( j in 1:NxWithinLvl ) { 
         mBxW[i,j] <- ( sum( mSxBxW[1:NxSubjectLvl,i,j] ) / sum( mSxBxW[1:NxSubjectLvl,i,j]!=0 ) )
     }
}

Note that mSxBxW[,,] is a three-dimentional array defined upstream.

What does the function in the loop do? Is it "sum of cells 1:NxSubectLvl in dimensions i and j in array mSxBxW[,,] divided by sum of the total number of non-zero cells across cells 1:NxSubectLvlin dimension i and j? Is that right?

And how would i reproduce this in Stan?

If I use this code in the same place in my Stan script...

// Stan Code
      for (i in 1:nGroup) {
        for (j in 1:nCond) {
          mGxC[i,j] = sum(mSxCxG[1:nSubj,i,j]) / sum( mSxCxG[1:nSubj,i,j]!=0 );
        }
      }

... it throws the error

Binary infix operator != with functional interpretation logical_neq requires arguments or primitive type (int or real), found left type=real[], right arg type=int; 

Which means nothing to me, other than there is something about the != operator that works in JAGS but not in Stan.

Upvotes: 0

Views: 148

Answers (1)

Kreuni
Kreuni

Reputation: 312

The problem is that the 0 is an integer while your matrix has reals in it.

One way around this is to just define a real variable as zero and use that:

   {
      real zero = 0;
      for (i in 1:nGroup) {
        for (j in 1:nCond) {
          mGxC[i,j] = sum(mSxCxG[1:nSubj,i,j]) / sum( mSxCxG[1:nSubj,i,j]!=zero);
        }
      }
   }

Upvotes: 1

Related Questions