Skip to contents

nmfkc.ecv performs k-fold cross-validation by randomly holding out individual elements of the data matrix (element-wise), assigning them a weight of 0 via Y.weights, and evaluating the reconstruction error on those held-out elements.

This method (also known as Wold's CV) is theoretically robust for determining the optimal rank (Q) in NMF. This function supports vector input for Q, allowing simultaneous evaluation of multiple ranks on the same folds.

When Y.symmetric = "bi" or "tri" is passed via ..., fold creation uses only the upper triangle (including the diagonal) to prevent information leakage through the symmetric entries \(Y_{ij} = Y_{ji}\).

Usage

nmfkc.ecv(Y, A = NULL, rank = 1:3, data, ...)

Arguments

Y

Observation matrix, or a formula (see nmfkc for Formula Mode).

A

Covariate matrix. Ignored when Y is a formula.

rank

Vector of ranks to evaluate (e.g., 1:5). For backward compatibility, Q is accepted via ....

data

A data frame (required when Y is a formula with column names).

...

Additional arguments passed to nmfkc (e.g., method="EU"). Also accepts: nfolds (number of folds, default 5; div also accepted), seed (integer seed, default 123).

Value

A list with components:

objfunc

Numeric vector containing the Mean Squared Error (MSE) for each Q.

sigma

Numeric vector containing the Residual Standard Error (RMSE) for each Q. Only available if method="EU".

objfunc.fold

List of length equal to Q vector. Each element contains the MSE values for the k folds.

folds

A list of length div, containing the linear indices of held-out elements for each fold (shared across all Q).

See also

Examples

# Element-wise CV to select rank
Y <- t(iris[1:30, 1:4])
res <- nmfkc.ecv(Y, rank = 1:2, nfolds = 3)
#> Performing Element-wise CV for Q = 1,2 (3-fold)...
res$objfunc
#>       Q=1       Q=2 
#> 0.2597611 0.4376291