From the general problem to its implementation in JAGS

The most general problem would be to evaluate the joint conditional probability of the uncertain (`unobserved') quantities, conditioned by the `observed' (`known'/ `assumed'/`postulated') ones, that is, in this case46 (see Appendix A),
$\displaystyle f(p,n_I,n_{NI},n_{P_I},n_{P_{NI}},\pi_1,\pi_2\,\vert\,n_P,n_s,r_1,s_1,r_2,s_2)\,,$     (79)

although in practice we are indeed interested in $f(p\,\vert\,n_P,n_s,r_1,s_1,r_2,s_2)$, and perhaps in $f(n_I\,\vert\,n_P,n_s,r_1,s_1,r_2,s_2)$ and $f(n_{P_I}\,\vert\,n_P,n_s,r_1,s_1,r_2,s_2)$. This is done marginalizing Eq. ([*]) i.e. summing (or integrating, depending on their nature) over the variables on which we are not interested (see Appendix A). As commented in the same appendix, Eq. ([*]) is obtained, apart from a normalization factor, from
$\displaystyle f(p,n_I,n_{NI},n_{P_I},n_{P_{NI}},\pi_1,\pi_2,n_P,n_s,r_1,s_1,r_2,s_2),$     (80)

and the latter from a properly chosen chain rule. The steps used to build up Eq. ([*]) by the proper chain rule are exactly the instructions given to JAGS to set up the model, if we start from the bottom of the diagram of Fig. [*] and ascend through the parents (see Sec. [*]):
model {
  nP ~ sum(nP.I, nP.NI)
  nP.I ~ dbin(pi1, n.I)
  nP.NI ~ dbin(pi2, n.NI)
  pi1 ~ dbeta(r1, s1)
  pi2 ~ dbeta(r2, s2)
  n.I ~ dbin(p, ns)
  n.NI <- ns - n.I
  p ~ dbeta(r0,s0)       
}
The differences with respect to the JAGS model of Sec. [*] are Hereafter we proceed using, very conveniently, JAGS, showing in Sec. [*] the steps needed from writing down the chain rule till the exact evaluation of $f(p)$ after marginalization.