Skip to content

Commit

Permalink
more tests for missings
Browse files Browse the repository at this point in the history
  • Loading branch information
mnwright committed Jul 2, 2024
1 parent 0ab1efb commit e779a65
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion tests/testthat/test_missings.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ test_that("Training works with missing values in x but not in y", {
dat <- iris
dat[25, 1] <- NA
expect_silent(ranger(Species ~ ., dat, num.trees = 5))
expect_silent(ranger(Petal.Width ~ ., dat, num.trees = 5))
expect_error(ranger(Sepal.Length ~ ., dat, num.trees = 5), "Missing data in dependent variable.")

dat <- iris
dat[4, 5] <- NA
Expand All @@ -35,7 +37,7 @@ test_that("No error if missing value in irrelevant column, prediction", {
expect_silent(predict(rf, dat))
})

test_that("Prediction works with missing values", {
test_that("Prediction works with missing values, classification", {
rf <- ranger(Species ~ ., iris, num.trees = 5, write.forest = TRUE)

dat <- iris
Expand All @@ -44,6 +46,15 @@ test_that("Prediction works with missing values", {
expect_silent(predict(rf, dat))
})

test_that("Prediction works with missing values, regression", {
rf <- ranger(Sepal.Width ~ ., iris, num.trees = 5, write.forest = TRUE)

dat <- iris
dat[4, 4] <- NA
dat[25, 1] <- NA
expect_silent(predict(rf, dat))
})

test_that("Order splitting working with missing values for classification", {
n <- 20
dt <- data.frame(x = sample(c("A", "B", "C", "D", NA), n, replace = TRUE),
Expand All @@ -63,3 +74,10 @@ test_that("Order splitting working with missing values for multiclass classifica
rf <- ranger(y ~ ., data = dt, num.trees = 5, min.node.size = n/2, respect.unordered.factors = 'order')
expect_true(all(rf$forest$is.ordered))
})

test_that("Missing values for survival not yet working", {
dat <- veteran
dat[1, 1] <- NA

expect_error(ranger(Surv(time, status) ~ ., dat, num.trees = 5), "Error: Missing value handling not yet implemented for survival forests\\.")
})

0 comments on commit e779a65

Please sign in to comment.