从stan 网页开始标准示例时,如下所示:
schools_code <- '
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
real theta[J];
real mu;
real<lower=0> tau;
}
model {
theta ~ normal(mu, tau);
y ~ normal(theta, sigma);
}
'
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18))
fit <- stan(model_code = schools_code, data = schools_dat,
iter = 1000, n_chains = 4)
Run Code Online (Sandbox Code Playgroud)
(这是从这里获得的)
但这只能为我提供参数后验的分位数.所以我的问题是:如何获得其他百分位数?我想它应该类似于错误(?)
评论:我试图引入标签stan然而,我声名太低;)对不起
从rstan v1.0.3(尚未发布)开始,您将能够apply()直接在stanfit class由...生成的对象上使用主力函数stan() function.如果fit是从中获得的对象stan(),那么例如,
apply(fit, MARGIN = "parameters", FUN = quantile, probs = (1:100) / 100)
Run Code Online (Sandbox Code Playgroud)
要么
apply(as.matrix(fit), MARGIN = 2, FUN = quantile, probs = (1:100) / 100)
Run Code Online (Sandbox Code Playgroud)
前者将FUN应用于每个链中的每个参数,而后者在将FUN应用于每个参数之前组合链.如果你只对一个参数感兴趣,那么就像
beta <- extract(fit, pars = "beta", inc_warmup = FALSE, permuted = TRUE)[[1]]
quantile(beta, probs = (1:100) / 100)
Run Code Online (Sandbox Code Playgroud)
是一个选择.
这是我的尝试希望这是正确的:
假设fit是从 获得的对象stan(...)。那么任何百分位数的后验可以通过以下方式获得:
quantile(fit@sim$sample[[1]]$beta, probs=c((1:100)/100))
Run Code Online (Sandbox Code Playgroud)
其中方括号中的数字是我猜的链。如果这还不清楚:我用rstan