本文介绍了根据 WinBugs/JAGS 中的 if - else 条件选择不同的发行版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 Winbugs/Jags 模型来对多粒度主题模型进行建模(正是这篇论文 -> http://www.ryanmcd.com/papers/mg_lda.pdf)

I am trying to write a Winbugs/Jags model for modeling multi grain topic models (exactly this paper -> http://www.ryanmcd.com/papers/mg_lda.pdf)

在这里,我想根据特定值选择不同的分布.例如:我想做类似的事情

Here I would like to choose a different distribution based on a particular value.For Eg: I would like to do something like

`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 中完成吗?

Is this possible to be done in Winbugs/JAGS?

推荐答案

Winbugs/JAGS 不是过程语言,因此您不能使用这样的结构.使用 step 功能.引自手册:

Winbugs/JAGS is not a procedural language, so you cannot use the construct like that. Use step function. Quote from the manual:

step(e) ...... 1 if e >= 0;0 否则

所以你需要一个技巧来改变条件:

So you need a trick to change the condition:

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

然后将其用于索引技巧:

and then use this for indexing trick:

# 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]]

这篇关于根据 WinBugs/JAGS 中的 if - else 条件选择不同的发行版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 12:21