Metrics Reference: Selection Criteria for K and r
Yusuke Matsui
2026-08-14
Source:vignettes/metrics_reference.Rmd
metrics_reference.RmdIntroduction
This reference document provides detailed explanations of all metrics used for selecting the optimal number of factors (r) and clusters (K) in mixture-based muscle synergy analysis. Each metric has specific strengths and limitations, making it important to understand when and how to use each one.
Quick Reference Table
| Metric | Purpose | Direction | Best For | Limitations |
|---|---|---|---|---|
| CV | Generalization | Lower | Global r estimation | Computationally expensive |
| VAF | Reconstruction | Higher | Minimum r threshold | Cannot detect overfitting |
| BIC | Model complexity | Lower | Joint K,r selection | Sensitive to sample size |
| Smoothness | Temporal quality | Lower | EMG-specific validation | Domain-specific |
| VSS | Interpretability | Higher | Post-rotation evaluation | Requires rotation |
| PA | Dimension estimation | Count | Exploratory analysis | Conservative |
Detailed Metric Descriptions
Cross-Validation (CV)
Theoretical Background
Cross-validation estimates the model’s ability to generalize to unseen data. In the context of factor analysis, we use leave-one-subject-out CV:
For each subject : 1. Fit model on subjects (excluding ) 2. Compute error on held-out subject 3. Average errors across all subjects
Mathematical Formulation
Negative Log-Likelihood CV:
Frobenius Norm CV:
where are parameters estimated without subject .
Implementation
library(synergyMixR)
# CV-based r selection
result_cv <- select_r(
list_of_data,
method = "cv",
rvec = 2:6,
model_type = "MFA"
)
# View results
print(result_cv)
cat("Selected r:", result_cv$r_selected, "\n")
# Visualize
plot(result_cv$rvec, result_cv$cv_errors, type = "b",
xlab = "r", ylab = "CV Error",
main = "Cross-Validation Curve")
abline(v = result_cv$r_selected, col = "red", lty = 2)Variance Accounted For (VAF)
Theoretical Background
VAF measures the proportion of total variance in the data explained by the model. It is analogous to in regression.
Mathematical Formulation
where: - is the observed data for subject - is the reconstructed data - is the grand mean across all subjects
Implementation
# Compute VAF for a fitted model
vaf <- compute_global_vaf_mfa(list_of_data, mfa_fit)
cat("Global VAF:", round(vaf, 4), "\n")
# VAF-based r selection
result_vaf <- select_r_by_vaf_mfa(
list_of_data,
K = 3,
rvec = 2:6,
vaf_threshold = 0.90 # Select smallest r achieving 90% VAF
)
cat("Selected r:", result_vaf$best_r, "\n")
print(result_vaf$summary)VAF Thresholds in Literature
| Domain | Typical Threshold | Reference |
|---|---|---|
| Muscle synergy (locomotion) | 0.90 | Ivanenko et al., 2004 |
| Muscle synergy (reaching) | 0.85-0.90 | d’Avella et al., 2006 |
| General EMG analysis | 0.80-0.85 | Tresch et al., 2006 |
Caution: VAF Alone is Insufficient
VAF increases monotonically with r. Choosing r based only on VAF will favor overfitting:
# Demonstrate VAF's monotonic increase
for (r in 1:6) {
fit <- mfa_em_fit(list_of_data, K = 3, r = r, n_init = 1)
vaf_r <- compute_global_vaf_mfa(list_of_data, fit)
cat("r =", r, ": VAF =", round(vaf_r, 4), "\n")
}
# Output: VAF always increases with rSolution: Use VAF as a threshold constraint, not the primary selection criterion.
Bayesian Information Criterion (BIC)
Theoretical Background
BIC balances model fit (likelihood) against model complexity (number of parameters). It penalizes complex models to prevent overfitting.
Mathematical Formulation
where: - is the maximized likelihood - is the number of free parameters - is the sample size (total time points)
For MFA:
- : loadings per cluster
- : means and diagonal noise per cluster
- : mixing proportions
Implementation
# Compute BIC for a single model
loglik <- mfa_fit$logLik
bic <- compute_BIC_mfa(loglik, K = 3, r = 4, M = 8, N_total_rows = 6000)
# Grid search with BIC selection
grid_result <- select_optimal_K_r_mfa(
list_of_data,
Kvec = 1:5,
rvec = 1:5
)
# View BIC surface
print(grid_result$summary)
# Visualize BIC heatmap
library(ggplot2)
ggplot(grid_result$summary, aes(x = r, y = K, fill = BIC)) +
geom_tile() +
scale_fill_viridis_c(direction = -1) +
geom_point(data = grid_result$best_model_info,
aes(x = r, y = K), color = "red", size = 4)Smoothness Penalty
Theoretical Background
The smoothness penalty is designed for time-series data like EMG. It measures how “wiggly” the factor scores are over time. When r is too large, models tend to fit high-frequency noise, producing non-smooth factors.
Mathematical Formulation
Using second-order differences:
This is equivalent to summing the squared discrete second derivatives of all factor score time series.
Implementation
# Compute factor scores first
mfa_fit <- compute_factor_scores_mfa(mfa_fit, list_of_data)
# Compute smoothness
smoothness <- compute_smoothness_penalty(mfa_fit$factor_scores)
cat("Smoothness penalty:", smoothness, "\n")
# Compare smoothness across r values
for (r in 2:6) {
fit <- mfa_em_fit(list_of_data, K = 3, r = r, n_init = 1)
fit <- compute_factor_scores_mfa(fit, list_of_data)
smooth_r <- compute_smoothness_penalty(fit$factor_scores)
cat("r =", r, ": Smoothness =", round(smooth_r, 2), "\n")
}Interpretation
| Smoothness Level | Interpretation |
|---|---|
| Low | Smooth, physiologically plausible activations |
| High | Noisy, potentially overfitting high-frequency components |
Very Simple Structure (VSS)
Theoretical Background
VSS evaluates the interpretability of factor loadings. The concept of “simple structure” (Thurstone, 1947) suggests that each variable (muscle) should load primarily on one factor (synergy).
Mathematical Formulation
Threshold Method:
Counts the proportion of muscles with exactly one loading above threshold .
Ratio Method:
Measures how concentrated each muscle’s loading is on its dominant factor.
Implementation
# Compute VSS
vss_thresh <- compute_vss_criterion(mfa_fit$Lambda, threshold = 0.5, method = "threshold")
vss_ratio <- compute_vss_criterion(mfa_fit$Lambda, method = "ratio")
cat("VSS (threshold):", round(vss_thresh, 4), "\n")
cat("VSS (ratio):", round(vss_ratio, 4), "\n")
# VSS after rotation (recommended)
rotated_fit <- rotate_mfa_model(mfa_fit, rotation = "varimax")
vss_rotated <- compute_vss_criterion(rotated_fit$Lambda, method = "ratio")
cat("VSS after varimax:", round(vss_rotated, 4), "\n")Rotation Importance
Unrotated factor loadings often do not exhibit simple structure. Apply varimax (or another rotation) before computing VSS:
# Without rotation - often lower VSS
vss_unrot <- compute_vss_criterion(mfa_fit$Lambda, rotate = "none")
# With rotation - typically higher VSS
vss_rot <- compute_vss_criterion(mfa_fit$Lambda, rotate = "varimax")
cat("Unrotated VSS:", round(vss_unrot, 4), "\n")
cat("Rotated VSS:", round(vss_rot, 4), "\n")Parallel Analysis (PA)
Theoretical Background
Parallel analysis compares observed eigenvalues to those expected from random data. Factors are retained only if their eigenvalues exceed the random baseline.
Algorithm
- Compute eigenvalues of pooled covariance matrix:
- Generate B random permutations of the data
- For each permutation, compute eigenvalues
- Calculate 95th percentile of random eigenvalues:
- Select r = max{j : }
Implementation
# Global MFA-based r estimation
result_pa <- select_r_global_mfa(
list_of_data,
rvec = 1:8
)
cat("Selected r:", result_pa$r_global, "\n")
# View results
print(result_pa)Metric Comparison and Selection Strategy
Single Metric Usage
| Metric | When to Use Alone |
|---|---|
| CV | Global r estimation with sufficient data |
| BIC | Joint K,r selection via grid search |
| PA | Quick exploratory analysis |
Recommended Combinations
Conservative Selection (Avoid Overfitting)
# Prioritize BIC and smoothness
result <- select_r_multicriteria_mfa(
list_of_data, K = 3, rvec = 2:6,
weights = c(BIC = 2, VAF = 1, smoothness = 2, VSS = 1)
)Interpretability Focus
# Prioritize VSS
result <- select_r_multicriteria_mfa(
list_of_data, K = 3, rvec = 2:6,
weights = c(BIC = 1, VAF = 1, smoothness = 1, VSS = 2)
)Balanced Approach (Recommended Default)
result <- select_r_multicriteria_mfa(
list_of_data, K = 3, rvec = 2:6,
vaf_threshold = 0.85,
weights = c(BIC = 1, VAF = 1, smoothness = 1, VSS = 1)
)Decision Framework
┌─────────────────────────────────────────────────────────────┐
│ Step 1: Establish baseline with global CV │
│ r_cv = select_r(method = "cv", ...)$r_selected │
└────────────────────┬────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Step 2: Check VAF threshold │
│ If VAF(r_cv) < 0.85 → consider larger r │
│ If VAF(r_cv) > 0.95 → may be overfitting, check smoothness │
└────────────────────┬────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Step 3: Validate with secondary metrics │
│ - Check smoothness (should not spike) │
│ - Check VSS after rotation (should be reasonable) │
└────────────────────┬────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Step 4: Joint K,r selection with BIC │
│ Use focused rvec around validated r │
└─────────────────────────────────────────────────────────────┘
Summary
Key Takeaways
- No single metric is perfect - each has strengths and limitations
- CV is the gold standard for generalization but is computationally expensive
- VAF should be a constraint, not the primary criterion
- BIC works well for joint K,r selection with moderate sample sizes
- Smoothness is EMG-specific - leverage temporal structure of your data
- VSS requires rotation - always apply varimax before evaluation
- PA is conservative - useful for exploratory analysis
References
- Ivanenko, Y. P., et al. (2004). Five basic muscle activation patterns account for muscle activity during human locomotion. Journal of Physiology.
- d’Avella, A., et al. (2006). Control of fast-reaching movements by muscle synergy combinations. Journal of Neuroscience.
- Tresch, M. C., et al. (2006). Matrix factorization algorithms for the identification of muscle synergies. Journal of Neurophysiology.
- Revelle, W., & Rocklin, T. (1979). Very simple structure: An alternative procedure for estimating the optimal number of interpretable factors. Multivariate Behavioral Research.
- Horn, J. L. (1965). A rationale and test for the number of factors in factor analysis. Psychometrika.