Skip to content

Commit

Permalink
PR addressing Issue #122 (#227)
Browse files Browse the repository at this point in the history
fix: added specific check to `perf()` to notify user when there is a single sample associated with one of the classes in a DA context

test: included a basic test for function and to test functionality of new singple sample check
  • Loading branch information
Max-Bladen authored Dec 13, 2022
1 parent 6ea2b80 commit 7e7b4c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions R/perf.R
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,12 @@ perf.mixo_plsda <- function(object,
warning("Leave-One-Out validation does not need to be repeated: 'nrepeat' is set to '1'.")
nrepeat = 1
}

if (any(table(object$Y) <= 1)) {
stop(paste("Cannot evaluate performance when a class level ('",
names(table(object$Y))[which(table(object$Y) == 1)],
"') has only a single assocaited sample.", sep = ""))
}

if (nrepeat < 3 && validation != "loo") {
warning("Values in '$choice.ncomp' will reflect component count with the minimum error rate rather than the best based on a one-way t.test")
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-perf.mixo.splsda.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test_that("perf.mixo_splsda functions", code = {
data(liver.toxicity)
X <- liver.toxicity$gene
Y <- liver.toxicity$treatment$Dose.Group

set.seed(12)
res <- plsda(X, Y, ncomp = 2)
out <- perf(res, validation = "Mfold", folds = 3, nrepeat = 3)

ground.ncomp <- matrix(c(2,1,2,2,1,2), ncol = 3, byrow=T,
dimnames = list(c("overall", "BER"),
c("max.dist", "centroids.dist", "mahalanobis.dist")))

expect_equal(out$choice.ncomp, ground.ncomp)
})

test_that("does not allow for class with 1 associated sample", code = {
data(liver.toxicity)
X <- liver.toxicity$gene
Y <- liver.toxicity$treatment$Dose.Group
# create a class with one sample only
Y[c(1)] <- 'random.class'

res <- plsda(X, Y, ncomp = 2)

expect_error(perf(res, validation = "Mfold", folds = 3, nrepeat = 3),
"single assocaited")
})

0 comments on commit 7e7b4c9

Please sign in to comment.