enumerate_regressions: Compare mixture regressions with different numbers of classes

View source: R/mixture-regression-enumerate.R

enumerate_regressionsR Documentation

Compare mixture regressions with different numbers of classes

Description

Fits mixture_regression() for every requested number of classes (and of group classes, for the two-level model) and returns their fit statistics in one table. Optionally adds a parametric bootstrap likelihood-ratio test of k - 1 against k classes (McLachlan 1987; Nylund, Asparouhov and Muthen 2007): outcomes are simulated from the k - 1 class fit, both models are refitted to every replicate, and the p-value is the share of replicate statistics at least as large as the observed one, (1 + b) / (1 + B).

Usage

enumerate_regressions(
  formula,
  data,
  n_classes = 1:4,
  n_group_classes = 1L,
  bootstrap = 0L,
  bootstrap_starts = 3L,
  seed = NULL,
  ...
)

## S3 method for class 'latents_regression_enumeration'
as.data.frame(x, row.names = NULL, optional = FALSE, ...)

## S3 method for class 'latents_regression_enumeration'
get_results(
  x,
  what = c("fit", "model"),
  n_classes = NULL,
  n_group_classes = 1L,
  ...
)

## S3 method for class 'latents_regression_enumeration'
print(x, digits = 4L, ...)

Arguments

formula

A model formula outcome ~ predictors. Factors, interactions and offset() are supported.

data

A data frame.

n_classes

Integer vector of class counts to fit.

n_group_classes

Integer vector of group-class counts (two-level model; needs id). Crossed with n_classes.

bootstrap

Number of bootstrap replicates for the likelihood-ratio test; 0 skips it.

bootstrap_starts

Random starts for each bootstrap refit.

seed

NULL or an integer seed. The caller's random state is restored afterwards.

...

Further arguments to mixture_regression(), such as family, id, class_level, common, membership or variance.

x

A latents_regression_enumeration object.

row.names, optional

Unused; part of the generic.

what

"fit" for the comparison table, "model" for one fitted model.

digits

Significant digits.

Value

An object of class latents_regression_enumeration. Its table (from as.data.frame() or get_results(x, "fit")) has one row per model: n_classes, n_group_classes, log_likelihood, n_parameters, aic, bic, bic_rows, sabic, icl, entropy, smallest_share, converged, n_best_replicated, best_bic (the minimum-BIC row), and, with bootstrap > 0, blrt_statistic, blrt_p_value and blrt_replicates (the successful replicates) and blrt_flagged (the failed, unconverged, separated or reversed-likelihood replicates). The p-value is NA if any requested replicate fails validation. get_results(x, "model", n_classes = , n_group_classes = ) returns one fitted model.

Conditions

latents_bad_argument for an empty or invalid grid. A model that cannot be fitted at some count (for example every start degenerates) gets a row of NA with its condition message in note, rather than stopping the comparison.

References

McLachlan, G. J. (1987). On bootstrapping the likelihood ratio test statistic for the number of components in a normal mixture. Applied Statistics, 36, 318–324.

Nylund, K. L., Asparouhov, T., & Muthen, B. O. (2007). Deciding on the number of classes in latent class analysis and growth mixture modeling: A Monte Carlo simulation study. Structural Equation Modeling, 14, 535–569.

Examples

classes <- enumerate_regressions(score ~ hours, data = study_hours,
                                 n_classes = 1:3, n_starts = 3, seed = 1)
classes

with_test <- enumerate_regressions(score ~ hours, data = study_hours,
                                   n_classes = 1:2, n_starts = 3, seed = 1,
                                   bootstrap = 19)
as.data.frame(with_test)


latents documentation built on Oct. 16, 2026, 5:09 p.m.