xgboost/R-package/tests/testthat/test_helpers.R
2015-11-30 15:04:17 +01:00

41 lines
1.3 KiB
R

context('Test helper functions')
require(xgboost)
require(data.table)
require(Matrix)
require(vcd)
set.seed(1994)
data(Arthritis)
data(agaricus.train, package='xgboost')
df <- data.table(Arthritis, keep.rownames = F)
df[,AgeDiscret := as.factor(round(Age / 10,0))]
df[,AgeCat := as.factor(ifelse(Age > 30, "Old", "Young"))]
df[,ID := NULL]
sparse_matrix <- sparse.model.matrix(Improved~.-1, data = df)
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)))
expect_true(xgb.dump(bst, 'xgb.model.dump', with.stats = T))
})
test_that("xgb.importance works", {
importance <- xgb.importance(sparse_matrix@Dimnames[[2]], model = bst)
expect_equal(dim(importance), c(7, 4))
expect_equal(colnames(importance), c("Feature", "Gain", "Cover", "Frequency"))
})
test_that("xgb.plot.tree works", {
xgb.plot.tree(names = agaricus.train$data@Dimnames[[2]], model = bst)
})
test_that("xgb.plot.deepness works", {
xgb.plot.deepness(model = bst)
})
test_that("xgb.plot.multi.trees works", {
xgb.plot.multi.trees(model = bst, names = agaricus.train$data@Dimnames[[2]], 3)
})