Plot data from a stan file and define char-valued functnions in a Stan file

I am not sure the following two is possible or not.

  • I want to plot data synthesized in a transformed data block,
  • I also want to define character valued function in Stan file, as following manner.

My desired code

  stanmodelcode <- "


 functions {
   char foo(int x ) {
  if(x) y = "TRUE";   
  if(!x) y = "FALSE";  
     return y;
   }
 }

data {
  int<lower=0> N;
  real z[N];
} 
transformed data {                 
real x[3];
real y[3];

 x[1] = normal_rng (0,1); 
 x[2] = normal_rng (0,1); should change seed?...how??
 x[3] = normal_rng (0,1);

 y[1] = normal_rng (0,1);
 y[2] = normal_rng (0,1);
 y[3] = normal_rng (0,1);

plot(x,y); // I want to plot the data from stan file, is it possible?
print(foo(0),foo(1)); // I also want to define char-valued function in Stan file, possible?

}
parameters {
  real mu;
} 

model {
  target += normal_lpdf(mu | 0, 10);
  target += normal_lpdf(z  | mu, 1);
} 
"

z <- rnorm(20) 
dat <- list(N = 20, z = z); 
fit <- stan(model_code = stanmodelcode, model_name = "example", 
            data = dat, iter = 2012, chains = 3, verbose = TRUE,
            sample_file = file.path(tempdir(), 'norm.csv'))
1 Like

Hi,
unfortunately I don’t think it is possible to access the result of transformed data block currently. So if you need extensive manipulation in transformed data I suggest you do the transformation in R (or whatever language you call Stan from) and pass the results as data.

Best of luck with your model!

1 Like