How to estimate a latent class mixed model using hlme function

Background and definitions

The linear mixed model assumes that the population of N subjects is homogeneous and described at the population level by a unique profile \(X_{i}(t)\beta\). In contrast, the latent class mixed model consists in assuming that the population is heterogeneous and composed of \(G\) latent classes of subjects characterized by \(G\) mean profiles of trajectories.

The latent class mixed model

Latent class membership is defined by a discrete random variable \(c_{i}\) that equals \(g\) if subject \(i\) belongs to latent class \(g\) (\(g\) = 1, …,\(G\)). The variable \(c_{i}\) is latent; its probability is described using a multinomial logistic model according to covariates \(X_{ci}\):

\(\pi_{ig}= P(c_{i} = g|X_{ci}) = \frac{\exp(\xi_{0g}+X_{ci}\xi_{1g})}{ \sum_{l=1}^{G}\exp(\xi_{0l}+X_{ci}\xi_{1l})}\)

where \(\xi_{0g}\) is the intercept for class \(g\) and \(\xi_{1g}\) is the q1-vector of class-specific parameters associated with the q1-vector of time-independent covariates \(X_{ci}\). For identifiability, \(\xi_{0G} = 0\) and \(\xi_{1G} = 0\). When no covariate predicts the latent class membership, this model reduces to a class-specific probability.

For a continuous and Gaussian variable, the trajectories of \(Y\) are defined conditionally to the latent class by a linear mixed model. So, conditional on class \(g\), the model is defined for subject \(i\) at occasion \(j\):


\[Y_{ij}|_{c_{i}=g} = X_{2ij}\beta+X_{3ij}\gamma_{g}+Z_{ij}b_{i}+\epsilon_{ij}\]


where \(X_{2ij}\), \(X_{3ij}\) and \(Z_{ij}\) are vectors of covariates respectively associated with common fixed effects over classes \(\beta\), class-specific fixed effects \(\gamma_{g}\) and with individual random effects \(b_{i}|_{ci=g}\) called \(b_{ig}\) whose distributions are now class-specific. \(X_{2}\) and \(X_{3}\) can’t have common variables.

Posterior classification

In models involving latent classes, a posterior classification of the subjects in each latent class can be made. It is based on the posterior calculation of the class-membership probabilities and is used to characterize the classification of the subjects as well as to evaluate the goodness-of-fit of the model (Proust-Lima et al. 2014 - ).

Posterior class-membership probabilities are computed using the Bayes theorem as the probability of belonging to a latent class given the information collected. In a longitudinal model, they are defined for subject \(i\) and latent class \(g\) as:



where : \(\hat{\theta}_{G}\) is the vector of parameters estimated in the \(G\) latent class model.



Example for Gaussian Data

In this example, we study the quadratic trajectory of a cognitive marker, the Mini Mental State Examination (MMSE) pre-normalized (to have a Gaussian distribution) in a sample of old people (aged 65 years old and over at inclusion) followed for up to 15 years with an adjustment on education level. The model does not consider interactions here although any regression may be considered.

Model considered

We consider the following latent class linear mixed model, where \(g\) denotes the class, \(i\) the subject and \(j\) the repeated measurement:




Where : \(u_{ig} \sim \mathcal{N}(0,B_{g})\) and \(\epsilon_{ij} \sim \mathcal{N}(0,\sigma^2)\)


The Fixed part is \(\beta_{0g}+\beta_{1g}age_{ij}+\beta_{2g}age_{ij}^2+\beta_{3}CEP_{i}\) with a mixture on \(age_{ij}\) and \(age_{ij}^2\); the random part is \(u_{0ig}+u_{1ig}age_{ij}+u_{2ig}age_{ij}^2\),


Dependent variable: normalized MMSE


As MMSE has a very skewed distribution, we use its normalized version easily obtained from NormPsy package (see Introduction vignette: ).



Estimate the model with only one class (G=1)

Analysis is done according to age. To avoid any numerical problems, we recenter and scale age:


We fit the linear mixed model for normMMSE:


Estimate the model with more than one class (G > 1)

From the model estimated by assuming a single latent class, we can now search for heterogeneous profiles. The next lines provide the estimation of the model for 2 latent classes using the automatic specification for the initial values when \(G>1\).

Be careful with initial values !

Initial values are specified in argument B. The option B=m1 automatically generates initial values from the maximum likelihood estimates of a 1-class model (here, m1). Not specifying B or specifying B=NULL is not recommended since it induces the internal pre-estimation of the model with \(G = 1\) (i.e m1) which may substantially increase computational time.


Values pre-specified by the user

In the following example, the initial values are pre-specified by the user: parameters of the variance covariance were taken at the estimated values of the linear mixed model and arbitrary initial values were tried for the class-specific trajectories:


Values randomly generated

An alternative is to randomly generate the initial values from the asymptotic distribution of the estimates of the 1-class model (here, m1):


Choose the best model


The estimation process of a set of models (usually with a varying number of latent classes) can be summarized with summarytable. Different information can be displayed:



We summarize here the 6 models we previously estimated. We can see that all the 2-class models converged at the same estimation point. Only the order of the classes is changed. This phenomenon called “label switching” often happens. It is not a problem: this is the exact same optimum.

This example illustrates the complexity to define the “optimal number of latent classes”. Indeed, according to recommended BIC, the 2 class model should be retained (as it provides the lowest value). But both the AIC and Size adjusted BIC (which involve smaller penalties) favor the 3-class model. The entropy also favors the 3-class model as having a better discriminatory power (entropy closer to 1). Finally, the 3-class model creates a very very small class, which is usually not the kind of heterogeneity ones searches for and is interested in. In this example, depending on the statistical and clinical criteria, either the 2- or the 3-class model could be retained. Below, we retained the 2-class model for the final output description.


Description of the 2-class linear mixed model

Predictions of the model

Class-specific predictions can be computed for any data contained in a dataframe as soon as all the covariates specified in the model are included in the dataframe. In the next lines, such a dataframe is created by generating a vector of \(age\) values between 65 and 95 and defining \(CEP\) at 1 or 0. The predictions are computed with predictY and plotted with the associated plot functionality or by using standard R tools.



Predictions are computed for each class at the point estimate:



Predictions can then be plotted:



If we want to have a sense of the variability, we can compute the predictions with confidence intervals and plot them:



Finally, the predicted trajectories for 1-, 2- and 3-class models can be represented together in the following graph:



Evaluation of the final latent class mixed model

Plot of the residuals

Graph of the predictions versus observations


In order to evaluate the fit of the selected model, we plot simultaneously the observations and the predicted values for each latent class.


The graph shows here the very good fit to the data.  


The posterior classification of the model is obtained with:



Class 1 is composed of 62 subjects (12.4%), whereas 438 are in the second class.

We can also see information about the discrimination power of the model with:

  • the posterior classification table: subjects classified in class 1 (resp. class 2) have a mean probability of 0.8054 (resp. 0.8730) to belong to this class. This show good discrimination of the classes.

  • the proportion of classified subjects above a threshold: here 90.18% (resp. 61.29%) of subjects in class 1 (resp. 2) are classified in the class with a posterior probability greater than 70%.

Examples with other types of dependent variables

Latent class mixed models can be estimated for other types of data within lcmm R package:

Each time, the same functions (e.g., summarytable, gridsearch, postprob, predictY) apply exactly in the same way.