Computes the K-fold cross-validation loss of a PhiSpace model over a grid of numbers of components (ncomp) and then over a grid of numbers of features per phenotype (nfeat), and returns the chosen values with the selected features. In an interactive session the function plots each loss curve and asks for a value unless ncomp or nfeat is given. In a non-interactive session it chooses the ncomp with the lowest loss, and nfeat must be given when tune_nfeat = TRUE.

tunePhiSpace(
  reference,
  assayName = "logcounts",
  phenotypes = NULL,
  YY = NULL,
  ncomp = NULL,
  impScores = NULL,
  center = TRUE,
  scale = FALSE,
  tune_ncomp = TRUE,
  tune_nfeat = TRUE,
  ncompLimits = c(1, 100),
  ncompGridSize = 20,
  ncompGrid = NULL,
  selectedFeat = NULL,
  nfeatLimits = c(10, 15000),
  nfeatGridSize = 30,
  nfeatV = NULL,
  Kfolds = 5,
  regMethod = "PLS",
  labelCode = "-1,1",
  Ncores = 1,
  seed = 5202056,
  cellTypeThreshold = NULL,
  nfeat = NULL
)

Arguments

reference

SingleCellExperiment object.

assayName

Character.

phenotypes

Character.

YY

Matrix.

ncomp

Integer. With tune_ncomp = TRUE, the value to use after the cross-validation instead of asking or taking the lowest loss. With tune_ncomp = FALSE, the value to use; NULL uses the number of columns of YY.

impScores

Matrix.

center

Logical.

scale

Logical.

tune_ncomp

Logical: compute the cross-validation loss for each value in the ncomp grid.

tune_nfeat

Logical: compute the cross-validation loss for each value in the nfeat grid.

ncompLimits

Vector.

ncompGridSize

Integer.

ncompGrid

Vector.

selectedFeat

Vector.

nfeatLimits

Vector.

nfeatGridSize

Integer.

nfeatV

Vector.

Kfolds

Integer.

regMethod

Character.

labelCode

Character.

Ncores

Integer.

seed

Integer. The function sets the seed with withr::local_seed(), so the session's random-number state is restored on exit.

cellTypeThreshold

Integer or NULL. If a positive integer, cell types with fewer than this many cells in the reference will be removed before tuning. Only used when phenotypes is provided. Default is NULL (no filtering).

nfeat

Integer. With tune_nfeat = TRUE, the number of features per phenotype to use after the cross-validation instead of asking; required in a non-interactive session.

Value

A list with ncomp, nfeat, impScores (feature importance scores), selectedFeat (selected features), and tuneNcompErrs and tuneNfeatErrs (data frames of the cross-validation loss for each grid value, or NULL when that parameter was not tuned).

Examples

set.seed(1)
ref <- logNormTransf(scuttle::mockSCE(ncells = 200, ngenes = 300))
# Outside an interactive session, nfeat must be given.
tuned <- tunePhiSpace(ref, assayName = "logcounts", phenotypes = "Cell_Cycle",
                      ncompGrid = 1:4, nfeatV = c(20, 50), Kfolds = 3,
                      nfeat = 50)
tuned$ncomp
tuned$tuneNcompErrs