Element-wise cross-validation for nmfkc.net (upper-triangle folds)
Source:R/nmfkc.net.R
nmfkc.net.ecv.Rdk-fold CV with folds taken over the upper triangle of the
symmetric \(Y\) (mirrored to the lower triangle) to prevent information
leakage through symmetry. A single entry point covers all three symmetric
NMF variants; type selects the fitting function:
Usage
nmfkc.net.ecv(Y, rank = 1:3, type = c("tri", "bi", "signed"), ...)Arguments
- Y
Symmetric N x N non-negative matrix.
- rank
Integer vector of ranks to evaluate. Default
1:3.- type
Model type:
"tri"(default),"bi", or"signed".- ...
Passed to the underlying fitter; also accepts:
nfolds(default 5;divalias) andseed(default 123).diag.exclude(defaultFALSE): leave the diagonal out of the folds and out of every fit (seenmfkc.net), for a network without self-loops. Otherwise its zeros would be scored as edges to predict.folds: the folds to use, as a list of linear indices into \(Y\) – e.g.\ the$foldsof an earlier run, so that two models are compared on the same split. A lower-triangle index is read as its upper-triangle mirror. An entry in two folds, or one that cannot be held out (NA, zero weight, or the diagonal underdiag.exclude), is an error.nfoldsis thenlength(folds). (Within this function the folds depend only on \(N\),nfolds,seedand the entries that can be held out, so runs that share these already share their folds.)seeds: a vector of seeds (overridingseed), to repeat the CV over different splits. Each split refits with its seed, so the results for seed \(s\) are those ofseed = s.objfunc,sigmaandr.squared.cvare then averaged over the seeds, with their spread inobjfunc.sdandr.squared.cv.sd. Not withfolds.pred(defaultFALSE): also return the held-out predictions (an \(N \times N\) array per rank; kept optional because it is \(N^2\) per rank).Y.weights: as innmfkc.net. Entries of weight 0 are never held out, every fit keeps the weights, and the held-out loss is the weighted mean. (Before 1.0.0 aY.weightsgiven here was silently replaced by the fold mask.)cores(getOption("mc.cores", 1L)) to evaluate the rank x fold grid in parallel; results are identical to the sequential run for anycores(PSOCK cluster on Windows, forking elsewhere).
NAentries of \(Y\) are never held out and stay masked in every fit (before 1.0.0 they were fitted as zeros and madeobjfuncNA).
Value
A list with objfunc, sigma, objfunc.fold,
folds, Q.grid, type, and
r.squared.cvHeld-out \(R^2\) per rank, pooled over the folds: \(1 - \sum (y - \hat y)^2 / \sum (y - \bar y)^2\) over all held-out upper-triangle entries (weighted when
Y.weightsis given). Unlike ther.squaredof a fit (a squared correlation), it can be negative.predWith
pred = TRUE: \(N \times N \times\) (ranks) array of held-out predictions, mirrored to the lower triangle;NAwhere an entry was never held out.diag.excludeTRUE, present only when used.seeds,objfunc.rep,objfunc.sd,r.squared.cv.rep,r.squared.cv.sd,folds.repPresent only with two or more
seeds: the per-seed values (ranks x seeds), their standard deviations, and each seed's folds.objfunc.fold,foldsandpredare those of the first seed.
Lifecycle
This function is experimental. The interface may change in future versions; details are to be described in an upcoming paper.