I tried to assemble a specific correlation matrix in the transformed parameters block, yet failed due to the stan syntax error that my index ind could not be declared as “int” in the transformed parameters block. Below is my code; any suggestion on how to make this work?

```
transformed parameters{
corr_matrix[sum(K)] R;
int<lower=0> ind; // couldn't declare ind as int here; real would work, but nu[ind] require ind to be integer
R = rep_matrix(0, sum(K), sum(K));
for (m in 1:sum(K)){
R[m, m] = 1;
}
ind = 0;
for (m in 1:K[1]){
for (n in (K[1]+1): sum(K)){
ind += 1;
R[m, n] = nu[ind]; // nu is a vector, want to assess different element in nu each time
R[n, m] = nu[ind];
}
}
}
```

1 Like

Could you show us the actual error message? That is, can you copy-and-paste the error message in your post?

Sure. Here is the error message:

I see the problem. Since Stan calculates derivatives with respect to parameters, the parameters have to be continuous quantities. However, if you only need `ind`

to be an index and aren’t using it as an actual parameter in your model, there should be a simple solution:

```
transformed parameters{
corr_matrix[sum(K)] R;
{ // Opening bracket to start a "block".
// Variables declared in this block cannot be seen outside of the block.
R = rep_matrix(0, sum(K), sum(K));
for (m in 1:sum(K)){
R[m, m] = 1;
}
int ind = 0;
for (m in 1:K[1]){
for (n in (K[1]+1): sum(K)){
ind += 1;
R[m, n] = nu[ind]; // nu is a vector, want to assess different element in nu each time
R[n, m] = nu[ind];
}
}
} // End block
}
```

Thank you, James for your quick reply. I tried your solution, but stan gave me another error:

Sorry, that was my mistake. I forgot that variables must be declared at the beginning of a block. Here’s the corrected code:

```
transformed parameters{
corr_matrix[sum(K)] R;
{ // Opening bracket to start a "block".
// Variables declared in this block cannot be seen outside of the block.
int ind = 0; // Variable declarations must be at the top of a block.
R = rep_matrix(0, sum(K), sum(K));
for (m in 1:sum(K)){
R[m, m] = 1;
}
for (m in 1:K[1]){
for (n in (K[1]+1): sum(K)){
ind += 1;
R[m, n] = nu[ind]; // nu is a vector, want to assess different element in nu each time
R[n, m] = nu[ind];
}
}
} // End block
}
```

Thank you so much! It works now.