Use incrementing index in transformed parameter block

stanc

#1

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


#2

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


#3

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:


#4

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


#5

thank you, fixed!