Skip to contents

This function implements K-fold cross-validation for choosing the optimal number of clusters K. It evaluates model generalization by computing log-likelihood on held-out subjects.

Usage

select_K_crossvalidation(
  list_of_data,
  Kvec = 2:5,
  r = 2,
  nfolds = 5,
  max_iter = 50,
  n_init = 1,
  model_type = c("MFA", "MPCA"),
  verbose = TRUE
)

Arguments

list_of_data

A list of data matrices, each (T_i x M).

Kvec

Vector of candidate K values to test.

r

Number of factors (fixed).

nfolds

Number of cross-validation folds (default: 5).

max_iter

Maximum EM iterations per fit.

n_init

Number of random initializations per fit.

model_type

Either "MFA" or "MPCA" (default: "MFA").

verbose

Print progress messages (default: TRUE).

Value

A list containing:

best_K

The selected optimal K based on CV log-likelihood

cv_scores

Mean CV log-likelihood for each K

cv_se

Standard error of CV log-likelihood for each K

cv_df

Data frame with K, mean CV score, and SE

Details

The cross-validation procedure: 1. Split subjects into nfolds groups 2. For each K and each fold: - Train model on (nfolds-1) folds - Compute log-likelihood on held-out fold 3. Average log-likelihoods across folds for each K 4. Select K with highest average CV log-likelihood

Higher CV log-likelihood indicates better generalization.