【发布时间】:2018-11-27 00:54:12
【问题描述】:
我正在尝试在 Stan 中复制 John Kruschke 的“做贝叶斯分析”(第 676 页)中的有序概率 JAGS 模型:
JAGS 型号:
model {
for ( i in 1:Ntotal ) {
y[i] ~ dcat( pr[i,1:nYlevels] )
pr[i,1] <- pnorm( thresh[1] , mu , 1/sigma^2 )
for ( k in 2:(nYlevels-1) ) {
pr[i,k] <- max( 0 , pnorm( thresh[ k ] , mu , 1/sigma^2 )
- pnorm( thresh[k-1] , mu , 1/sigma^2 ) )
}
pr[i,nYlevels] <- 1 - pnorm( thresh[nYlevels-1] , mu , 1/sigma^2 )
}
mu ~ dnorm( (1+nYlevels)/2 , 1/(nYlevels)^2 )
sigma ~ dunif( nYlevels/1000 , nYlevels*10 )
for ( k in 2:(nYlevels-2) ) { # 1 and nYlevels-1 are fixed, not stochastic
thresh[k] ~ dnorm( k+0.5 , 1/2^2 )
}
}
到目前为止,我有以下运行,但没有产生与书中相同的结果。 斯坦模型:
data{
int<lower=1> n; // number of obs
int<lower=3> n_levels; // number of categories
int y[n]; // outcome var
}
parameters{
real mu; // latent mean
real<lower=0> sigma; // latent sd
ordered[n_levels] thresh; // thresholds
}
model{
vector[n_levels] pr[n];
mu ~ normal( (1+n_levels)/2 , 1/(n_levels)^2 );
sigma ~ uniform( n_levels/1000 , n_levels*10 );
for ( k in 2:(n_levels-2) ) // 1 and nYlevels-1 are fixed, not stochastic
thresh[k] ~ normal( k+0.5 , 1/2^2 );
for(i in 1:n) {
pr[i, 1] = normal_cdf(thresh[1], mu, 1/sigma^2);
for (k in 2:(n_levels-1)) {
pr[i, k] = max([0.0, normal_cdf(thresh[k], mu, 1/sigma^2) - normal_cdf(thresh[k-1], mu, 1/sigma^2)]);
}
pr[i, n_levels] = 1 - normal_cdf(thresh[n_levels - 1], mu, 1/sigma^2);
y[i] ~ categorical(pr[i, 1:n_levels]);
}
}
数据在这里:
list(n = 100L, n_levels = 7, y = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L, 7L))
应该恢复 1.0 的 mu 和 2.5 的 sigma。相反,我得到了 3.98 的 mu 和 1.25 的 sigma。
我确定我在 Stan 模型中做错了什么,但我还是个初学者,不知道下一步该做什么。谢谢!
【问题讨论】:
-
要检查的一个基本事项是您是否正确指定了正态分布 - “与 JAGS 不同,Stan 根据均值和标准差定义正态分布,而不是均值和精度”(来自:ling.uni-potsdam.de/~vasishth/JAGSStanTutorial/…),所以你想做
normal(mean, sd)而不是normal(mean, 1 / sd^2)。 -
您还必须注意这些模型的可识别性。你不能有一个截距和完全不同的切点。您是否运行了多个链并获得了接近 1 的 Rhat 和不错的有效样本量?
-
谢谢@Marius!我发布了一个新模型作为使用您的建议的答案。
-
谢谢@BobCarpenter!我发布了一个新模型作为使用您的建议的答案。