tests/testthat/test_quantreg.R

library(blockForest)
context("blockForest_quantreg")

rf.quant <- blockForest(mpg ~ ., mtcars[1:26, ], quantreg = TRUE, 
                   keep.inbag = TRUE, num.trees = 500)
pred.quant <- predict(rf.quant, mtcars[27:32, ], type = "quantiles")

test_that("Quantile prediction is of correct size", {
  expect_equal(dim(pred.quant$predictions), c(pred.quant$num.samples, 3))
})

test_that("Quantile OOB prediction is of correct size", {
  pred <- predict(rf.quant, data = NULL, type = "quantiles")
  expect_equal(dim(pred$predictions), c(rf.quant$num.samples, 3))
})

test_that("Lower quantile smaller central smaller upper", {
  expect_true(all(pred.quant$predictions[, 1] < pred.quant$predictions[, 2]))
  expect_true(all(pred.quant$predictions[, 2] < pred.quant$predictions[, 3]))
})

test_that("Working for single quantile", {
  expect_silent(pred <- predict(rf.quant, data = mtcars[27:32, ], type = "quantiles", quantiles = .5))
  expect_silent(pred <- predict(rf.quant, data = NULL, type = "quantiles", quantiles = .5))
})

test_that("Working for single new observation", {
  expect_silent(pred <- predict(rf.quant, mtcars[27, ], type = "quantiles"))
})

test_that("Error message if no regression forest", {
  rf <- blockForest(Species ~ ., iris, num.trees = 5)
  expect_error(predict(rf, iris, type = "quantiles"), 
               "Error\\: Quantile prediction implemented only for regression outcomes\\.")
})

test_that("Error message if not grown with quantreg=TRUE", {
  rf <- blockForest(mpg ~ ., mtcars[1:26, ], quantreg = FALSE, num.trees = 50)
  expect_error(predict(rf, mtcars[27:32, ], type = "quantiles"), 
               "Error\\: Set quantreg\\=TRUE in blockForest\\(\\.\\.\\.\\) for quantile prediction\\.")
})

Try the blockForest package in your browser

Any scripts or data that you put into this service are public.

blockForest documentation built on April 3, 2023, 5:49 p.m.