analysis, visualisation and playing around with data

Example for a latent class analysis with the poLCA-package in R

When you work with R for some time, you really start to wonder why so many R packages have some kind of pun in their name. Intended or not, the poLCA package is one of them. Today i´ll give a glimpse on this package, which doesn´t have to do anything with dancing or nice dotted dresses.

This article is kind of a draft and will be revised anytime.

The „poLCA“-package has its name from „Polytomous Latent Class Analysis“. Latent class analysis is an awesome and still underused (at least in social sciences) statistical method to identify unobserved groups of cases in your data. Polytomous latent class analysis is applicable with categorical data. The unobserved (latent) variable could be different attitude-sets of people which lead to certain response patterns in a survey. In marketing or market research latent class analysis could be used to identify unobserved target-groups with different attitude structures on the basis of their buy-decisions. The data would be binary (not bought, bought) and depending on the products you could perhaps identify a class which chooses the cheapest, the most durable, the most environment friendly, the most status relevant […] product. The latent classes are assumed to be nominal. The poLCA package is not capable of sampling weights, yet.

By the way: There is also another package for latent class models called „lcmm“ and another one named „mclust„.

What does a latent class analysis try to do?
A latent class model uses the different response patterns in the data to find similar groups. It tries to assign groups that are „conditional independent“. That means, that inside of a group the correlations between the variables become zero, because the group membership explains any relationship between the variables.

Latent class analysis is different from latent profile analysis, as the latter uses continous data and the former can be used with categorical data.
Another important aspect of latent class analysis is, that your elements (persons, observations) are not assigned absolutely, but on probability. So you get a probability value for each person to be assigned to group 1, group 2, […], group k.

Before you estimate your LCA model you have to choose how many groups you want to have. You aim for a small number of classes, so that the model is still adequate for the data, but also parsimonious.
If you have a theoretically justified number of groups (k) you expect in your data, you perhaps only model this one solution. A typical assumption would be a group that is pro, one group contra and one group neutral to an object. Another, more exploratory, approach would be to compare multiple models – perhaps one with 2 groups, one with 3 groups, one with 4 groups – and compare these models against each other. If you choose this second way, you can decide to take the model that has the most plausible interpretation. Additionally you could compare the different solutions by BIC or AIC information criteria. BIC is preferred over AIC in latent class models, but usually both are used. A smaller BIC is better than a bigger BIC. Next to AIC and BIC you also get a Chi-Square goodness of fit.
I once asked Drew Linzer, the developer of PoLCA, if there would be some kind of LMR-Test (like in MPLUS) implemented anytime. He said, that he wouldn´t rely on statistical criteria to decide which model is the best, but he would look which model has the most meaningful interpretation and has a better answer to the research question.

Latent class models belong to the family of (finite) mixture models. The parameters are estimated by the EM-Algorithm. It´s called EM, because it has two steps: An „E“stimation step and a „M“aximization step. In the first one, class-membership probabilities are estimated (the first time with some starting values) and in the second step those estimates are altered to maximize the likelihood-function. Both steps are iterative and repeated until the algorithm finds the global maximum. This is the solution with the highest possible likelihood. That´s why starting values in latent class analysis are important. I´m a social scientist who applies statistics, not a statistician, but as far as i understand this, depending on the starting values the algorithm can stop at point where one (!) local maximum is reached, but it might not be the „best“ local maximum (the global maximum) and so the algorithm perhaps should´ve run further. If you run the estimation multiple times with different starting values and it always comes to the same solution, you can be pretty sure that you found the global maximum.

data preparation
Latent class models don´t assume the variables to be continous, but (unordered) categorical. The variables are not allowed to contain zeros, negative values or decimals as you can read in the poLCA vignette. If your variables are binary 0/1 you should add 1 to every value, so they become 1/2. If you have NA-values, you have to recode them to a new category. Rating Items with values from 1-5 should could be added a value 6 from the NAs.

R

1

mydata[is.na(mydata)]<-6

Running LCA models
First you should install the package and define a formula for the model to be estimated.

R

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

install.packages("poLCA")

library("poLCA")

# By the way, for all examples in this article, you´ll need some more packages:

# Generate a HTML-TABLE and show it in the RSTUDIO-Viewer (for copy & paste)

view_kable<-function(x,...){

tab<-paste(capture.output(kable(x,...)),collapse='\n')

tf<-tempfile(fileext=".html")

writeLines(tab,tf)

rstudio::viewer(tf)

}

view_kable(lca_results,format='html',table.attr="class=nofluid")

# Another possibility which is prettier and easier to do:

install.packages("ztable")

ztable::ztable(lca_results)

Elbow-Plot
Sometimes, an Elbow-Plot (or Scree-Plot) can be used to see, which solution is parsimonius and has good fit-values. You can get it with this ggplot2-code i wrote:

R

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

# plot 1

# Order categories of results$model in order of appearance

install.packages("forcats")

library("forcats")

results$model<-as_factor(results$model)

#convert to long format

results2<-tidyr::gather(results,Kriterium,Guete,4:7)

results2

#plot

fit.plot<-ggplot(results2)+

geom_point(aes(x=Model,y=Guete),size=3)+

geom_line(aes(Model,Guete,group=1))+

theme_bw()+

labs(x="",y="",title="")+

facet_grid(Kriterium~.,scales="free")+

theme_bw(base_size=16,base_family="")+

theme(panel.grid.major.x=element_blank(),

panel.grid.major.y=element_line(colour="grey",size=0.5),

legend.title=element_text(size=16,face='bold'),

axis.text=element_text(size=16),

axis.title=element_text(size=16),

legend.text=element_text(size=16),

axis.line=element_line(colour="black"))# Achsen etwas dicker

# save 650 x 800

fit.plot

Inspect population shares of classes
If you are interested in the population-shares of the classes, you can get them like this:

R

1

2

round(colMeans(lc$posterior)*100,2)

[1]27.9240.1331.95

or you inspect the estimated class memberships:

R

1

2

3

4

5

6

7

table(lc$predclass)

123

158234185

round(prop.table(table(lc$predclass)),4)*100

123

27.3840.5532.06

Ordering of latent classes
Latent classes are unordered, so which latent class becomes number one, two, three… is arbitrary. The latent classes are supposed to be nominal, so there is no reason for one class to be the first. You can order the latent classes if you must. There is a function for manually reordering the latent classes: poLCA.reorder()
First, you run a LCA-model, extract the starting values and run the model again, but this time with a manually set order.

Thank you very much for your comment! Of course you´re right, the default-plot is fine. It´s just that i usually try to avoid 3D-visualisations, because they might bias the perception of the bars´ heights.

Hey Ana,
sorry for my late reply. Yes, it´s possible to do mixture models with covariates or a mixture regression model in poLCA. In the example above i define the model like this:
# define function
f< -with(mydata, cbind(var1,var2,var3...)~1)

I thought that i needed covLCA for what I am asking, something that affects the manifests variables but not the latent variable. I am building something across time and I need to make like a group or something for the years of the survey (because I have different sizes of samples). But, covLCA did not work. I am still looking.

Dear Simon,
I am analyzing data with poLCA right now, and your article really precious.
but i have a problem when i tried to immitate the elbow-plot, mine are 10class and Model 10 comes after Model 1, so it was not respectively appeared in the plot..(1,10,2,3,4,5,6,7,8,9). Do you have any sugesstion how to fix this problem?
thanks a lot..

Hey Dian,
that problem occured because „results2$model“ was of type character and not a factor. Thanks to Hadley Wickhams package forcats() we can convert results2$model to a factor where the categories are in the order of appearance.

I don´t think that´s normal.
I tried it with the example-data i just posted and get entropy results for model 2 – 10. If your code is exactly the same as mine, i think there´s some data related problem. Perhaps having a look at lc4$P (the class proportions) and lc4$posterior will help figuring this out.
best

Thank you very much for sharing your knowledge on this blog. It helps me to save time.
Now, I am wondering wthether it possible to introduce both categorical (dichotomous) data and continuous variables in latent class analysis. If I correctly understood your draft, poLCA cannot do this.
Best,
Florian

Hi Florian,
you´re right, poLCA can´t use continous data. It´s only suitable for latent class analysis (observed categorical variables, unobserved categorical variables), not latent profile analysis (observed continous variables, unobserved categorical variables). Latent profile analysis has the same aim as latent class analysis: Finding unobserved segments of cases. But different to latent class analysis the observed values aren´t categorical but continous.
Of course, if you want to stay with poLCA, you could cut you continous data in categories of equal size, but that would mean a loss of information. So, i think you would have to switch to mclust() or another package that is capable of that.
This paper might be a good starting point: Oberski: Mixture models: latent profile and latent class analysis

You can access this information with lc$predclass where lc is the LCA-Model you estimated via poLCA().

BUT: Be aware that LCA is a probabilistic way assigning the classes. Every observation has a probability to belong to each of the classes, which you can inspect with lc$posterior. The lc$predclass-thing just assigns the class with the highest probability. Bonus-Info: Thats why, if you calculate the shares of the predicted classes (prop.table(table(lc$predclass)) they will differ from the estimated population share (colMeans(lc$posterior)*100 .

thanks Neils !!!
I appreciate your help:).
can u tell how to validate LCA model,
i have split the original data 80:20 (train :test )…
built model on train data
which function in poLCA does this purpose….
also wat would ensure classes formed would be stable???

Sorry for my late reply. In my blogpost i recommend to estimate each model multiple times with different starting values so that you can be pretty sure that the algorithm found the best solution. The precision of the classification can be inspected through the entropy-statistic. It is near zero if the classification is no better than random with 1 beeing the opposite. Which model describes your data the best depends on model fit criteria and if the classes make sense (interpretation). There is also k-fold-cross-validation for this purpose.

Cross Validation could be used for your problem as well. If the model you estimated with your randomly selected training data gets a comparable good fit with the test data, the model should be appropriate here as well. There is also two-fold (or k-fold) crossvalidation you could have a look at. Would love to hear feedback on how you solved your problem.

Thank you for your wonderful codes and it has been very helpful for me! I do have a question: in your post I assume you used observed categorical variables in the analysis, so how about unobserved variables?

For example, I have multiple categorical outcome variables (y1, y2, y3, …, y27) and I want to group them into 6 factors (f1, f2, …, f6). I know I can create sum or average scores of these outcome variables but that will make me lose the variances. So how can poLCA handle these unobserved factors? Can you provide some codes?

Hi Chao,
i´m not sure i understood your question completely. A Latent class analysis tries to find subtypes of related cases in that way, that the assigned group explains the correlations among the observed variables. While an exploratory factor analysis tries to find variables that belong together (i.e. you have a scale of 10 variables and PCA finds 2 principal components explaining X % of the variance), LCA tries to find cases that belong together. I just remembered this website, that explains how LCA compares to other methods http://www.john-uebersax.com/stat/faq.htm#otherm.

I know I can create sum or average scores of these outcome variables but that will make me lose the variances. So how can poLCA handle these unobserved factors?

Perhaps i don´t get this right, but to me, this sounds more like an factor analysis approach, where you make average scores of all variables of one factor. In latent class analysis you have the conditional class probabilities, where each observation has a probability to belong to each group. Perhaps you can help me understand your goal a bit better 🙂

Thank you for your response and sorry for any confusion. What I am trying to do is more like a two-step procedure: first a factor analysis and second a latent class analysis on those factors derived from the first step. So I wonder if there is any way that I can take those factors derived from factor analysis and put them into the latent class analysis. I thought about using lavaan to run a cfa first and take the resulted factors from lavaan to run a latent class analysis in poLCA but couldn’t figure out how. I hope this makes sense to you. Maybe I totally misunderstood what LCA is capable of doing but does it have to run on manifest variables?

Hi Chao,
thanks, that cleared things up for me. First: Yes, LCA runs on categorical manifest variables and tries to find the values of categorical latent variables.
Considering your goal, i have some ideas, but they are more or less quesses what you could do and i´m not sure if they are methodologically adequate. Ok, so you want to reduce the data through factor analysis and then run a LCA to see the structure of your cases. The problem is, that factors are continous and an LCA uses categorical data, so a latent profile analysis would be more adequate. In latent profile analysis the observed data is continous and the latent is categorical. PoLCA is only capable of LCA. But you could also use cut() and make categories from the continous data, loosing some information, of course.
In the first step, you would fit the cfa()-model, then you would save the predicted factor scores (Perhaps like this https://groups.google.com/d/msg/lavaan/E4NPoUiKsks/5IYLv5ggAAAJ), make them categorical and then run the LCA on them. But i´m not sure about the interpretation of the results.

Thank you for your suggestion! I will try the approach you provided. Just a quick follow-up question: my observed variables are categorical and I guess factor analysis will make them continuous in the end?

Hi, as you surely know, factor analysis normally needs continuous data. But in the social sciences it is often ok to use items that have equidistant scales with at least 5 categories (likert-type items). There are also other assumptions like normality. Latent class analysis has the advantage of beeing a nonparametric method without such assumptions. TL;DR: In a way your categorical data will lead to continous factors, yeah 🙂
Best,
Niels

Hi Niels,
Recently I tried to do LCA in Latent Gold, and there are several outputs, such as p value (to measure the difference between model and our data–when we perform goodness of fit test, such as likelihood ratio) that not produce in poLCA output.
Also, for example, when I perform 2 class analysis and we see the lc2$predcell results, we will see the observed vs expected value for each combination of variables (ie: F09_a (1), F09_b (1), F09_c(1), F27_a (1), F27_e(1), F27_e(1), F29_a (1), F29_b(1), F29_c(1), but there is no two additional information just like Latent Gold provide which is the probability of that combination belong to class 1 or 2. Do you know how to produce this probability value?

Hi Dian, i’m afraid poLCA doesn’t do likelihood ratio tests. I know MPlus and latent gold have them to compare models, but poLCA lacks this function. I guess this can be done somehow, but haven’t seen it on the internet, yet. Sorry.

I did a latent class analysis, which gave a best fit for 3 classes. Now I want to use these classes and do a multinomial logistic regression. I read that I have to use flexmix package. But I am not sure really how that works.
Do you have any tips?

Or is poLCA package sufficient enough to do a regression analysis with the 3 classes by adding the covariates?

I want to add demographical variables to the classes, so i can see which X variable predicts the membership of a certain class.

Hi,
here´s what the vignette says about it (https://cran.r-project.org/web/packages/poLCA/poLCA.pdf):
„The term „Latent class regression“ (LCR) can have two meanings. In this package, LCR models refer to latent class models in which the probability of class membership is predicted by one or more covariates. However, in other contexts, LCR is also used to refer to regression models in which the manifest variable is partitioned into some specified number of latent classes as part of estimating the regression model. It is a way to simultaneously fit more than one regression to the data when the latent data partition is unknown. The flexmix function in package flexmix will estimate this other
type of LCR model. Because of these terminology issues, the LCR models this package estimates are sometimes termed „latent class models with covariates“ or „concomitant-variable latent class analysis,“ both of which are accurate descriptions of this model.“

I was thinking instead of doing a regression,
I could simply compute two-way tables summarizing the class membership probabilities
per covariate category (e.g., for males and females, for educational levels, for
age groups).

Mhm, interesting… do have you a source, where you read about this analyis-step? You could compare entropy of two models, where one model omits a variable. But my approach is mostly to have a latent class model that makes sense from a theoretical point of view and criteria like maximization of entropy are of less importance to me. This seems to be different in your case, but i´m afraid i´m not of use here.