Checking Stan model

Hello,
I am new to stan and I was wondering if there was an easy way to visualize intermediate quantity used in a stan model for debugging purposes? Especially, I would like to be able to see the quantity created by the transformed data block (either using Rstan or cmdstanr) ?

data {
  int<lower=1> N;      // number of observations
  vector[N] x;  
  vector[N] y; 
}
transformed data {
  // Normalize data
  real xmean = mean(x);
  real ymean = mean(y);
  real xsd = sd(x);
  real ysd = sd(y);
  real xn[N] = to_array_1d((x - xmean)/xsd);
  vector[N] yn = (y - ymean)/ysd;
}
// rest of the model

For example, in this very simple model, I would like to visualize xn, yn.

Thanks for your help,
Edouard

1 Like

While not the most efficient, the simplest approach for visualising transformed data items is to assign them to a dummy output in the generated quantities block:

generated quantities {
  real xn_out[N] = xn;
  vector[N] yn_out = yn;
}

These quantities will then be reported in the output for visualisation.

If you just want to see what values are being created (rather than additional visualisation), then you can just use the print() function to print the values to the console

1 Like