我正在尝试编写一个用于建模多粒度主题模型的Winbugs/Jags模型(正是本文-> http://www.ryanmcd.com/papers/mg_lda.pdf)
在这里,我想根据一个特定值选择一个不同的分布。例如:我想做像这样的事情
`if ( X[i] > 0.5 )
{
Z[i] ~ dcat(theta-gl[D[i], 1:K-gl])
W[i] ~ dcat(phi-gl[z[i], 1:V])
}
else
{
Z[i] ~ dcat(theta-loc[D[i], 1:K-loc])
W[i] ~ dcat(phi-loc[z[i], 1:V])
}
`这可以在Winbugs/JAGS中完成吗?
发布于 2013-12-12 05:13:25
Winbugs/JAGS不是一种过程化语言,所以您不能使用这样的结构。使用step函数。引用手册中的内容:
第(E)步......如果e >=为0,则为1;否则为0
所以你需要一个小窍门来改变这个条件:
X[i] > 0.5 <=>
X[i] - 0.5 > 0 <=>
!(X[i] - 0.5 <= 0) <=>
!(-(X[i] - 0.5) >= 0) <=>
!(step(-(X[i] - 0.5)) == 1) <=>
step(-(X[i] - 0.5)) == 0然后使用下面的索引技巧:
# then branch
Z_branch[i, 1] ~ dcat(theta-gl[D[i], 1:K-gl])
W_branch[i, 1] ~ dcat(phi-gl[z[i], 1:V])
# else branch
Z_branch[i, 2] ~ dcat(theta-loc[D[i], 1:K-loc])
W_branch[i, 2] ~ dcat(phi-loc[z[i], 1:V])
# decision here
if_branch[i] <- 1 + step(-(X[i] - 0.5)) # 1 for "then" branch, 2 for "else" branch
Z[i] ~ Z_branch[i, if_branch[i]]
W[i] ~ W_branch[i, if_branch[i]]https://stackoverflow.com/questions/15414303
复制相似问题