Debian Codegraph @

tests/testthat/test_monotone.R

raw
1 context("monotone constraints")
2
3 set.seed(1024)
4 x <- rnorm(1000, 10)
5 y <- -1 * x + rnorm(1000, 0.001) + 3 * sin(x)
6 train <- matrix(x, ncol = 1)
7
8
9 test_that("monotone constraints for regression", {
10 bst <- xgb.train(
11 data = xgb.DMatrix(train, label = y, nthread = 1),
12 nrounds = 100, verbose = 0,
13 params = xgb.params(
14 max_depth = 2,
15 learning_rate = 0.1,
16 nthread = 2,
17 monotone_constraints = -1
18 )
19 )
20
21 pred <- predict(bst, train)
22
23 ind <- order(train[, 1])
24 pred.ord <- pred[ind]
25 expect_true({
26 !any(diff(pred.ord) > 0)
27 }, "Monotone constraint satisfied")
28 })