Skip to content

Commit

Permalink
fix all skipped tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach Deane-Mayer committed Jun 26, 2024
1 parent c059702 commit fe24b61
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion R/caretList.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ trControlCheck <- function(x, y) {
stop("Please pass exactly 1 argument to savePredictions, e.g. savePredictions='final'")
}

if (x$savePredictions) {
if (x$savePredictions %in% c(TRUE)) {
warning("x$savePredictions == TRUE is depreciated. Setting to 'final' instead.")
x$savePredictions <- "final"
}
Expand Down
5 changes: 1 addition & 4 deletions R/helper_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,7 @@ extractModelTypes <- function(list_of_models) {
#' @importFrom data.table data.table setorderv
bestPreds <- function(x) {
stopifnot(is(x, "train"))
stopifnot({
x$control$savePredictions %in% c("all", "final") |
x$control$savePredictions
})
stopifnot(x$control$savePredictions %in% c("all", "final", TRUE))
a <- data.table(x$bestTune, key = names(x$bestTune))
b <- data.table(x$pred, key = names(x$bestTune))
b <- b[a, ]
Expand Down
7 changes: 3 additions & 4 deletions tests/testthat/test-a-skip.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
context("Test skips are working correctly")
test_that("Skips work correctly", {
skip("Basic skip failed")
expect_equal(1, 0)
})
test_that("Skips work correctly", {
skip_on_travis()
})
test_that("Skips work correctly", {
test_that("Skips work correctly on CRAN", {
skip_on_cran()
expect_equal(1, 1)
})
6 changes: 4 additions & 2 deletions tests/testthat/test-caretStack.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
context("Does stacking and prediction work?")
library(caret)

data(models.reg)
Expand All @@ -9,6 +8,9 @@ data(models.class)
data(X.class)
data(Y.class)


context("Does stacking and prediction work?")

test_that("We can stack regression models", {
set.seed(96367)
ens.reg <- caretStack(
Expand Down Expand Up @@ -43,7 +45,6 @@ test_that("We can stack classification models", {
})

test_that("caretStack plots", {
skip_if_not_installed("gbm")
test_plot_file <- "caretEnsemble_test_plots.png"
ens.reg <- caretStack(
models.reg,
Expand All @@ -55,6 +56,7 @@ test_that("caretStack plots", {
dotplot(ens.reg, metric = "RMSE")
dev.off()
unlink(test_plot_file)
expect_is(ens.reg, "caretStack")
})

context("Prediction errors for caretStack work as expected")
Expand Down
29 changes: 14 additions & 15 deletions tests/testthat/test-ensembleMethods.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@ test_that("We can get variable importance in ensembles", {
expect_is(varImp(ens.reg, scale = TRUE, weight = TRUE), "data.frame")
})

test_that("We get warnings when scale is set to FALSE and weight is TRUE", {
skip_on_cran()
test_that("varImp works for caretEnsembles", {
set.seed(2239)
ens.class <- caretEnsemble(models.class, trControl = trainControl(method = "none"))
# varImp struggles with the rf in our test suite, why?
models.subset <- models.reg[2:4]
class(models.subset) <- "caretList"
ens.reg <- caretEnsemble(models.subset, trControl = trainControl(method = "none"))
i <- varImp(ens.reg, scale = FALSE, weight = TRUE)
i <- varImp(ens.class, scale = FALSE, weight = TRUE)
i <- varImp(ens.reg, scale = FALSE, weight = TRUE)
i <- varImp(ens.class, scale = FALSE, weight = TRUE)
i <- varImp(ens.reg, scale = FALSE)
i <- varImp(ens.class, scale = FALSE)
i <- varImp(ens.reg, scale = FALSE)
i <- varImp(ens.class, scale = FALSE)
for (models in list(models.class, models.reg)) {
ens <- caretEnsemble(models, trControl = trainControl(method = "none"))
expected_names <- c("overall", names(ens$models))
expected_row_names <- c("Intercept", "Speciesversicolor", "Speciesvirginica", "Petal.Width", "Sepal.Width", "Petal.Length")
for (s in c(TRUE, FALSE)) {
for (w in c(TRUE, FALSE)) {
i <- varImp(ens, scale = s, weight = w)
expect_is(i, "data.frame")
expect_equal(names(i), expected_names)
expect_equal(row.names(i), expected_row_names)
}
}
}
})

test_that("We get the right dimensions back", {
Expand Down
5 changes: 2 additions & 3 deletions tests/testthat/test-helper_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ w1 <- c(0.1, 0.1, 0.1, 0.7)

test_that("wtd.sd applies weights correctly", {
expect_error(caretEnsemble:::wtd.sd(x))
expect_false(sd(x1) == wtd.sd(x1, w = x2))
expect_false(sd(x1) == wtd.sd(x1, w = x2))
expect_false(sd(x1) == caretEnsemble:::wtd.sd(x1, w = x2))
expect_false(sd(x1) == caretEnsemble:::wtd.sd(x1, w = x2))
expect_equal(caretEnsemble:::wtd.sd(y, w = w1), 7.84, tolerance = .001)
expect_equal(caretEnsemble:::wtd.sd(y, w = w1 * 100), caretEnsemble:::wtd.sd(y, w = w1))
})
Expand All @@ -131,7 +131,6 @@ test_that("wtd.sd handles NA values correctly", {

test_that("Checks generate errors", {
skip_on_cran()
skip_if_not_installed("rpart")
set.seed(42)
myControl <- trainControl(method = "cv", number = 5, savePredictions = "final")
suppressWarnings(
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
context("Parallelization works")
test_that("predict.caretEnsemble works in parallel", {
skip_on_cran()
pbapply::pboptions(type = "none")
X_reg <- model.matrix(~., iris[, -1])
X_reg_big <- do.call(rbind, lapply(1:100, function(x) X_reg))
Y_reg <- iris[, 1]
Expand Down

0 comments on commit fe24b61

Please sign in to comment.