Fits a set of latent class (LCA) segmentation models and returns them in a standardized “rack” structure. This function accepts a dataset, an optional model formula, and one or more segment counts (n_seg), then automatically prepares the inputs, runs poLCA for each specified number of segments, and packages the outputs into a consistent rack object.
If no formula is supplied, the function constructs a default formula using all
variables in data$x as categorical indicators.
The output includes:
Fitted LCA models for each segment count
Segment assignments for each respondent
Model fit statistics (AIC, BIC, G², X², df)
A cleaned and aligned rack of segment solutions
Arguments
- data
A list-like object containing
x, a data frame of indicator variables for LCA.- formula
Optional model formula. If NULL, a formula using all columns of
data$xis constructed.- n_seg
Integer or vector of integers indicating how many latent classes to estimate.
- maxiter
The maximum number of iterations through which the estimation algorithm will cycle.
- seed
Optional random seed for reproducibility.
- ...
Additional arguments passed to
poLCA::poLCA().
Examples
seg_lca_data <- pack_model_data(
example_lca_data,
id_col = response_id,
x_cols = c(starts_with("q_"), starts_with("used_"), starts_with("attr_"))
)
#> ✔ Converted 10 <factor> columns to <labelled numeric>: q_main_brand, q_main_occasion, used_brand_A, used_brand_B, used_brand_C, used_brand_D, attr_quality, attr_value, attr_innovation, and attr_trust.
#>
#> ── Summary of mapping for 10 <labelled> columns ────────────────────────────────
#>
#> [1 column] q_main_brand:
#> 1: Brand A
#> 2: Brand B
#> 3: Brand C
#> 4: Brand D
#>
#> [1 column] q_main_occasion:
#> 1: Everyday
#> 2: On sale
#> 3: Rarely
#> 4: Special occasions
#>
#> [4 columns] used_brand_A, used_brand_B, used_brand_C, used_brand_D:
#> 1: No
#> 2: Yes
#>
#> [4 columns] attr_quality, attr_value, attr_innovation, attr_trust:
#> 1: 1
#> 2: 2
#> 3: 3
#> 4: 4
#> 5: 5
#>
lca_model_rack <- seg_rack_lca(seg_lca_data, n_seg = 3:5, maxiter = 1000, seed = 1L)
#> Conditional item response (column) probabilities,
#> by outcome variable, for each class (row)
#>
#> $q_main_brand
#> Pr(1) Pr(2) Pr(3) Pr(4)
#> class 1: 0.0925 0.1885 0.0377 0.6813
#> class 2: 0.6283 0.3009 0.0708 0.0000
#> class 3: 0.5263 0.0000 0.3316 0.1421
#>
#> $q_main_occasion
#> Pr(1) Pr(2) Pr(3) Pr(4)
#> class 1: 0.2020 0.2896 0.3077 0.2006
#> class 2: 0.2280 0.1627 0.3818 0.2275
#> class 3: 0.2744 0.2955 0.1341 0.2959
#>
#> $used_brand_A
#> Pr(1) Pr(2)
#> class 1: 0.5268 0.4732
#> class 2: 0.3291 0.6709
#> class 3: 0.3539 0.6461
#>
#> $used_brand_B
#> Pr(1) Pr(2)
#> class 1: 0.5895 0.4105
#> class 2: 0.4647 0.5353
#> class 3: 0.4254 0.5746
#>
#> $used_brand_C
#> Pr(1) Pr(2)
#> class 1: 0.7070 0.2930
#> class 2: 0.5220 0.4780
#> class 3: 0.5924 0.4076
#>
#> $used_brand_D
#> Pr(1) Pr(2)
#> class 1: 0.8155 0.1845
#> class 2: 0.9332 0.0668
#> class 3: 0.6768 0.3232
#>
#> $attr_quality
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0046 0.0676 0.2962 0.3947 0.2369
#> class 2: 0.0370 0.2084 0.5389 0.2157 0.0000
#> class 3: 0.0127 0.0870 0.2563 0.4522 0.1918
#>
#> $attr_value
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0000 0.0122 0.1249 0.4518 0.4112
#> class 2: 0.0212 0.0748 0.2270 0.4543 0.2227
#> class 3: 0.0000 0.0271 0.2688 0.4661 0.2380
#>
#> $attr_innovation
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0182 0.1426 0.4099 0.3231 0.1062
#> class 2: 0.1511 0.2873 0.3460 0.2156 0.0000
#> class 3: 0.0000 0.2622 0.4104 0.2813 0.0461
#>
#> $attr_trust
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.000 0.0367 0.1702 0.3858 0.4074
#> class 2: 0.000 0.0894 0.4028 0.4290 0.0787
#> class 3: 0.005 0.1048 0.2445 0.5054 0.1403
#>
#> Estimated class population shares
#> 0.4094 0.189 0.4017
#>
#> Predicted class memberships (by modal posterior prob.)
#> 0.408 0.168 0.424
#>
#> =========================================================
#> Fit for 3 latent classes:
#> =========================================================
#> number of observations: 500
#> number of estimated parameters: 80
#> residual degrees of freedom: 420
#> maximum log-likelihood: -5101.147
#>
#> AIC(3): 10362.29
#> BIC(3): 10699.46
#> G^2(3): 4007.095 (Likelihood ratio/deviance statistic)
#> X^2(3): 114279.1 (Chi-square goodness of fit)
#>
#> Conditional item response (column) probabilities,
#> by outcome variable, for each class (row)
#>
#> $q_main_brand
#> Pr(1) Pr(2) Pr(3) Pr(4)
#> class 1: 0.0880 0.0780 0.0679 0.7662
#> class 2: 0.4547 0.2739 0.1576 0.1139
#> class 3: 0.4934 0.4629 0.0000 0.0437
#> class 4: 0.5610 0.0420 0.2907 0.1063
#>
#> $q_main_occasion
#> Pr(1) Pr(2) Pr(3) Pr(4)
#> class 1: 0.2381 0.2795 0.2868 0.1956
#> class 2: 0.2890 0.1004 0.3923 0.2183
#> class 3: 0.1482 0.3563 0.2977 0.1978
#> class 4: 0.2284 0.3188 0.1397 0.3131
#>
#> $used_brand_A
#> Pr(1) Pr(2)
#> class 1: 0.5462 0.4538
#> class 2: 0.2511 0.7489
#> class 3: 0.4775 0.5225
#> class 4: 0.3702 0.6298
#>
#> $used_brand_B
#> Pr(1) Pr(2)
#> class 1: 0.5373 0.4627
#> class 2: 0.4947 0.5053
#> class 3: 1.0000 0.0000
#> class 4: 0.3513 0.6487
#>
#> $used_brand_C
#> Pr(1) Pr(2)
#> class 1: 0.7011 0.2989
#> class 2: 0.3654 0.6346
#> class 3: 1.0000 0.0000
#> class 4: 0.5960 0.4040
#>
#> $used_brand_D
#> Pr(1) Pr(2)
#> class 1: 0.8219 0.1781
#> class 2: 1.0000 0.0000
#> class 3: 0.5780 0.4220
#> class 4: 0.6847 0.3153
#>
#> $attr_quality
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0039 0.0632 0.2814 0.3722 0.2793
#> class 2: 0.0000 0.0869 0.4837 0.4294 0.0000
#> class 3: 0.0000 0.0634 0.3569 0.5246 0.0551
#> class 4: 0.0337 0.1550 0.2837 0.3402 0.1874
#>
#> $attr_value
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0000 0.0107 0.1195 0.4234 0.4464
#> class 2: 0.0000 0.0000 0.2720 0.4706 0.2574
#> class 3: 0.0229 0.0000 0.1762 0.6800 0.1209
#> class 4: 0.0054 0.0699 0.2517 0.4330 0.2401
#>
#> $attr_innovation
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0000 0.1294 0.4270 0.3405 0.1031
#> class 2: 0.1438 0.3224 0.3048 0.1513 0.0778
#> class 3: 0.0000 0.3368 0.4464 0.1475 0.0693
#> class 4: 0.0255 0.2230 0.4051 0.3327 0.0137
#>
#> $attr_trust
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0000 0.0379 0.1491 0.3940 0.4190
#> class 2: 0.0000 0.0642 0.2866 0.4552 0.1940
#> class 3: 0.0000 0.1738 0.4280 0.1800 0.2182
#> class 4: 0.0053 0.0898 0.2700 0.5417 0.0931
#>
#> Estimated class population shares
#> 0.3542 0.1839 0.087 0.3749
#>
#> Predicted class memberships (by modal posterior prob.)
#> 0.36 0.168 0.1 0.372
#>
#> =========================================================
#> Fit for 4 latent classes:
#> =========================================================
#> number of observations: 500
#> number of estimated parameters: 107
#> residual degrees of freedom: 393
#> maximum log-likelihood: -5081.582
#>
#> AIC(4): 10377.16
#> BIC(4): 10828.13
#> G^2(4): 3967.965 (Likelihood ratio/deviance statistic)
#> X^2(4): 679520.1 (Chi-square goodness of fit)
#>
#> Conditional item response (column) probabilities,
#> by outcome variable, for each class (row)
#>
#> $q_main_brand
#> Pr(1) Pr(2) Pr(3) Pr(4)
#> class 1: 0.1553 0.1713 0.0475 0.6258
#> class 2: 0.0000 0.1037 0.1281 0.7682
#> class 3: 0.5307 0.1992 0.1445 0.1255
#> class 4: 0.7147 0.2470 0.0383 0.0000
#> class 5: 0.4349 0.0000 0.3931 0.1720
#>
#> $q_main_occasion
#> Pr(1) Pr(2) Pr(3) Pr(4)
#> class 1: 0.1798 0.3355 0.3359 0.1489
#> class 2: 0.2392 0.2431 0.3061 0.2117
#> class 3: 0.1779 0.1087 0.4513 0.2621
#> class 4: 0.2371 0.1930 0.2648 0.3051
#> class 5: 0.3081 0.3506 0.0491 0.2921
#>
#> $used_brand_A
#> Pr(1) Pr(2)
#> class 1: 0.4421 0.5579
#> class 2: 0.6832 0.3168
#> class 3: 0.1303 0.8697
#> class 4: 0.4687 0.5313
#> class 5: 0.3216 0.6784
#>
#> $used_brand_B
#> Pr(1) Pr(2)
#> class 1: 0.6043 0.3957
#> class 2: 0.5776 0.4224
#> class 3: 0.3848 0.6152
#> class 4: 0.5769 0.4231
#> class 5: 0.3451 0.6549
#>
#> $used_brand_C
#> Pr(1) Pr(2)
#> class 1: 0.7642 0.2358
#> class 2: 0.7004 0.2996
#> class 3: 0.0000 1.0000
#> class 4: 0.8037 0.1963
#> class 5: 0.5762 0.4238
#>
#> $used_brand_D
#> Pr(1) Pr(2)
#> class 1: 0.7054 0.2946
#> class 2: 0.9560 0.0440
#> class 3: 1.0000 0.0000
#> class 4: 0.8768 0.1232
#> class 5: 0.5754 0.4246
#>
#> $attr_quality
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0000 0.0770 0.2926 0.4008 0.2296
#> class 2: 0.0114 0.0522 0.3125 0.3333 0.2906
#> class 3: 0.0443 0.0670 0.4024 0.4863 0.0000
#> class 4: 0.0164 0.1926 0.4077 0.2736 0.1097
#> class 5: 0.0134 0.0963 0.2659 0.4463 0.1781
#>
#> $attr_value
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0083 0.0160 0.0950 0.4596 0.4210
#> class 2: 0.0000 0.0223 0.1961 0.2992 0.4824
#> class 3: 0.0178 0.0000 0.2874 0.4537 0.2411
#> class 4: 0.0000 0.0796 0.2032 0.5275 0.1896
#> class 5: 0.0000 0.0197 0.2647 0.4993 0.2163
#>
#> $attr_innovation
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0173 0.0911 0.4401 0.3466 0.1049
#> class 2: 0.0000 0.2107 0.4146 0.2702 0.1045
#> class 3: 0.1485 0.3317 0.1762 0.2869 0.0567
#> class 4: 0.0699 0.2974 0.4011 0.2316 0.0000
#> class 5: 0.0000 0.2241 0.4410 0.2850 0.0499
#>
#> $attr_trust
#> Pr(1) Pr(2) Pr(3) Pr(4) Pr(5)
#> class 1: 0.0000 0.0000 0.2750 0.0000 0.7250
#> class 2: 0.0000 0.0873 0.0624 0.8503 0.0000
#> class 3: 0.0000 0.0789 0.2658 0.4580 0.1973
#> class 4: 0.0000 0.1434 0.3440 0.4847 0.0278
#> class 5: 0.0075 0.0740 0.2371 0.5475 0.1339
#>
#> Estimated class population shares
#> 0.24 0.1637 0.1126 0.2165 0.2672
#>
#> Predicted class memberships (by modal posterior prob.)
#> 0.246 0.164 0.13 0.228 0.232
#>
#> =========================================================
#> Fit for 5 latent classes:
#> =========================================================
#> number of observations: 500
#> number of estimated parameters: 134
#> residual degrees of freedom: 366
#> maximum log-likelihood: -5063.678
#>
#> AIC(5): 10395.36
#> BIC(5): 10960.11
#> G^2(5): 3932.156 (Likelihood ratio/deviance statistic)
#> X^2(5): 138757.1 (Chi-square goodness of fit)
#>
# extract particular summaries ---
library(dplyr)
library(tidyr)
## unpack model measures and arrange models by sparseness.basis descending
lca_model_rack |>
select(model_name, measures) |>
unpack(measures) |>
arrange(desc(BIC))
#> # A tibble: 3 × 6
#> model_name AIC BIC `G^2` `X^2` Df
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 lc_5 10395. 10960. 3932. 138757. 366
#> 2 lc_4 10377. 10828. 3968. 679520. 393
#> 3 lc_3 10362. 10699. 4007. 114279. 420
## extract segment assignments for all models into one flat dataframe
lca_model_rack |>
rowwise() |>
mutate(
id = list(data$id),
weight = list(data$weight)
) |>
select(model_name, id, weight, segments) |>
unnest(c(id, weight, segments))
#> # A tibble: 1,500 × 4
#> model_name id weight segments
#> <chr> <chr> <dbl> <fct>
#> 1 lc_3 r_1 1 2
#> 2 lc_3 r_2 1 1
#> 3 lc_3 r_3 1 1
#> 4 lc_3 r_4 1 3
#> 5 lc_3 r_5 1 3
#> 6 lc_3 r_6 1 3
#> 7 lc_3 r_7 1 1
#> 8 lc_3 r_8 1 1
#> 9 lc_3 r_9 1 1
#> 10 lc_3 r_10 1 3
#> # ℹ 1,490 more rows