I am writing my first stan program, with Rstan interface. in the model below, if I declare the betavar variable as a local variable in the model block, the code runs, but the values of betavar is not saved. If I declare the betavar in the transformed parameters block, I get an error :

52: n4= abs(l-ll);
53: sumn = n1+n2+n3+n4;
54: betavar = sigbeta2*(gam^sumn);
55: if (sumn == 0) beta[n1,n2,n3,n4] =0

Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘b88c4f5b4184fcf2b7d34fb060c58a97’ due to the above error.

Here is the model part. Any help would be much appreciated.

data {
int<lower=1> xdim;
int<lower=1> ydim;
int<lower=1> zdim;
int<lower=1> tdim;
real YData[64,64,33,210];
parameters {
real beta[6,6,6,6];
real gam;
transformed parameters {
real<lower=0> sig2;
real<lower=0> sigbeta2;
real betavar;

model {
real Ymean;
int xliml;
int yliml;
int zliml;
int tliml;
int xlimu;
int ylimu;
int zlimu;
int tlimu;
int n1;
int n2;
int n3;
int n4;
int sumn;

for( i in 1:xdim) {
for( j in 1:ydim) {
for( k in 1:zdim) {
for( l in 1:tdim) {
Ymean = 0.0;
xliml =max(1, i-5);xlimu = min(i+5, xdim);
yliml =max(1, j-5); ylimu = min(j+5, ydim);
zliml =max(1, k-5); zlimu = min(k+5, zdim);
tliml =max(1, l-5); tlimu = min(l+5, tdim);
for(ii in xliml:xlimu){
for(jj in yliml:ylimu){
for(kk in zliml:zlimu){
for(ll in tliml:tlimu){
n1<- abs(i-ii);
n2= abs(j-jj);
n3= abs(k-kk);
n4= abs(l-ll);
sumn = n1+n2+n3+n4;
betavar = sigbeta2*(gam^sumn);
if (sumn == 0) beta[n1,n2,n3,n4] =0
beta[n1,n2,n3,n4] ~ normal(0.0, betavar);
Ymean = Ymean + YData[ii,jj,kk,ll]*beta[n1,n2,n3,n4];
} } } }

YData[i,j,k,l] ~ normal(Ymean, sig2);

gam ~ uniform(0,1);
sig2 ~ inv_gamma(1,1);
sigbeta2 ~ inv_gamma(1,1);

You can’t assign to variables defined in the transformed parameters block outside of that block.

For instance, if you had a parameter sigma and wanted also the parameter sigma^2, you could do this:

parameters {
  real<lower=0.0> sigma;
transformed parameters {
  real sigma_squared;

  sigma_squared = sigma^2;

But you can’t modify sigma_squared in the model or generated quantities blocks after that.

(the error should be something like: attempt to assign variable in wrong block. left-hand-side variable origin=transformed parameter)

In your case you’d need to define a betavar in the transformed data block big enough to hold all the intermediate betavars you compute, fill up that array of betavars, and then you use that in the model block.

Hope that helps!

agreed. you should get a better error message - will check this one out.

1 Like

@mitzimorris , nono, the error is actually that. I just ran some code with that problem and copy-pasted the error here :D.

right - the parser doesn’t give a proper error message - will check out the parser.
what version(s) did you try this on?

From R’s sessionInfo():


continuing to agree with @bbbales2 - ran the model in Rstan -

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Yosemite 10.10.5

and got the error message:

ERROR at line 52

 50:                      n4= abs(l-ll); 
 51:                      sumn = n1+n2+n3+n4;
 52:                      betavar = sigbeta2*(gam^sumn);
 53:                      if (sumn == 0) beta[n1,n2,n3,n4] =0

PARSER EXPECTED: <expression assignable to left-hand side>

there are two problems with the line 53:

 if (sumn == 0) beta[n1,n2,n3,n4] =0
  1. need semicolon after both stmts in an if-else construct
  2. you can’t assign to parameter beta - there is no way to set a parameter, although you can init params via Rstan. so you can’t really do exactly what your trying to do - although a simple if stmt condition
    if (sumn != 0) or (sumn > 0) or something you could still do the sampling in the then clause.

if for some reason you’re not seeing the full error message, please send details on Rstan and Stan versions.

1 Like