From e23f4ec3db905134c89dec20db75fef694baac02 Mon Sep 17 00:00:00 2001 From: terrytangyuan Date: Fri, 30 Oct 2015 19:48:00 -0500 Subject: [PATCH] Minor addition to R unit tests --- R-package/tests/testthat/test_basic.R | 5 ++++- R-package/tests/testthat/test_helpers.R | 8 +++++--- R-package/tests/testthat/test_poisson_regression.R | 5 +++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/R-package/tests/testthat/test_basic.R b/R-package/tests/testthat/test_basic.R index 2e4e54902..34d47103f 100644 --- a/R-package/tests/testthat/test_basic.R +++ b/R-package/tests/testthat/test_basic.R @@ -6,14 +6,15 @@ data(agaricus.train, package='xgboost') data(agaricus.test, package='xgboost') train <- agaricus.train test <- agaricus.test +set.seed(1994) test_that("train and predict", { bst <- xgboost(data = train$data, label = train$label, max.depth = 2, eta = 1, nthread = 2, nround = 2, objective = "binary:logistic") pred <- predict(bst, test$data) + expect_equal(length(pred), 1611) }) - test_that("early stopping", { res <- xgb.cv(data = train$data, label = train$label, max.depth = 2, nfold = 5, eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic", @@ -23,6 +24,7 @@ test_that("early stopping", { eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic", early.stop.round = 3, maximize = FALSE) pred <- predict(bst, test$data) + expect_equal(length(pred), 1611) }) test_that("save_period", { @@ -30,4 +32,5 @@ test_that("save_period", { eta = 0.3, nthread = 2, nround = 20, objective = "binary:logistic", save_period = 10, save_name = "xgb.model") pred <- predict(bst, test$data) + expect_equal(length(pred), 1611) }) diff --git a/R-package/tests/testthat/test_helpers.R b/R-package/tests/testthat/test_helpers.R index 0ac6b388e..95e8d2d1c 100644 --- a/R-package/tests/testthat/test_helpers.R +++ b/R-package/tests/testthat/test_helpers.R @@ -5,6 +5,7 @@ require(data.table) require(Matrix) require(vcd) +set.seed(1994) data(Arthritis) data(agaricus.train, package='xgboost') df <- data.table(Arthritis, keep.rownames = F) @@ -16,15 +17,16 @@ output_vector <- df[,Y := 0][Improved == "Marked",Y := 1][,Y] bst <- xgboost(data = sparse_matrix, label = output_vector, max.depth = 9, eta = 1, nthread = 2, nround = 10,objective = "binary:logistic") - test_that("xgb.dump works", { - capture.output(print(xgb.dump(bst))) + dump <- xgb.dump(bst) + expect_equal(length(dump, 172)) }) test_that("xgb.importance works", { - xgb.dump(bst, 'xgb.model.dump', with.stats = T) + expect_true(xgb.dump(bst, 'xgb.model.dump', with.stats = T)) importance <- xgb.importance(sparse_matrix@Dimnames[[2]], 'xgb.model.dump') expect_equal(dim(importance), c(7, 4)) + expect_equal(colnames(importance), c("Feature", "Gain", "Cover", "Frequence")) }) test_that("xgb.plot.tree works", { diff --git a/R-package/tests/testthat/test_poisson_regression.R b/R-package/tests/testthat/test_poisson_regression.R index c28820774..c5389dd0f 100644 --- a/R-package/tests/testthat/test_poisson_regression.R +++ b/R-package/tests/testthat/test_poisson_regression.R @@ -1,6 +1,7 @@ context('Test poisson regression model') require(xgboost) +set.seed(1994) test_that("poisson regression works", { data(mtcars) @@ -9,5 +10,5 @@ test_that("poisson regression works", { expect_equal(class(bst), "xgb.Booster") pred <- predict(bst,as.matrix(mtcars[, -11])) expect_equal(length(pred), 32) - sqrt(mean( (pred - mtcars[,11]) ^ 2)) -}) \ No newline at end of file + expect_equal(sqrt(mean( (pred - mtcars[,11]) ^ 2)), 1.16, tolerance = 0.01) +})