How can we perform Latent class Analysis for choice based conjoint data, to both segment respondents and determine co-efficients for attributes at the same point of time? I know there is a package poLCA to do this, but haven't seen anyone using that. Any sugestion on package, function or sample code?


1 Answer 1


flexmix would do the job but (so far I remember) only if you model binary (Yes/No) or pairwise (A vs B) choices (Last time I checked the authors were working on an extension to multinomial (MNL) choices)

However, latent class logit (LCL) models are relatively easy to code as they consist in a discrete mixture of standard MNL models (so if you know how to code an MNL model you should be able to write your own LCL code).

Here is an example for a LCL with 2 classes:

X -> Matrix of independent variables (e.g., attributes' levels)
Y -> Column vector of observed choices (0/1)
N -> Column vector of respondents ID (e.g., 1 1 1 1 2 2 2 2 3 3 3 3 ...)
G -> Column vector of observations ID (e.g., 1 1 2 2 3 3 4 4 5 5 6 6 ...)

In this the code, the model specification is quite simple:
- Only 2 latent classes.
- Same set of predictors for the 2 classes (Possible to add some constraints).
- Constant only for class membership (Possible to add some covariates (age, gender, etc)).

loglik.LCL = function(beta, X, Y, N, G){
### Class 1
num1 = exp(as.matrix(X) %*% as.vector(beta[1:ncol(X)]))
den1 = tapply(num1, G, sum)
prb1 = num1[Y==1] / den1
sprb1 = tapply(prb1, N, prod)
### Class 2
num2 = exp(as.matrix(X) %*% as.vector(beta[1+ncol(X):2*ncol(X)]))
den2 = tapply(num2, G, sum)
prb2 = num2[Y==1] / den2
sprb2 = tapply(prb2, N, prod)
### Membership
cla1 = exp(0)
cla2 = exp(beta[1+2*ncol(X)])
CLA = cla1 + cla2
### Log-likelihood
llik = -sum(log(cla1/CLA * sprb1 + cla2/CLA * sprb2))

Remark: Possible to write more efficient version of this code if you have a complete dataset by replacing tapply() by matrix operations (reshape, colSums, etc).

You can compare your results with the "lclogit" Stata command.

  • $\begingroup$ Cool answer @Umka, do you think it is possible to extend this to MNL, in particular using alternative-specific attributes? I have been searching for a package that estimates Latent Class models in R by Expectation-Maximization as it is done by Stata's lclogit (and the enhanced version lclogit2, but so far, I haven't found any. Only gmnl appears to do it, but only by Maximum Likelihood, which have terrible numerical properties when it comes to Latent Class. $\endgroup$
    – TTT
    Mar 15, 2021 at 14:35
  • 1
    $\begingroup$ @ÁlvaroA.Gutiérrez-Vargas Relatively recently a new R package has been developped to analyze choice data - It is called "apollo" (apollochoicemodelling.com/manual.html) - Great package, allowing for wide range of models and possibility to use different optimization algorithms (EM being one of them) - But i did not have a chance yet to try EM-based LCMNL with this package. Manual provides a lot of examples. $\endgroup$
    – Nicolas K
    Mar 16, 2021 at 8:23
  • $\begingroup$ Cool that you pointed in that direction. I was aware of it and waiting for them to update the latest version of the EM-based LCMNL model. Here is where it can be found for future references. $\endgroup$
    – TTT
    Mar 16, 2021 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.