Why is there a second bracket without closing the first?

transformed parameters {
    vector[u] h;
    matrix[r, w] yo = rep_matrix(1,r,w);
    matrix[r, w] yo2  = rep_matrix(1,r,w);
    matrix[r, w] yo3 = rep_matrix(1,r,w);
    matrix[r, w] yo4 = yo3;
    {
      matrix[r,w] jk = rep_matrix(1,r,w);
      for(i in 1:u){
        h[i] = h_r[i] - ( 1/2);
      }

I made some edits to your code. Enclosing your model with ``` makes it easier to read.

Can you post the whole model and the reference from where it’s from?

I just want to understand why it opens the second bracket without closing the first in “transformed parameters”, what does this do as a syntax? Thanks for response

Does the model run? The bracket looks odd to me there.

yes model run!

Why jk is defined inside brackets? It is a local variable which is only defined inside of those brackets, like temporary variable

I’ve taken to creating nested local environments like this to put variable declarations closer to where they’re actually computed or used. It can confuse though, I admit.

@mike-lawrence I forget when it was introduced, but you can now do this in the model block:

parameters {
 real x;
 real y;
}
model {
 real w = 1.5;
 x ~ normal(w, 1);
 
 real z = -3.2;
 y ~ normal(z, 1);
}

I haven’t tested it in rstan, but with cmdstanr and a recent version of cmdstan that definitely works.

1 Like

this was introduced in the Stanc3 compiler, it went into CmdStan v 2.22 (Jan 2020)

2 Likes

What do you mean? If these brackets missing is tha same???

A Stan program has named program blocks.

The data and parameters blocks consist only of variable declarations.

All other blocks allow both variable declarations and statements. A block of statements enclosed by curly braces ({, }) is a local block. Blocks can be nested as deeply as you like. Variables declared inside a local block are local variables. The only variables that are reported as part of the model outputs are the variables declared in the top-level of the parameters, transformed parameters, and generated quantities block.

In your example, the matrix jk won’t be reported in the Stan output file, but variables h, yo, … yo4 will.

3 Likes

Thank you so much!!!