I want to extract z
in the trasformed data block from the stanfit object f
. Is it possible?
library(rstan)
m <- stan_model(model_code = '
data{real x;}
transformed data{real z; z = chi_square_rng(x); }
parameters {real y;}
model {y ~ normal(z,1);}')
f <- sampling(m, data=list(x=1), iter = 100)
I would add a new value into generated quantities
like this:
library(rstan)
m <- stan_model(model_code = '
data{real x;}
transformed data{real z; z = chi_square_rng(x); }
parameters {real y;}
model {y ~ normal(z,1);}
generated quantities {real zhat = z;}')
f <- sampling(m, data=list(x=1), iter = 100)
This returns valid values
print(f, pars = "zhat")
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
zhat 0.16 0 0 0.16 0.16 0.16 0.16 0.16 2 0.98
And you can extract the values, just to show a few of the results:
> extract(f)[["zhat"]]
[1] 0.16445 0.16445 0.16445 0.16445 0.16445 0.16445
[7] 0.16445 0.16445 0.16445 0.16445 0.16445 0.16445
[13] 0.16445 0.16445 0.16445 0.16445 0.16445 0.16445