For example, in the election example from the documentation, if I create a new set of answers to the questions, how can I use the poLCA model to tell me what class (cluster) it's most likely to be in?

There doesn't appear to be a function to do this, though the model has a df within it that lists the probabilities of class membership for each value of each manifest variable. I'm tasked with converting some sql code that takes a second dataset and classifies the patients there as members of the clusters created from a first. Superficially this is a programming question. It seems like a function to do this would be a reasonable addition to the package. More deeply, if indeed there isn't such a function, it would become a question about how to use the table of probabilities to classify new data.

If readers aren't familiar with the R package poLCA, it's an LCA package that works with discrete/categorized data.

(full disclosure: I asked on cross-validated and a shorter version of this question was put on hold.)

  • $\begingroup$ Part of the data returned from poLCA tells what probability a particular value of a variable adds to a subject's probability of membership to each class. The appendix S1 to this paper walks through an example calculation. I have some R code that does this at github to implement this. Comments welcome. $\endgroup$
    – ChrisR
    Dec 19, 2018 at 23:56
  • $\begingroup$ ...no code on github from me, as my answer below says, code to apply the model is actually part of the package. $\endgroup$
    – ChrisR
    Apr 21, 2020 at 15:35

2 Answers 2


Using carcinoma data available in the poLCA package and a 4 latent classes solution:

 library poLCA
 f <- cbind(A, B, C, D, E, F, G) ~ 1
 lc4 <- poLCA(f, carcinoma, nclass = 4)

The following line give the classification in terms of predicted probabilities


They could be usefully binded to the original data for further visualizations or analyses

 carcinoma.predclass <- cbind(carcinoma, "Predicted LC" = lc4$predclass)

You could have a new data frame with the same columns/variables used in the previous analysis.

new.data <- data.frame(A=c(1,2,1), B=c(2,2,1), C=c(1,2,1), D=c(1,1,1), E=c(1,2,1), F=c(2,2,1), G=c(1,2,1))

  A B C D E F G
1 1 2 1 1 1 2 1
2 2 2 2 1 2 2 2
3 1 1 1 1 1 1 1

A simple method can be link the observed data patterns in the new data frame with the estimated latent class probabilities in the previous data. In fact, the first pattern has missing prediction because it wasn't in the training data.

left_join(new.data, unique(carcinoma.predclass))

Joining, by = c("A", "B", "C", "D", "E", "F", "G")
  A B C D E F G Predicted LC
1 1 2 1 1 1 2 1           NA
2 2 2 2 1 2 2 2            1
3 1 1 1 1 1 1 1            4

The best method is to use the posterior of poLCA. From the parameters estimated by the latent class model, this function calculates the probability that a specified pattern belongs to each latent class. This function can calculate posterior class membership probabilities for new data, observed or not in the training data.

new.lc4.posterior <- poLCA.posterior(lc4, new.data)

And bind the predicted Latent Classes (the classes which the highest posterior probability) to the new data.

cbind(new.data, "Predicted LC" = apply(new.lc4.posterior,1, FUN=which.max))

  A B C D E F G Predicted LC
1 1 2 1 1 1 2 1            2
2 2 2 2 1 2 2 2            1
3 1 1 1 1 1 1 1            4
  • $\begingroup$ Thanks. I want to create the model from one dataset and apply it to another. I have working code now, that I'll publish shortly. $\endgroup$
    – ChrisR
    Dec 5, 2018 at 23:10
  • 1
    $\begingroup$ Was my answer useful! $\endgroup$ Dec 5, 2018 at 23:15
  • $\begingroup$ The desire is to apply the lc4$probs to a second dataset. Still planning on posting detail. $\endgroup$
    – ChrisR
    Dec 13, 2018 at 18:48
  • 1
    $\begingroup$ I have updated the answer with a solution. Hope it helps! $\endgroup$ Dec 13, 2018 at 23:19
  • $\begingroup$ Thanks, I must have confused lca_model$posterior, the data, with poLCA.posterior() the function. $\endgroup$
    – ChrisR
    Jan 15, 2019 at 23:27

As Paolo says, use the poLCA.poseterior() function. The data comes out in the same format as the lca_model$posterior structure returned by the poLCA function.

column_names <- c('MORALG', 'CARESG', 'KNOWG', 'LEADG', 'DISHONG', 
                  'INTELG', 'MORALB',
                  'CARESB', 'KNOWB', 'LEADB', 'DISHONB', 'INTELB')
election_matrix = as.matrix(mapply(as.numeric,election[,column_names]))
election_matrix_no_na =election_matrix[apply(election_matrix, 1,  
    function(x) all(is.finite(x))   ),]
preds =  poLCA.posterior(lc=lca_model, y=election_matrix_no_na)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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