Unified r Selection for Mixture Models (MFA/MPCA)
Source:R/r_selection_mixture.R
select_r_mixture.RdThis function provides a unified interface for selecting the optimal number of factors (r) in mixture models. It supports three modes: "clusterwise" (evaluate r separately per cluster), "global-first" (estimate r from pooled data first), and "hybrid" (combine both approaches).
Usage
select_r_mixture(
list_of_data,
K,
rvec = 1:6,
mode = c("clusterwise", "global-first", "hybrid"),
model_type = c("MFA", "MPCA"),
criteria = c("VAF", "VSS", "smoothness"),
agg_method = c("max", "mean", "min", "majority", "global"),
global_model = c("PPCA", "FA"),
global_criterion = c("CV", "ParallelAnalysis"),
global_folds = 5,
global_seed = NULL,
weights = c(VAF = 0.4, VSS = 0.4, smoothness = 0.2),
vss_threshold = 0.5,
vss_method = c("threshold", "ratio"),
vss_rotate = c("none", "varimax"),
vss_normalize = c("logr", "max", "r", "r2", "none"),
max_iter = 50,
nIterFA = 5,
nIterPCA = 5,
tol = 0.001,
n_init = 1,
use_kmeans_init = TRUE,
subject_rdim_for_kmeans = 2,
mc_cores = 1,
n_threads = 1,
verbose = FALSE,
refit_final = TRUE
)Arguments
- list_of_data
A list of N matrices, each (T_i x M).
- K
Number of clusters (fixed).
- rvec
Vector of candidate r values to evaluate. Default is 1:6.
- mode
Selection mode. Options:
"clusterwise" (default): Evaluate r separately for each cluster, then aggregate
"global-first": Estimate r using global FA/PPCA on pooled data
"hybrid": Use global r as initialization for clusterwise evaluation
- model_type
Model type. Options: "MFA" (Mixture Factor Analysis, default), "MPCA" (Mixture Principal Component Analysis).
- criteria
Character vector specifying which criteria to use for clusterwise selection. Options: "VAF", "VSS", "smoothness". Default is c("VAF", "VSS", "smoothness"). Only used when mode = "clusterwise" or "hybrid".
- agg_method
Method for aggregating cluster-specific r values. Options: "max" (conservative, takes maximum r across clusters), "mean" (average r, rounded), "min" (minimum r across clusters), "majority" (most common r value), "global" (use the global r estimate r_global as the final r; only valid in hybrid mode). Default is "max". In clusterwise mode, only "max", "mean", "min", "majority" are allowed. In hybrid mode, you may additionally use "global" to set r_final = r_global. Ignored when mode = "global-first".
- global_model
Model for global r estimation. Options: "PPCA" (default), "FA". Only used when mode = "global-first" or "hybrid".
- global_criterion
Criterion for global r selection. Options: "CV" (default), "ParallelAnalysis". Only used when mode = "global-first" or "hybrid".
- global_folds
Number of CV folds for global r selection. Default is 5. Only used when mode = "global-first" or "hybrid" with criterion = "CV".
- global_seed
Random seed for global r selection. Default is NULL.
- weights
Named vector of weights for combining criteria in clusterwise mode. Default is c(VAF=0.4, VSS=0.4, smoothness=0.2).
- vss_threshold
Threshold for VSS criterion. Default is 0.5.
- vss_method
Method for computing VSS. Options: "threshold", "ratio". Default is "threshold".
- vss_rotate
Rotation method for VSS. Options: "none", "varimax". Default is "none".
- vss_normalize
Method for normalizing VSS. Options: "logr", "max", "r", "r2", "none". Default is "logr".
- max_iter
Maximum EM iterations for model fitting.
- nIterFA
Sub-iterations for FA update (MFA only).
- nIterPCA
Sub-iterations for PCA update (MPCA only).
- tol
Convergence tolerance.
- n_init
Number of random initializations for mixture model fitting.
- use_kmeans_init
Whether to use k-means initialization.
- subject_rdim_for_kmeans
Dimension for k-means features.
- mc_cores
Number of cores for parallelization.
- n_threads
Number of OpenMP threads.
- verbose
Logical. If TRUE, print detailed diagnostic information.
- refit_final
Logical. If TRUE, refit the final model with selected r_final. Default is TRUE.
Value
A list with class "mixture_r_selection" containing:
- r_final
The final selected r value.
- r_global
Global r estimate (if mode = "global-first" or "hybrid").
- r_cluster
Named vector of cluster-specific r values (if mode = "clusterwise" or "hybrid").
- initial_fit
Initial mixture model fit used for cluster assignments (if applicable).
- final_fit
Final mixture model fit with r_final (if refit_final = TRUE).
- diagnostics
List containing detailed diagnostic information.
- mode
The selection mode used.
- model_type
The model type used (MFA or MPCA).
- agg_method
The aggregation method used (if applicable).
Details
This function provides a unified interface for r selection in mixture models, integrating three complementary approaches:
Mode: "global-first"
Pool all data and fit a global FA/PPCA model
Select r using cross-validation or parallel analysis
Return r_global as the final estimate
This mode is fast and provides a cluster-independent baseline. It's recommended when you want a simple, stable estimate that doesn't depend on cluster structure.
Mode: "clusterwise"
Fit initial mixture model with median(rvec) to get cluster assignments
For each cluster, evaluate different r values using cluster-specific metrics
Aggregate cluster-specific r values using specified method
This mode is useful when different clusters may require different numbers of factors.
Mode: "hybrid"
Estimate r_global using global FA/PPCA (as in "global-first")
Use r_global as initialization for clusterwise evaluation
Evaluate r values around r_global for each cluster
Aggregate cluster-specific r values
This mode combines the stability of global estimation with the flexibility of cluster-specific evaluation.
Examples
if (FALSE) { # \dontrun{
# Global-first mode (recommended for simplicity)
result <- select_r_mixture(
list_of_data, K = 3, rvec = 2:6,
mode = "global-first",
model_type = "MFA"
)
print(result$r_final)
# Clusterwise mode
result <- select_r_mixture(
list_of_data, K = 3, rvec = 2:6,
mode = "clusterwise",
model_type = "MFA",
agg_method = "max"
)
# Hybrid mode
result <- select_r_mixture(
list_of_data, K = 3, rvec = 2:6,
mode = "hybrid",
model_type = "MPCA"
)
} # }