2019-05-31 22:13:24 +00:00
|
|
|
context("Train, save, and load without error")
|
|
|
|
|
|
|
|
test_that("Can save & load regression example", {
|
|
|
|
|
2019-11-14 01:07:58 +00:00
|
|
|
if(file.exists("trees_saving_loading")){
|
|
|
|
unlink("trees_saving_loading", recursive=TRUE)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect_false(file.exists("trees_saving_loading")) # Folder shouldn't exist at this point
|
2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
x1 <- rnorm(1000)
|
|
|
|
x2 <- rnorm(1000)
|
|
|
|
y <- 1 + x1 + x2 + rnorm(1000)
|
|
|
|
|
|
|
|
data <- data.frame(x1, x2, y)
|
|
|
|
forest <- train(y ~ x1 + x2, data,
|
2019-06-19 20:14:11 +00:00
|
|
|
ntree=100, numberOfSplits = 5, mtry = 1, nodeSize = 5,
|
|
|
|
displayProgress=FALSE)
|
2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
|
2019-06-06 22:53:25 +00:00
|
|
|
saveForest(forest, "trees_saving_loading")
|
|
|
|
new_forest <- loadForest("trees_saving_loading")
|
2019-05-31 22:13:24 +00:00
|
|
|
|
|
|
|
# try making a little prediction to verify it works
|
|
|
|
newData <- data.frame(x1=seq(from=-3, to=3, by=0.5), x2=0)
|
|
|
|
predictions <- predict(new_forest, newData)
|
|
|
|
|
|
|
|
expect_true(file.exists("trees_saving_loading")) # show Ok if we got this far
|
|
|
|
unlink("trees_saving_loading", recursive=TRUE)
|
|
|
|
|
|
|
|
})
|