# How do I use the model generated by the R package poLCA to classify new data as belonging to one of the classes?

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.)

• 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. Dec 19, 2018 at 23:56
• ...no code on github from me, as my answer below says, code to apply the model is actually part of the package. Apr 21, 2020 at 15:35

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

 library poLCA
data(carcinoma)
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

 lc4$predclass  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

• 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. Dec 5, 2018 at 23:10
• Was my answer useful! Dec 5, 2018 at 23:15
• The desire is to apply the lc4$probs to a second dataset. Still planning on posting detail. Dec 13, 2018 at 18:48 • I have updated the answer with a solution. Hope it helps! Dec 13, 2018 at 23:19 • Thanks, I must have confused lca_model$posterior, the data, with poLCA.posterior() the function. 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.

library(poLCA)
data(election)
column_names <- c('MORALG', 'CARESG', 'KNOWG', 'LEADG', 'DISHONG',
'INTELG', 'MORALB',