我正在尝试使用bridgesampling
R 中的包计算贝叶斯因子。为此,我试图将模型拟合到Stan
. 它是一个分层模型,其中具有参数的替代(假设)模型beta, gamma1, gamma3 & sigma
。
请在下面找到型号:
stanmodelH1 = 'data {
int<lower=0> n;
int<lower=0> k;
vector[n] y;
int x1[n];
real<lower=0> a;
real<lower=0> b;
matrix[n, k] G;
matrix[n, k+1] X1;
matrix[n, 2] x;
}
parameters {
vector[2] beta;
vector[k+1] gamma3;
vector[k] gamma1;
real<lower=0> sigma;
}
model {
target += inv_gamma_lpdf(sigma | a, b);
target += normal_lpdf(gamma1 | 0, sqrt(1.1));
target += normal_lpdf(gamma3 | 0, 1);
target += normal_lpdf(beta | 0, sqrt(1.2));
target += normal_lpdf(y | x*beta + G*gamma1, sqrt(sigma)) + bernoulli_lpmf(x1 | Phi(X1*gamma3));
//these two _lpdf and _lpmf should be added and not multiplied. This is the answer.
}
'
和相应的rstan
代码:
stanfitmodelH1 = sampling(stanmodelH1, data = list(n = n, k = k, y = y, x1 = x1,
a = 4, b = 3, G = G, X1 = X1, x = x),
iter = 50000, warmup = 20000, chains = 3, cores = 3,
control = list(adapt_delta = 0.99, max_treedepth = 15))
现在,当我从这个模型中取样时Stan
;它引发以下错误:
Warning messages:
1: There were 205 divergent transitions after warmup. Increasing adapt_delta above 0.99 may help. See
http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
2: There were 3 chains where the estimated Bayesian Fraction of Missing Information was low. See
http://mc-stan.org/misc/warnings.html#bfmi-low
3: Examine the pairs() plot to diagnose sampling problems
请注意,从空模型采样时会引发类似的错误。基本上beta = beta0 = c(rnorm(1), 0)
与gamma3 = rep(0, k+1)
替代模型中的无限制相反。
这些显然是由于Stan
. 我知道这一点是因为我在没有以下部分的情况下多次运行模型(以及空模型):bernoulli_lpmf(x1 | Phi(X1*gamma3))
在Stan
模型的最后一行并相应地修改代码的数据块,然后它没有返回任何警告或错误。但是,这不会解决我的目的。因为,我的假设涉及两者beta & gamma3
,并且上述部分清楚地添加了gamma3
.
由于 中的错误,包中Stan
的函数返回以下错误:bridge_sampler
R
bridgesampling
> H1.bridge = bridge_sampler(stanfitmodelH1, silent = TRUE)
Error in while (i <= maxiter && criterion_val > tol) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
2744 of the 45000 log_prob() evaluations on the proposal draws produced -Inf/Inf.
另请注意,bridge_sampler
当没有来自 的错误时,不会抛出这些错误(来自) Stan
。例如,模型在没有零件的情况下运行的情况bernoulli_lpmf(x1 | Phi(X1*gamma3))
。
我试图最好地解释我的问题。如果需要进一步澄清,请询问我。
有人可以确定错误发生在哪里吗?
非常感谢您!