Technical Details and Algorithms
Siyang Ni
2025-11-13
Source:vignettes/technical-details.Rmd
technical-details.RmdIntroduction
This vignette provides technical details on the statistical methods
and algorithms implemented in surveymixr. It covers:
- Growth mixture modeling framework
- Complex survey design integration
- EM algorithm implementation
- Standard error computation
- Bootstrap likelihood ratio test (BLRT)
- Model fit indices
For an introduction to using the package, see
vignette("surveymixr-intro").
Statistical Framework
Growth Mixture Models
Growth mixture models (GMMs) combine growth curve modeling with latent class analysis to identify distinct subgroups (classes) with different developmental trajectories (Muthén and Shedden 1999).
Estimation Algorithm
EM Algorithm Overview
The Expectation-Maximization (EM) algorithm (Dempster, Laird, and Rubin 1977) iterates between:
- E-step: Compute posterior class probabilities
- M-step: Update parameters given posterior probabilities
Missing Data Handling (FIML)
surveymixr uses Full Information Maximum Likelihood
(FIML) for missing data under the Missing at Random (MAR)
assumption.
For individual with observed data :
Advantages: - Uses all available data - Unbiased under MAR - No need for imputation
Multiple Random Starts
Local maxima are common in mixture models. surveymixr
uses multiple random starts:
- Generate
starts(default 500) random initial parameter values - Run EM from each starting point (parallelized)
- Select solution with highest log-likelihood
- Check for local maxima with
diagnose_convergence()
Example:
fit <- gmm_survey(
data = mcs_simulated,
id = "id",
time = "age",
outcome = "selfcontrol",
n_classes = 3,
starts = 500, # Multiple random starts
cores = 4 # Parallel processing
)
# Check for local maxima
diagnose_convergence(fit)Standard Errors
Sandwich Estimator
surveymixr uses the sandwich (robust) variance estimator
to account for: - Model misspecification - Clustering -
Stratification
where: - = Hessian (observed information matrix) - = Outer product of score contributions (meat)
Bootstrap Likelihood Ratio Test (BLRT)
The BLRT (McLachlan 1987) tests whether a -class model fits better than a -class model.
Algorithm
- Fit -class model, obtain
- Fit -class model, obtain
- Compute observed LRT statistic:
-
Bootstrap (repeat
times):
- Simulate data from -class model
- Fit both and -class models
- Compute
- -value = proportion of
Interpretation: - : Reject classes, favor classes - : No evidence for classes
Example:
selection <- gmm_select(
data = mcs_simulated,
id = "id",
time = "age",
outcome = "selfcontrol",
min_classes = 1,
max_classes = 5,
run_blrt = TRUE,
blrt_samples = 500, # Bootstrap replicates
cores = 4
)
# View BLRT results
summary(selection)Model Fit Indices
Information Criteria
AIC (Akaike Information Criterion):
BIC (Bayesian Information Criterion):
Sample-size adjusted BIC:
where: - = maximized log-likelihood - = number of parameters - = sample size (effective sample size for weighted data)
Lower values indicate better fit.
Entropy
Entropy measures classification certainty (Ramaswamy et al. 1993):
- Range:
- Higher values = better classification
- is considered good
- is excellent
Computational Efficiency
Parallelization
Random starts are parallelized using the parallel
package:
# Detect cores
n_cores <- parallel::detectCores() - 1
fit <- gmm_survey(
...,
cores = n_cores
)Memory Management
For large datasets: - Use sparse matrices where applicable - Process data in chunks for FIML - Limit random starts for initial exploration
Typical Run Times
On a modern laptop (4 cores):
| n | T | K | starts | Time |
|---|---|---|---|---|
| 1,000 | 4 | 2 | 100 | ~1 min |
| 1,000 | 4 | 3 | 500 | ~5 min |
| 5,000 | 6 | 3 | 500 | ~15 min |
| 10,000 | 6 | 4 | 1000 | ~45 min |
Tips for speed: 1. Start with fewer random starts (50-100) for exploration 2. Use parallel processing (4+ cores) 3. Reduce to final model with more starts (500-1000)
Comparison with Mplus
surveymixr produces results consistent with Mplus:
| Feature | surveymixr | Mplus |
|---|---|---|
| Parameter estimates | ✓ | ✓ |
| Standard errors | ✓ (sandwich) | ✓ |
| Survey weights | ✓ | ✓ |
| BLRT | ✓ | ✓ |
| Speed | Comparable | Comparable |
| Cost | Free | |
| R integration | Native | Via MplusAutomation |
See vignette("mplus-validation") for detailed
comparisons.
References
Session Info
sessionInfo()
#> R version 4.3.3 (2024-02-29)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 24.04.3 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0
#>
#> locale:
#> [1] C
#>
#> time zone: Etc/UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] surveymixr_0.2.0
#>
#> loaded via a namespace (and not attached):
#> [1] vctrs_0.6.5 cli_3.6.5 knitr_1.50
#> [4] rlang_1.1.6 xfun_0.54 generics_0.1.4
#> [7] S7_0.2.0 textshaping_1.0.4 jsonlite_2.0.0
#> [10] glue_1.8.0 htmltools_0.5.8.1 ragg_1.5.0
#> [13] sass_0.4.10 scales_1.4.0 rmarkdown_2.30
#> [16] grid_4.3.3 tibble_3.3.0 evaluate_1.0.5
#> [19] jquerylib_0.1.4 fastmap_1.2.0 numDeriv_2016.8-1.1
#> [22] yaml_2.3.10 lifecycle_1.0.4 compiler_4.3.3
#> [25] dplyr_1.1.4 RColorBrewer_1.1-3 fs_1.6.6
#> [28] pkgconfig_2.0.3 htmlwidgets_1.6.4 farver_2.1.2
#> [31] systemfonts_1.3.1 digest_0.6.38 R6_2.6.1
#> [34] tidyselect_1.2.1 parallel_4.3.3 pillar_1.11.1
#> [37] magrittr_2.0.4 bslib_0.9.0 tools_4.3.3
#> [40] gtable_0.3.6 pkgdown_2.2.0 ggplot2_4.0.0
#> [43] cachem_1.1.0 desc_1.4.3