Search for the Best (K, r) for Mixture PCA via Grid Search
Source:R/best_fit.R
select_optimal_K_r_mpca.RdThis function tries all combinations of K in Kvec and r in rvec, fits a Mixture PCA (PPCA) model using mixture_pca_em_fit for each combination, computes BIC, and returns the best model by BIC.
Usage
select_optimal_K_r_mpca(
list_of_data,
Kvec = 1:5,
rvec = 1:5,
max_iter = 50,
nIterPCA = 5,
tol = 0.001,
method = "EM",
n_init = 1,
use_kmeans_init = TRUE,
subject_rdim_for_kmeans = 2,
mc_cores_grid = 1,
mc_cores = 1,
n_threads = 1
)Arguments
- list_of_data
A list of data matrices, each (T_i x M).
- Kvec
An integer vector of candidate K values.
- rvec
An integer vector of candidate r values.
- max_iter
Maximum EM iterations.
- nIterPCA
Sub-iterations within the PCA/PPCA update in C++.
- tol
Convergence tolerance.
- method
"EM" or "closed_form" (passed to mixture_pca_em_fit).
- n_init
Number of random initializations to try inside mixture_pca_em_fit.
- use_kmeans_init
Logical; whether to also try a k-means-based init.
- subject_rdim_for_kmeans
Dimension for PCA-based features if use_kmeans_init=TRUE.
- mc_cores_grid
Number of CPU cores for grid-level parallelization (default: 1).
- mc_cores
Number of CPU cores for initialization-level parallelization within each (K, r) (default: 1).
- n_threads
Number of OpenMP threads for EM-level parallelization (default: 1 to avoid nested parallelism).
Value
A list with:
- summary
A data.frame with columns K, r, logLik, BIC.
- best_model_info
A list with (K, r, model, logLik, BIC) for the best BIC.
- all_models
A list of all fits, each (K, r, model, logLik, BIC).
Details
- Calls mixture_pca_em_fit(..., n_init, use_kmeans_init, ...) for each (K, r). - Uses fit$logLik as the final log-likelihood. - BIC formula can be adjusted if needed. By default, we might do p = K*(M*r + r + M) + (K-1), for example, if we consider each cluster having: - W is M x r (or equivalently P x D) - mu is length M - sigma2 is 1 scalar - mixing proportions (K-1)