--- title: "Auxiliary Analyses for Latent Classes" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Auxiliary Analyses for Latent Classes} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include=FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = requireNamespace("OpenMx", quietly = TRUE), out.width = "100%" ) run_everything <- isTRUE(as.logical(toupper(Sys.getenv("TIDYSEM_RUN_EVERYTHING", "false")))) show_results <- isTRUE(as.logical(toupper(Sys.getenv("TIDYSEM_SHOW_RESULTS", "false")))) run_openmx <- requireNamespace("OpenMx", quietly = TRUE) if (!run_openmx) run_everything <- show_results <- FALSE options(scipen = 1, digits = 2) if (show_results) knitr::opts_chunk$set(results = "markup") ``` Latent class analysis (LCA) and latent profile analysis (LPA) identify unobserved groups using a set of observed indicator variables. Often, researchers also have research questions about variables that were not used to define the classes. These are called *auxiliary variables*. A distal outcome is an auxiliary variable considered as an outcome; other auxiliary variables may be predictors or covariates. This vignette introduces three approaches in `tidySEM`: **BCH**, **two-step maximum likelihood**, and **pseudo-class draws**. We illustrate class-specific means, category probabilities, regressions, hypothesis tests, and predictors of class membership. For an introduction to estimating the measurement model, see [Introduction to Latent Class Analysis](https://cjvanlissa.github.io/tidySEM/articles/lca_introduction.html). ## The Naive Approach An often-heard question is: Why can we not simply assign every participant to their most likely class, and use class membership as a variable in auxiliary analyses? The reason is that latent class membership is uncertain, and based on posterior class probabilities. A participant with posterior class probabilities $(.05,.93,.02)$ is most likely to belong to class 2, with high certainty. However, a participant with posterior class probabilities $(.45,.35,.20)$ is most likely to belong to class 1, but there is a lot of uncertainty with this assignment. If we treat class assignments as an observed variable, this uncertainty propagates into auxiliary analyses and biases the results and their standard errors. The three approaches below address this uncertainty in different ways. ## Example: Caregiver profiles We use the `zegwaard_carecompass` data included in `tidySEM`. As explained in the vignette on [Confirmatory LCA](https://cjvanlissa.github.io/tidySEM/articles/lca_confirmatory.html), the measurement model uses four caregiver-experience indicators. For the present analyses, auxiliary variables concern the caregiver and the person receiving care. One requirement of all analyses presented here is that the auxiliary data must have the same number of rows in **exactly the same order** as the measurement data. ```{r data} library(tidySEM) df <- zegwaard_carecompass indicators <- c("burdened", "trapped", "negaffect", "loneliness") measurement_data <- df[, indicators] ``` For the examples in this vignette, we will just fit a simple two-class model: ```{r fit-model-code, eval=FALSE} set.seed(297) lpa_model <- mx_profiles(measurement_data, classes = 2) ``` ```{r fit-model, include=FALSE, eval=run_openmx && (run_everything || show_results)} set.seed(297) lpa_model <- tidySEM:::run_cached(mx_profiles(measurement_data, classes = 2)) ``` ```{r fit-results, eval=show_results && run_openmx} table_fit(lpa_model) ``` ### Classification uncertainty ```{r classification-code, eval=FALSE} cp <- class_prob(lpa_model) head(cp$individual) cp$mostlikely.class ``` ```{r classification-results, echo=FALSE, eval=show_results && run_openmx} cp <- class_prob(lpa_model) format_numeric(head(cp$individual)) format_numeric(cp$mostlikely.class) ``` This table shows the average probability that individuals belong to each true class (rows), and their probability of being assigned to each class (columns). The diagonal of this table thus represents the probability that observations in each class will be correctly classified. These probabilities are high, but not perfect. As also indicated in the fit table by a low Entropy criterion, these classes are not perfectly separated. Therefore, do not treat assigned class as an observed variable; these classification errors would influence downstream auxiliary analyses. ## BCH: class differences in distal outcomes The Bolck–Croon–Hagenaars (BCH) procedure first estimates the measurement model, then estimates an auxiliary multi-group model, accounting for classification uncertainty by weighting case contributions to each class' group. In `tidySEM`, `BCH()` can compare classes on a single numeric or ordered outcome, or an entire auxiliary structural equation model. ### Continuous distal outcome Here we compare caregiver distance across classes. The default numeric-outcome specification estimates class-specific means and variances. ```{r bch-mean-code, eval=FALSE} bch_mean <- BCH(lpa_model, data = df$distance) table_results(bch_mean, columns = NULL) ``` ```{r bch-mean-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} bch_mean <- tidySEM:::run_cached(BCH(lpa_model, data = df$distance)) ``` ```{r bch-mean-results, echo=FALSE, eval=show_results && run_openmx} table_results(bch_mean, columns = NULL) ``` The estimates describe class-specific distal means. They do **not** by themselves test whether the means differ. For an omnibus and pairwise comparison, use `lr_test()`; compare the means (`M`) rather than the entire auxiliary model. ```{r bch-lr-code, eval=FALSE} lr_test(bch_mean, compare = "M") ``` ```{r bch-lr-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} bch_lr <- tidySEM:::run_cached(lr_test(bch_mean, compare = "M")) ``` ```{r bch-lr-results, echo=FALSE, eval=show_results && run_openmx} bch_lr ``` One crucial limitation of the BCH approach is that its standard errors are not valid, so be careful drawing inferences about parameters in these distal models. ### Binary or ordinal distal outcome For a binary distal outcome, `BCH()` estimates class-specific thresholds. The function `table_prob()` represents these in the more interpretable response probability scale: ```{r bch-ordinal-code, eval=FALSE} sex <- ordered(df$sexpatient, levels = c("male", "female")) bch_sex <- BCH(lpa_model, data = sex) table_prob(bch_sex) ``` ```{r bch-ordinal-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} sex <- ordered(df$sexpatient, levels = c("male", "female")) bch_sex <- tidySEM:::run_cached(BCH(lpa_model, data = sex)) ``` ```{r bch-ordinal-results, echo=FALSE, eval=show_results && run_openmx} table_prob(bch_sex) ``` ### A class-specific regression model The `model` argument accepts lavaan-style syntax. This example investigates whether the classes differ in the effect of distance on frequency of visits. If we request the `"matrix"` column of the output, you can see that the regression coefficients live in the `"A"` matrix (asymmetric effects, in other words: regressions). A likelihood ratio test of the `"A"` matrix thus investigates whether the regression coefficient differs significantly between the two classes: ```{r bch-regression-code, eval=FALSE} df_aux <- df[, c("freqvisit", "distance"), drop = FALSE] df_aux$freqvisit <- as.numeric(df_aux$freqvisit) bch_reg <- BCH(lpa_model, model = "freqvisit ~ distance", data = df_aux) table_results(bch_reg, columns = c("label", "est_sig", "se", "pval", "confint", "group", "matrix")) lr_test(bch_reg, compare = "A") ``` ```{r bch-regression-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} df_aux <- df[, c("freqvisit", "distance"), drop = FALSE] df_aux$freqvisit <- as.numeric(df_aux$freqvisit) bch_reg <- tidySEM:::run_cached(BCH(lpa_model, model = "freqvisit ~ distance", data = df_aux)) ``` ```{r bch-regression-lr-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} bch_reg_lr <- tidySEM:::run_cached(lr_test(bch_reg, compare = "A")) ``` ```{r bch-regression-results, echo=FALSE, eval=show_results && run_openmx} table_results(bch_reg, columns = NULL) bch_reg_lr ``` ## Two-step maximum likelihood A different approach to auxiliary analysis is the `two_step()` procedure. It first estimates the latent class model, then fixes its parameters and adds class-specific auxiliary models (whose parameters are free). This prevents the auxiliary model from influencing the class solution. Unlike BCH, the auxiliary outcomes enter the likelihood directly; posterior class probabilities can therefore change even though measurement parameters stay fixed. The interface of this function is similar to `BCH()`: ```{r two-step-code, eval=FALSE} two_mean <- two_step(lpa_model, data = df$distance) table_results(two_mean, columns = NULL) ``` ```{r two-step-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} two_mean <- tidySEM:::run_cached(two_step(lpa_model, data = df$distance)) ``` ```{r two-step-results, echo=FALSE, eval=show_results && run_openmx} table_results(two_mean, columns = NULL) ``` Ordinary standard errors for the auxiliary model are also not valid, but `tidySEM` replaces them with corrected two-step standard errors. These are available in the attribute `attr(model, "se_robust")`, and `table_results()` shows them by default. ```{r two-step-se-code, eval=FALSE} attr(two_mean, "se_robust") ``` ```{r two-step-se-results, echo=FALSE, eval=show_results && run_openmx} attr(two_mean, "se_robust") ``` For a custom auxiliary RAM model, the syntax can include multiple outcomes and regressions: ```{r two-reg-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} two_reg <- tidySEM:::run_cached(two_step( lpa_model, model = "freqvisit ~ distance", data = df_aux )) ``` ```{r echo = TRUE, eval = FALSE} two_reg <- two_step( lpa_model, model = "freqvisit ~ distance", data = df_aux ) table_results(two_reg) ``` ## Pseudo-class draws and pooling The pseudo-class approach accounts for classification uncertainty by repeatedly drawing one class for each observation according to its posterior probabilities. Thus, a person with posterior probabilities $(.05,.93,.02)$ will be assigned to class 2 in *most* replications, but occasionally to class 1 or 3. Conversely, a participant with posterior class probabilities $(.45,.35,.20)$ will be assigned to each of the three classes in many replications. The auxiliary model is fitted in each completed dataset, and the results are pooled. This propagates classification uncertainty through the draws. Increasing the number of draws reduces Monte Carlo error. ### Compare distal means Here we use `lm()` with a factor-coded class variable and no intercept, yielding one coefficient per class. The `model` expression is evaluated in each pseudo-class dataset. ```{r pseudo-lm-code, eval=FALSE} set.seed(2027) pseudo_mean <- pseudo_class( x = lpa_model, model = lm(distance ~ I(factor(class)) - 1, data = data), data = df[, "distance", drop = FALSE], m = 20 ) pseudo_mean ``` ```{r pseudo-lm-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} set.seed(2027) pseudo_mean <- tidySEM:::run_cached(pseudo_class( x = lpa_model, model = lm(distance ~ I(factor(class)) - 1, data = data), data = df[, "distance", drop = FALSE], m = 20 )) ``` ```{r pseudo-lm-results, echo=FALSE, eval=show_results && run_openmx} pseudo_mean ``` ### Use RAM syntax The `pseudo_class()` approach also accepts auxiliary models expressed using `lavaan` syntax. If the model does not explicitly reference `class`, the function treats class as a grouping variable. ```{r pseudo-ram-code, eval=FALSE} set.seed(2028) pseudo_ram <- pseudo_class( x = lpa_model, model = "distance ~ 1", data = df[, "distance", drop = FALSE], m = 20 ) pseudo_ram ``` ```{r pseudo-ram-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} set.seed(2028) pseudo_ram <- tidySEM:::run_cached(pseudo_class( x = lpa_model, model = "distance ~ 1", data = df[, "distance", drop = FALSE], m = 20 )) ``` ```{r pseudo-ram-results, echo=FALSE, eval=show_results && run_openmx} pseudo_ram ``` ## Predicting class membership So far, we have treated class membership as an explanatory grouping for auxiliary outcomes. Sometimes the research question instead asks which external variables predict class membership. These are not the same estimands: differences in a distal mean do not supply the coefficients of a multinomial regression adjusted for other predictors. For example, a multinomial model can be fitted across pseudo-class draws using `nnet::multinom()`: ```{r pseudo-predict-code, eval=FALSE} set.seed(2030) pseudo_pred <- pseudo_class( x = lpa_model, model = function(data) { nnet::multinom(class ~ distance + freqvisit, data = data, trace = FALSE) }, data = df_aux, m = 20 ) pseudo_pred ``` ```{r pseudo-predict-fit, include=FALSE, eval=run_openmx && (run_everything || show_results)} if (requireNamespace("nnet", quietly = TRUE)) { set.seed(2030) pseudo_pred <- tidySEM:::run_cached(pseudo_class( x = lpa_model, model = function(data) nnet::multinom( class ~ distance + freqvisit, data = data, trace = FALSE), data = df_aux, m = 20 )) } ``` ```{r pseudo-predict-results, echo=FALSE, eval=show_results && run_openmx} if (exists("pseudo_pred", inherits = FALSE)) pseudo_pred ``` The coefficients describe conditional associations with class membership relative to a reference class. ## Reporting auxiliary analyses A report should identify the measurement indicators, selected number of classes, classification quality, auxiliary-variable role, estimation method, and treatment of uncertainty. Report class-specific estimates and their units, as well as uncertainty intervals. Mention what kind of standard errors you used, and whether they are valid. For comparisons across classes via likelihood ratio test, state which parameters were constrained.