How to code latent class model w/ > 2 classes

I have a latent class model with 2 latent classes. I want to make a general code for c classes. I am not sure how to do it though - I know I have to use categorical_lpmf() and have made an attempt below but it doesn’t work (gives nonsensical results).

Here is the code snippet:

          for (n in 1:N) {
              vector[n_class] lp;
              matrix[n_class, n_tests] y1; 

       ... does stuff with y1 ....

                if (n_class == 2) { 
                  lp[1]  =  sum(y1[1,]) + bernoulli_lpmf(1 |  p); 
                  lp[2]  =  sum(y1[2,]) + bernoulli_lpmf(0 |  p); 
                } else { // > 2 classes 
                   lp =  y1 * rep_vector(1, n_tests) + categorical_lpmf(vector_classes | probs_classes); // attempt (doesnt work)
                }
                 log_lik[n] =  log_sum_exp(lp); 

            }

EDIT: nevermind I think I got it. I still used bernoulli_lpmf but with a loop over the classes and using each element of probs_classes as the probability.

Hi

If you got a general code working, would you ind sharing the full Stan code?

thanks

I am still doing some testing I do still get some weird results… can someone confirm that the Jacobian adjustment and way I have manually coded the ordered vectors is right? (i.e. equivalent to declaring N ordered vectors in Stan?

Edit: nevermind it works now. Will post full model code soon.

I see, I asked because we have been working through another way to code LCA with categorical idnicators in this thread mixture moidels

1 Like