Use incrementing index in transformed parameter block

How do I do the following

transformed parameters {
	real xi[S, S];		// fix effect
	vector<lower=0.0,upper=1>[D] beta[S,S];

	int phiIndex = 0;
	for (row in 1:S){
		for (col in 1:S){
			if (row == col){
				xi[row, col] = fi[phiIndex];
				phiIndex += 1;
			}
		}
	}
	for (row in 1:S){
		for (col in 1:S){
			if (row == col){
				for (d in 1:D){
					beta[row, col, d] = fi[phiIndex];
					phiIndex += 1;
				}
			}
		}
	}
}

I am using phiIndex to increment the pointer for vector fi. The compiler does allow the declaration
int phiIndex = 0

You have to declare int phiIndex inside a local block within transformed parameters.

1 Like

I put the whole thing inside another { }, now it is ok with the declaration but doesn’t take the increment statement.

   phiIndex += 1;

says PARSER EXPECTED:

You have to do phiIndex = phiIndex + 1; with Stan versions less than 2.17.0.

1 Like

thank you, fixed!