Skip to contents

This function tries all combinations of K in Kvec and r in rvec, fits a Mixture Factor Analysis (MFA) model using mfa_em_fit for each combination, computes BIC, and returns the best model by BIC.

Usage

select_optimal_K_r_mfa(
  list_of_data,
  Kvec = 1:5,
  rvec = 1:5,
  max_iter = 50,
  nIterFA = 5,
  tol = 0.001,
  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.

nIterFA

Sub-iterations within the factor-analyzer update in C++.

tol

Convergence tolerance.

n_init

Number of random initializations to try inside mfa_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 mfa_em_fit(..., n_init, use_kmeans_init, ...) for each (K, r). - After fitting, we compute log-likelihood by summing over all data (you can use the built-in logLik from the fit, or re-compute). - Then compute BIC = -2*logLik + p*log(N_total_rows). Here p = K*(M*r + 2*M) + (K - 1) by default (diagonal noise).