How to declare a variable in model block

Look, if you need to post fake code as a stand-in, then say so upfront and try to make sure that it either compiles or produces the same error that you got from your real code. I had to change

normal(0,s_trial[o];
}

to

normal(0,s_trial[o]);

in order for the code to compile.

Now when I Googled the error message “Dimension mismatch in assignment”, I found a forum post that seemed to indicate that the error happens during compilation. Before the error message that you posted, did you see a message that said, “SYNTAX ERROR, MESSAGE(S) FROM PARSER”?

Yes.
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Dimension mismatch in assignment; variable name = log_A, num dimensions given = 0; right-hand side dimensions = 2
error in ‘model27e4503d3a3_8003a76c0b33e462a94a34effb8bf314’ at line 23, column 8
20: transformed data{
21: matrix[N,T] log_A;
22: log_A= log(A);
^

PARSER EXPECTED: expression assignable to left-hand side>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘8003a76c0b33e462a94a34effb8bf314’ due to the above error.

The data in matrix A has 0 as some elements. is it the reason that I can not get the log(A)? I want to use poisson_log to avoid the overflow in the model. there is exp() in the model.

The parser has no idea what’s in matrix A, so no. This is purely a problem with syntax, and one that is not in the “fake” Stan code that you’ve shown here.

The problem is solved by doing:

transformed data{
matrix[N,T] log_A= log(A);
}

instead of

transformed data{
matrix[N,T] log_A;
log_A= log(A);
}

For some easy conjugate problems, like say the Rats problem in BUGS, the speeds are about the same when measured with properly calibrated multi-chain effective sample sizes.

For most of those JAGS and BUGS models with discrete parameters, it’s way faster to run them marginalized in Stan and in BUGS or JAGS.

For hard problems, Stan is infinitely faster because Gibbs will never converge.

For problems in between, the speedup is somewhere between 1 and infinite. This isn’t so much JAGS or BUGS per se, as Gibbs. Switching to faster Gibbs doesn’t help for hard problems—you can run millions of iterations and not converge.

This does require you to code the models well in both systems. Badly coded models will fail to converge in both JAGS and Stan.

Cole Monahan wrote a nice survey. Michael Betancourt’s arXiv papers contain all the details on why HMC is so much faster in high dimensions and for hard problems.