SYNTAX PROBLEM: modifying data in model block or define int in transformed parameter block

Hi everyone,

I am puzzled about the syntax in stan, and I am not sure if I can overcome this problem in any way.
I have to fit to some integer data a multinomial likelihood:

model {
for(y in 1:nyears){
data[,y] ~ multinomial(model[,y]);

On the left side of the tilde I can only enter an object of nature int. The problem is that I want to apply a transformation to my data within the parameter block in stan. The issue doing that is that stan does not allow to transform whatever you put in the data block. I thought a possible way to overcome the problem could have been defining an integer of the same dimension of data in the parameters block and fill it with the transformation of the data, as such:

transformed parameters {
  int data_transformed[nlen,nyears];
  scale the survey data
  for (y in 1:nyears) {
    for (l in 1:nlen) {
      data_transformed[l,y] = data[l,y]/(sum(data[,y])*x;

The issue is that stan does not allow to define in into the transformed parameters block. I read that a possible trick was to put the operation into curly brackets, but doing so I am not able to call data_transformed in the model block.
It could be great if anybody had any idea! Thank you in advance.

If you want to apply a transformation to the data within stan, use the transformed data block so that the transformation is applied once and then remains fixed, thereby not imposing new computational costs at each iteration or leapfrog step.

However, your transformation doesn’t appear to make sense; you declare data_transformed to be an integer, but data[l,y]/(sum(data[,y])*x; doesn’t look like it will yield exclusively integers.

1 Like

Thank you very much for answering. Yes this partially solve the problem actually. I know that what comes out of the equation is not integer, but after being multiplied by x the value should be rounded to the integer. The problem is that Stan rounds to the integer before multiplying by x, so the result of the operation is a long sequence of 0s and few 1s. If I enter data as real or as matrix, I cannot perform the operation. Let me know if something comes to your mind! Thank you again