Applying a function across columns of a matrix in transformed_data

The user’s guide shows how to standardize variables in the transformed data block. The example writes out the same code for each variable it wants to standardize:

  x_std = (x - mean(x)) / sd(x);
  y_std = (y - mean(y)) / sd(y);

Sometimes we want to standardize many variables at once that may be the columns or rows of matrices. We could write a function and use it in a loop to do so. I think the below works to standardize each column of Y:

functions{
  real standardize(real foo){
    return (foo - mean(foo)) / sd(foo) 
  }
}

data{
  int<lower = 0> N;
  int<lower = 0> K;
  matrix[N,K] Y;
}

transformed data{
  for (k in 1:K){ 
    Y[ ,k] = standardize(Y[ ,k])
  }
}

Is there a way to get rid of this loop? The main motivation is simpler code, but perhaps there is some vectorized approach that is also faster.

While a previous question (Does Stan has a function similar to the Apply function in R?) was met with map_rect(), it is considerably more verbose than the loop and most examples I’ve found show map_rect() being used for parallelizing model fitting (for which the overhead would not be worthwhile in this simple case). Accordingly, I’m not sure if there is a solution when you are only working within the transformed blocks.

A loop is going to be your best option here.

Additionally, it’s not as important to maximise efficiency in the transformed data block, since that’s only executed once