diff --git a/R/predict.R b/R/predict.R index aac4c71e..1c26b8d5 100644 --- a/R/predict.R +++ b/R/predict.R @@ -405,6 +405,9 @@ predict.mixo_pls <- newdata[which(!is.na(ind.match))] = lapply(which(!is.na(ind.match)), function(x){sweep(newdata[[x]], 2, STATS = attr(X[[x]], "scaled:center"))}) if (scale) newdata[which(!is.na(ind.match))] = lapply(which(!is.na(ind.match)), function(x){sweep(newdata[[x]], 2, FUN = "/", STATS = attr(X[[x]], "scaled:scale"))}) + if (any(unlist(lapply(newdata[which(!is.na(ind.match))], function(x){any(is.infinite(x))})))) { + newdata[which(!is.na(ind.match))] <- lapply(which(!is.na(ind.match)), function(x){df <- newdata[[x]]; df[which(is.infinite(df))] <- NaN; return(df)}) + } means.Y = matrix(attr(Y, "scaled:center"),nrow=nrow(newdata[[1]]),ncol=q,byrow=TRUE); if (scale) diff --git a/tests/testthat/test-auroc.R b/tests/testthat/test-auroc.R index 1b10405c..74981fc2 100644 --- a/tests/testthat/test-auroc.R +++ b/tests/testthat/test-auroc.R @@ -21,3 +21,27 @@ test_that("auroc works", { rbind(0.9981, 7.124e-09)) }) + + + +test_that("Safely handles zero var (non-zero center) features", { + + X1 <- data.frame(matrix(rnorm(100000, 5, 5), nrow = 100)) + X2 <- data.frame(matrix(rnorm(150000, 5, 5), nrow = 100)) + Y <- c(rep("A", 50), rep("B", 50)) + + X <- list(block1=X1, block2=X2) + + list.keepX <- list(block1=c(15, 15), block2=c(30,30)) + + X$block1[,1] <- rep(1, 100) + model = suppressWarnings(block.splsda(X = X, Y = Y, ncomp = 2, + keepX = list.keepX, design = "full")) + + set.seed(9425) + auc.splsda = .quiet(auroc(model)) + + .expect_numerically_close(auc.splsda$block1$comp1[[1]], 0.815) + .expect_numerically_close(auc.splsda$block2$comp2[[2]], 2.22e-16) + +}) \ No newline at end of file