Skip to main content
. Author manuscript; available in PMC: 2019 May 1.
Published in final edited form as: Multivariate Behav Res. 2018 Feb 9;53(3):430–451. doi: 10.1080/00273171.2018.1428892

Table 3.

A comparison between the equations for Gibbs sampling and how they are implemented in R. The last column covers the primary R functions that carry out the sampling, which essentially involve feeding these functions the appropriate input parameters. For example, sampling from the Dirichlet posterior involve summing the number of people within each latent class and then combining the sums with the prior sample sizes.

Equation for Gibbs sampling Line(s) R function
πj(t+1)~Dirichlet(i=1Nci1(t)+u1,,i=1NciC(t)+ug) (10) 94 pi.tl <- rdirichlet(n = 1, alpha = colSums(Cl.tl) + dirich.prior)
pjk(t+1)~Beta(i=1Nyicij(t)+αjm,i=1N(1yi)cij(t)+βjm) (11) 97 – 101 pkj.tl[g, ] <- rbeta(K, shapel = alpha + colSums(Cl.tl [, g] * y), shape2 = beta + colSums(Cl.tl [, g] * (1 - y) )
ci(t+1)~Multinomial(1,π1(t+1)p(yi|p1(t+1))h=1Jπh(t+1)p(yi|ph(t+1)),,πJ(t+1)p(yi|pJ(t+1))h=1Jπh(t+1)p(yi|ph(t+1))) (12) 85 – 87 Cl.tl <- apply(Clp.tl, 1, function(prob) { rmultinom(n = 1, size = 1, prob = prob) })