提取 Stan 文件中转换后的数据块中的数据

2024-01-01

我想提取z在 stanfit 对象转换后的数据块中f。是否可以?

    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)

我会添加一个新值generated quantities像这样:

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)

这将返回有效值

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

您可以提取值,只是为了显示一些结果:

> 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

提取 Stan 文件中转换后的数据块中的数据 的相关文章

随机推荐