tests/testthat/trim_rpart.R

library(caret)

test_that('rpart classification', {
  skip_on_cran()
  set.seed(1)
  tr_dat <- twoClassSim(200)
  te_dat <- twoClassSim(200)
  
  set.seed(2)
  class_trim <- train(Class ~ ., data = tr_dat,
                      method = "rpart",
                      tuneGrid = data.frame(cp = 0.22),
                      trControl = trainControl(method = "none", 
                                               classProbs = TRUE,
                                               trim = TRUE))
  
  set.seed(2)
  class_notrim <- train(Class ~ ., data = tr_dat,
                        method = "rpart",
                        tuneGrid = data.frame(cp = 0.22),
                        trControl = trainControl(method = "none", 
                                                 classProbs = TRUE,
                                                 trim = FALSE))
  
  expect_equal(predict(class_trim,   te_dat),
               predict(class_notrim, te_dat))
  
  expect_equal(predict(class_trim,   te_dat, type = "prob"),
               predict(class_notrim, te_dat, type = "prob"))
  
  expect_less_than(object.size(class_trim)-object.size(class_notrim), 0)
})

test_that('rpart regression', {
  skip_on_cran()
  set.seed(1)
  tr_dat <- SLC14_1(200)
  te_dat <- SLC14_1(200)
  
  set.seed(2)
  reg_trim <- train(y ~ ., data = tr_dat,
                    method = "rpart",
                    tuneGrid = data.frame(cp = 0.12),
                    trControl = trainControl(method = "none", 
                                             trim = TRUE))
  
  set.seed(2)
  reg_notrim <- train(y ~ ., data = tr_dat,
                      method = "rpart",
                      tuneGrid = data.frame(cp = 0.12),
                      trControl = trainControl(method = "none", 
                                               trim = FALSE))
  expect_equal(predict(reg_trim,   te_dat),
               predict(reg_notrim, te_dat))
  expect_less_than(object.size(reg_trim)-object.size(reg_notrim), 0)
})


test_that('rpart2 classification', {
  skip_on_cran()
  set.seed(1)
  tr_dat <- twoClassSim(200)
  te_dat <- twoClassSim(200)
  
  set.seed(2)
  class_trim <- train(Class ~ ., data = tr_dat,
                      method = "rpart2",
                      tuneGrid = data.frame(maxdepth = 3),
                      trControl = trainControl(method = "none", 
                                               classProbs = TRUE,
                                               trim = TRUE))
  
  set.seed(2)
  class_notrim <- train(Class ~ ., data = tr_dat,
                        method = "rpart2",
                        tuneGrid = data.frame(maxdepth = 3),
                        trControl = trainControl(method = "none", 
                                                 classProbs = TRUE,
                                                 trim = FALSE))
  
  expect_equal(predict(class_trim,   te_dat),
               predict(class_notrim, te_dat))
  
  expect_equal(predict(class_trim,   te_dat, type = "prob"),
               predict(class_notrim, te_dat, type = "prob"))
  
  expect_less_than(object.size(class_trim)-object.size(class_notrim), 0)
})

test_that('rpart2 regression', {
  skip_on_cran()
  set.seed(1)
  tr_dat <- SLC14_1(200)
  te_dat <- SLC14_1(200)
  
  set.seed(2)
  reg_trim <- train(y ~ ., data = tr_dat,
                    method = "rpart2",
                    tuneGrid = data.frame(maxdepth = 3),
                    trControl = trainControl(method = "none", 
                                             trim = TRUE))
  
  set.seed(2)
  reg_notrim <- train(y ~ ., data = tr_dat,
                      method = "rpart2",
                      tuneGrid = data.frame(maxdepth = 3),
                      trControl = trainControl(method = "none", 
                                               trim = FALSE))
  expect_equal(predict(reg_trim,   te_dat),
               predict(reg_notrim, te_dat))
  expect_less_than(object.size(reg_trim)-object.size(reg_notrim), 0)
})

Try the caret package in your browser

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

caret documentation built on Aug. 9, 2022, 5:11 p.m.