gpt4 book ai didi

r - 如何获得 stan 中参数的完整边际分布

转载 作者:行者123 更新时间:2023-12-03 07:33:31 26 4
gpt4 key购买 nike

当从 stan 启动标准示例时 webpage像下面这样:

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)

(这是从 here 获得的)

然而,这只为我提供了参数后验的分位数。所以我的问题是:如何获得其他百分位数?我猜它应该类似于 bugs(?)

评论:我试图引入标签stan,但是,我的声誉太低;)对此感到抱歉

最佳答案

rstan v1.0.3(尚未发布)开始,您将能够直接在 stanfit 对象上使用主力 apply() 函数由 stan() 函数生成的类。如果fit是从stan()获得的对象,那么例如,

apply(fit, MARGIN = "parameters", FUN = quantile, probs = (1:100) / 100)

apply(as.matrix(fit), MARGIN = 2, FUN = quantile, probs = (1:100) / 100)

前者将 FUN 应用于每个链中的每个参数,而后者在将 FUN 应用于每个参数之前组合链。如果您只对一个参数感兴趣,那么类似

beta <- extract(fit, pars = "beta", inc_warmup = FALSE, permuted = TRUE)[[1]]
quantile(beta, probs = (1:100) / 100)

是一个选项。

关于r - 如何获得 stan 中参数的完整边际分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12268220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com