This function implements a two-step baseline approach for comparison with mixture-based methods:
Step 1: Individual Decomposition - Apply PCA or FA to each subject's data separately to extract
rbasis vectors per subject.Step 1.5: Basis Alignment - Align basis vectors across subjects using the Hungarian algorithm to ensure corresponding components are in the same order, improving clustering accuracy.
Step 2: Clustering - Normalize the basis vectors, concatenate them across all subjects, and apply k-means clustering. Assign each subject to a cluster based on majority vote of their basis vectors.
Usage
fit_two_step_baseline(
data_list,
r,
K,
method = c("PCA", "FA"),
seed = 123,
z_true = NULL,
refine_assignments = FALSE,
align_basis = TRUE
)Arguments
- data_list
A list of
(T_i x M)matrices, one per subject.- r
Number of components (factors or principal components) to extract per subject.
- K
Number of clusters for k-means.
- method
Either
"PCA"or"FA"for the decomposition method.- seed
Random seed for reproducibility.
- z_true
Optional true cluster labels for computing ARI. If
NULL, ARI is set toNA.- refine_assignments
Logical. If
TRUE, performs a single reassignment pass after fitting cluster-specific models in Step 4. Default isFALSE.- align_basis
Logical. If
TRUE(default), applies Hungarian algorithm alignment in Step 1.5 to ensure corresponding components across subjects are in the same order before clustering. Set toFALSEto disable alignment.
Value
A list with:
z_estInteger vector of estimated cluster assignments (1..K).
W_listList of normalized basis matrices, one per subject.
W_centroidsMatrix of cluster centroids in basis space.
mu_listList of centering vectors used for standardization, one per subject.
sd_listList of scaling vectors used for standardization, one per subject.
cluster_modelsList of cluster-specific models (each with
Wandn_members).methodCharacter string indicating the method used (e.g., "TwoStep_PCA").
BICSet to
NA(k-means does not have a canonical BIC).ARIAdjusted Rand Index if
z_trueis provided, otherwiseNA.
Details
The algorithm addresses sign indeterminacy by normalizing each basis vector so that its maximum absolute value element is positive. Tie-breaking in majority vote is handled by computing the mean distance from a subject's basis vectors to each tied cluster centroid and selecting the closer one.
Step 1.5 (basis alignment) uses the Hungarian algorithm to align basis vectors across subjects. This ensures that corresponding components (e.g., the first component of each subject) represent similar patterns, which is crucial for accurate clustering. Without alignment, the arbitrary ordering of components from individual PCA/FA can lead to poor clustering performance.
Step 4 (re-optimization) fits cluster-specific models in standardized space
to ensure consistency with Step 1. This improves reconstruction metrics (SSE/VAF)
but does not change ARI unless refine_assignments = TRUE.
If refine_assignments = TRUE, a single reassignment pass is performed
after fitting cluster-specific models. Each subject is reassigned to the cluster
that minimizes reconstruction error. This can improve ARI while maintaining the
two-step nature of the baseline (unlike iterative EM which would drift toward
the mixture model).
For FA decomposition, the function uses psych::fa() if available.
If the psych package is not installed or if FA fails for a subject,
the function falls back to PCA with a warning.
Examples
if (FALSE) { # \dontrun{
sim <- simulate_mixture_data(N = 50, K = 3, r = 2, M = 6, T_each = 100)
result <- fit_two_step_baseline(
sim$list_of_data, r = 2, K = 3, method = "PCA",
seed = 123, z_true = sim$z_true
)
print(result$ARI)
} # }