diff --git a/.travis.yml b/.travis.yml index b63bce269..8f70c6877 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,46 +1,52 @@ -# Sample .travis.yml for R projects from https://github.com/craigcitro/r-travis - -language: c +language: r +warnings_are_errors: false +sudo: required env: global: - - WARNINGS_ARE_ERRORS=1 - - R_BUILD_ARGS=" " - - R_CHECK_ARGS="--as-cran" - - BOOTSTRAP_LATEX="1" + - CRAN: http://cran.rstudio.com + - R_BUILD_ARGS="--no-build-vignettes --no-manual --no-examples" + - R_CHECK_ARGS="--no-build-vignettes --no-manuall --no-examples --as-cran" matrix: - NOT_CRAN="true" - NOT_CRAN="false" before_install: - cd pkg/caret - - curl -OL http://raw.github.com/craigcitro/r-travis/master/scripts/travis-tool.sh - - chmod 755 ./travis-tool.sh - - ./travis-tool.sh bootstrap - -install: - - ./travis-tool.sh install_deps - - sudo add-apt-repository -y ppa:texlive-backports/ppa + - sudo add-apt-repository ppa:texlive-backports/ppa -y - sudo apt-get -qq update - - ./travis-tool.sh aptget_install texlive-base - - ./travis-tool.sh aptget_install biblatex - - ./travis-tool.sh aptget_install texlive-latex-base - - ./travis-tool.sh aptget_install texlive-latex-recommended - - ./travis-tool.sh aptget_install texlive-latex-extra - - ./travis-tool.sh aptget_install texlive-fonts-recommended - - ./travis-tool.sh aptget_install texlive-fonts-extra - - ./travis-tool.sh aptget_install texlive-science - - ./travis-tool.sh github_package jimhester/covr - -script: ./travis-tool.sh run_tests -on_failure: - - ./travis-tool.sh dump_logs - -after_success: - - Rscript -e 'library(covr);coveralls()' +apt_packages: + - texlive-base + - biblatex + - texlive-latex-base + - texlive-latex-recommended + - texlive-latex-extra + - texlive-fonts-recommended + - texlive-fonts-extra + - texlive-science + +r_binary_packages: + - arm + - glmnet + - rpart + - C50 + - ipred + - plyr + - earth + - kknn + +r_github_packages: + - jimhester/covr notifications: email: on_success: change on_failure: change + +after_script: + - Rscript -e 'library(devtools);test()' + - dump_logs + +after_success: + - Rscript -e 'library(covr);coveralls()' diff --git a/pkg/caret/DESCRIPTION b/pkg/caret/DESCRIPTION index e44dd6d86..5e4d209fc 100644 --- a/pkg/caret/DESCRIPTION +++ b/pkg/caret/DESCRIPTION @@ -46,8 +46,9 @@ Suggests: RANN, spls, subselect, - pamr, - superpc, + pamr, + superpc, Cubist, - testthat (>= 0.9.1) + testthat (>= 0.9.1), + kknn License: GPL (>= 2) diff --git a/pkg/caret/NAMESPACE b/pkg/caret/NAMESPACE index 4160a518c..a5cdcc118 100644 --- a/pkg/caret/NAMESPACE +++ b/pkg/caret/NAMESPACE @@ -69,7 +69,7 @@ export(anovaScores, gafs_spCrossover, gafs_raMutation, gafs, - gafs.default, + gafs.default, gafsControl, gamFormula, gamFuncs, @@ -122,7 +122,7 @@ export(anovaScores, nullModel, nullModel.default, oneSE, - panel.calibration, + panel.calibration, panel.lift, panel.lift2, panel.needle, @@ -179,16 +179,16 @@ export(anovaScores, rfeControl, rfeIter, rfFuncs, - rfGA, + rfGA, rfSA, rfSBF, rfStats, RMSE, safs_initial, - safs_perturb, + safs_perturb, safs_prob, safs, - safs.default, + safs.default, safsControl, sbf, sbf.default, @@ -308,7 +308,7 @@ S3method(varImp, nnet) S3method(varImp, glmnet) S3method(varImp, gam) S3method(varImp, gafs) -S3method(varImp, safs) +S3method(varImp, safs) S3method(densityplot, train) S3method(histogram, train) @@ -346,7 +346,7 @@ S3method(plot, prcomp.resamples) S3method(plot, lift) S3method(plot, calibration) S3method(plot, gafs) -S3method(plot, safs) +S3method(plot, safs) S3method(confusionMatrix, train) S3method(confusionMatrix, rfe) @@ -387,7 +387,7 @@ S3method(print, lift) S3method(print, calibration) S3method(print, expoTrans) S3method(print, gafs) -S3method(print, safs) +S3method(print, safs) S3method(predict, plsda) S3method(predict, splsda) @@ -410,7 +410,7 @@ S3method(predict, dummyVars) S3method(predict, BoxCoxTrans) S3method(predict, expoTrans) S3method(predict, gafs) -S3method(predict, safs) +S3method(predict, safs) S3method(summary, bagEarth) S3method(summary, bagFDA) @@ -428,7 +428,7 @@ S3method(predictors, default) S3method(predictors, rfe) S3method(predictors, sbf) S3method(predictors, gafs) -S3method(predictors, safs) +S3method(predictors, safs) S3method(confusionMatrix, table) @@ -455,7 +455,7 @@ S3method(summary, diff.resamples) S3method(update, train) S3method(update, rfe) S3method(update, gafs) -S3method(update, safs) +S3method(update, safs) S3method(fitted, train) S3method(residuals, train) @@ -471,7 +471,7 @@ S3method(oob_pred, sbf) S3method(oob_pred, list) S3method(gafs, default) -S3method(safs, default) +S3method(safs, default) S3method(trim, train) diff --git a/pkg/caret/R/trim.R b/pkg/caret/R/trim.R index 0d815832f..f0bb5bfa2 100644 --- a/pkg/caret/R/trim.R +++ b/pkg/caret/R/trim.R @@ -6,14 +6,14 @@ trim.train <- function(object, ...) { "perfNames", "maxmimize", "times") for(i in removals) if(i %in% names(object)) object[i] <- NULL - c_removals <- c('method', 'number', 'repeats', 'p', 'initialWindow', - 'horizon', 'fixedWindow', 'verboseIter', 'returnData', - 'returnResamp', 'savePredictions', 'summaryFunction', - 'selectionFunction', 'index', 'indexOut', 'timingSamps', + c_removals <- c('method', 'number', 'repeats', 'p', 'initialWindow', + 'horizon', 'fixedWindow', 'verboseIter', 'returnData', + 'returnResamp', 'savePredictions', 'summaryFunction', + 'selectionFunction', 'index', 'indexOut', 'timingSamps', 'trim', 'yLimits') for(i in c_removals) - if(i %in% names(object$control)) object$control[i] <- NULL + if(i %in% names(object$control)) object$control[i] <- NULL if(!is.null(object$modelInfo$trim)) object$finalModel <- object$modelInfo$trim(object$finalModel) object -} \ No newline at end of file +} diff --git a/pkg/caret/tests/testthat/test_confusionMatrix.R b/pkg/caret/tests/testthat/test_confusionMatrix.R index 59d51c737..7ace37081 100644 --- a/pkg/caret/tests/testthat/test_confusionMatrix.R +++ b/pkg/caret/tests/testthat/test_confusionMatrix.R @@ -4,15 +4,15 @@ set.seed(442) test_that("Confusion matrix works", { library(caret) - train <- twoClassSim(n = 1000, intercept = -8, linearVars = 3, + train <- twoClassSim(n = 1000, intercept = -8, linearVars = 3, noiseVars = 10, corrVars = 4, corrValue = 0.6) - + ctrl <- trainControl(method = "cv", classProbs = TRUE) - - fullModel <- train(Class ~ ., data = train, - method = "knn", - preProc = c("center", "scale"), - tuneLength = 4, + + fullModel <- train(Class ~ ., data = train, + method = "knn", + preProc = c("center", "scale"), + tuneLength = 4, trControl = ctrl) dat <- train$Class ref <- predict(fullModel) @@ -26,10 +26,10 @@ test_that("Confusion matrix works", { dat5 <- as.character(dat3) dat5[200] <- "Class4" dat5 <- factor(dat5, levels = c("Class1", "Class4")) - cm1 <- confusionMatrix(dat, ref) - cm2 <- confusionMatrix(dat2, ref2) - cm3 <- confusionMatrix(dat3, ref2) - cm4 <- confusionMatrix(dat4, ref2) + suppressWarnings(cm1 <- confusionMatrix(dat, ref)) + suppressWarnings(cm2 <- confusionMatrix(dat2, ref2)) + suppressWarnings(cm3 <- confusionMatrix(dat3, ref2)) + suppressWarnings(cm4 <- confusionMatrix(dat4, ref2)) expect_true(class(cm1) == "confusionMatrix") expect_true(class(cm2) == "confusionMatrix") expect_true(class(cm3) == "confusionMatrix") @@ -42,4 +42,4 @@ test_that("Confusion matrix works", { expect_identical(cm4$overall, cm3$overall) expect_true(identical(cm1, cm2)) expect_true(identical(cm3, cm4)) -}) \ No newline at end of file +}) diff --git a/pkg/caret/tests/testthat/test_create_folds.R b/pkg/caret/tests/testthat/test_create_folds.R new file mode 100644 index 000000000..5fa6caeea --- /dev/null +++ b/pkg/caret/tests/testthat/test_create_folds.R @@ -0,0 +1,49 @@ +library(caret) +context('Test Data Splitting Functions') + +test_that('createTimeSlices', { + set.seed(1) + y <- 1:10 + + s1 <- createTimeSlices(y, initialWindow=5, horizon=1, fixedWindow=TRUE) + expect_equal(length(s1$train), 5) + expect_equal(length(s1$test), 5) + expect_equal(s1$train$Training1, 1:5) + expect_equal(s1$train$Training2, 2:6) + expect_equal(s1$train$Training3, 3:7) + expect_equal(s1$train$Training4, 4:8) + expect_equal(s1$train$Training5, 5:9) + expect_equal(s1$test$Testing1, 6) + expect_equal(s1$test$Testing2, 7) + expect_equal(s1$test$Testing3, 8) + expect_equal(s1$test$Testing4, 9) + expect_equal(s1$test$Testing5, 10) + + s2 <- createTimeSlices(y, initialWindow=5, horizon=1, fixedWindow=FALSE) + expect_equal(length(s2$train), 5) + expect_equal(length(s2$test), 5) + expect_equal(s2$train$Training1, 1:5) + expect_equal(s2$train$Training2, 1:6) + expect_equal(s2$train$Training3, 1:7) + expect_equal(s2$train$Training4, 1:8) + expect_equal(s2$train$Training5, 1:9) + expect_equal(s2$test$Testing1, 6) + expect_equal(s2$test$Testing2, 7) + expect_equal(s2$test$Testing3, 8) + expect_equal(s2$test$Testing4, 9) + expect_equal(s2$test$Testing5, 10) + + s3 <- createTimeSlices(y, initialWindow=5, horizon=5, fixedWindow=TRUE) + expect_equal(length(s3$train), 1) + expect_equal(length(s3$test), 1) + expect_equal(s3$train$Training1, 1:5) + expect_equal(s3$test$Testing1, 6:10) + + s4 <- createTimeSlices(y, initialWindow=5, horizon=5, fixedWindow=FALSE) + expect_equal(length(s3$train), 1) + expect_equal(length(s3$test), 1) + expect_equal(s3$train$Training1, 1:5) + expect_equal(s3$test$Testing1, 6:10) + + expect_identical(s3, s4) +}) diff --git a/pkg/caret/tests/testthat/trim.R b/pkg/caret/tests/testthat/test_trim.R similarity index 64% rename from pkg/caret/tests/testthat/trim.R rename to pkg/caret/tests/testthat/test_trim.R index eaccb2829..638d2c3b4 100644 --- a/pkg/caret/tests/testthat/trim.R +++ b/pkg/caret/tests/testthat/test_trim.R @@ -1,69 +1,56 @@ -context('Test model object trimming') +context('Base model trimming works') library(rpart) library(ipred) +library(plyr) ################################################################### -## rpart tests functions +## rpart tests -check_rpart_reg <- function() { +test_that("trimmed rpart regression produces identical predicted values", { skip_on_cran() set.seed(1) train_dat <- SLC14_1(100) train_dat$factor_var <- factor(sample(letters[1:2], nrow(train_dat), replace = TRUE)) test_dat <- SLC14_1(1000) test_dat$factor_var <- factor(sample(letters[1:2], nrow(test_dat), replace = TRUE)) - + library(rpart) rpart_full <- rpart(y ~ ., data = train_dat) - rpart_trim <- caret:::trim(rpart_full) - predict(rpart_full, test_dat) - predict(rpart_trim, test_dat) -} + rpart_trim <- getModelInfo('rpart', regex=FALSE)[[1]]$trim(rpart_full) + expect_identical(predict(rpart_full, test_dat), predict(rpart_trim, test_dat)) + expect_less_than(object.size(rpart_trim), object.size(rpart_full)) +}) -check_rpart_class <- function() { +test_that("trimmed rpart classification produces identical predicted values", { skip_on_cran() set.seed(1) train_dat <- twoClassSim(100) train_dat$factor_var <- factor(sample(letters[1:2], nrow(train_dat), replace = TRUE)) test_dat <- twoClassSim(1000) test_dat$factor_var <- factor(sample(letters[1:2], nrow(test_dat), replace = TRUE)) - + library(rpart) rpart_full <- rpart(Class ~ ., data = train_dat) - rpart_trim <- caret:::trim(rpart_full) - predict(rpart_full, test_dat)[, "Class1"] - predict(rpart_trim, test_dat)[, "Class1"] -} + rpart_trim <- getModelInfo('rpart', regex=FALSE)[[1]]$trim(rpart_full) + expect_identical(predict(rpart_full, test_dat)[, "Class1"], predict(rpart_trim, test_dat)[, "Class1"]) + expect_less_than(object.size(rpart_trim), object.size(rpart_full)) +}) ################################################################### -## bagging tests functions +## bagging tests -check_bag_reg <- function() { +test_that("trimmed bagging regression produces identical predicted values", { skip_on_cran() set.seed(1) train_dat <- SLC14_1(100) train_dat$factor_var <- factor(sample(letters[1:2], nrow(train_dat), replace = TRUE)) test_dat <- SLC14_1(1000) test_dat$factor_var <- factor(sample(letters[1:2], nrow(test_dat), replace = TRUE)) - + library(rpart) bag_full <- bagging(y ~ ., data = train_dat) - bag_trim <- caret:::trim(bag_full) - predict(bag_full, test_dat) - predict(bag_trim, test_dat) -} - -################################################################### -## Tests - -test_that("trimmed rpart regression produces identical predicted values", { - expect_that(sum(check_rpart_reg()), equals(0)) -}) - -test_that("trimmed rpart classification produces identical predicted values", { - expect_that(sum(check_rpart_class()), equals(0)) + bag_trim <- getModelInfo('treebag', regex=FALSE)[[1]]$trim(bag_full) + expect_identical(predict(bag_full, test_dat), predict(bag_trim, test_dat)) + expect_less_than(object.size(bag_trim), object.size(bag_full)) }) - -test_that("trimmed bagging regression produces identical predicted values", { - expect_that(sum(check_bag_reg()), equals(0)) -}) - - diff --git a/pkg/caret/tests/testthat/test_trim_C5.R b/pkg/caret/tests/testthat/test_trim_C5.R new file mode 100644 index 000000000..9f6180de7 --- /dev/null +++ b/pkg/caret/tests/testthat/test_trim_C5.R @@ -0,0 +1,152 @@ +# +# test_that('single tree', { +# skip_on_cran() +# library(caret) +# library(C50) +# +# set.seed(1) +# tr_dat <- twoClassSim(200) +# te_dat <- twoClassSim(200) +# +# set.seed(2) +# class_trim <- train(Class ~ ., data = tr_dat, +# method = "C5.0", +# tuneGrid = data.frame(trials = 1, +# model = "tree", +# winnow = FALSE), +# trControl = trainControl(method = "none", +# classProbs = TRUE, +# trim = TRUE)) +# +# set.seed(2) +# class_notrim <- train(Class ~ ., data = tr_dat, +# method = "C5.0", +# tuneGrid = data.frame(trials = 1, +# model = "tree", +# winnow = FALSE), +# 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('single rule', { +# skip_on_cran() +# library(caret) +# library(C50) +# +# set.seed(1) +# tr_dat <- twoClassSim(200) +# te_dat <- twoClassSim(200) +# +# set.seed(2) +# class_trim <- train(Class ~ ., data = tr_dat, +# method = "C5.0", +# tuneGrid = data.frame(trials = 1, +# model = "rules", +# winnow = FALSE), +# trControl = trainControl(method = "none", +# classProbs = TRUE, +# trim = TRUE)) +# +# set.seed(2) +# class_notrim <- train(Class ~ ., data = tr_dat, +# method = "C5.0", +# tuneGrid = data.frame(trials = 1, +# model = "rules", +# winnow = FALSE), +# 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('boosted tree', { +# skip_on_cran() +# library(caret) +# library(C50) +# +# set.seed(1) +# tr_dat <- twoClassSim(200) +# te_dat <- twoClassSim(200) +# +# set.seed(2) +# class_trim <- train(Class ~ ., data = tr_dat, +# method = "C5.0", +# tuneGrid = data.frame(trials = 5, +# model = "tree", +# winnow = FALSE), +# trControl = trainControl(method = "none", +# classProbs = TRUE, +# trim = TRUE)) +# +# set.seed(2) +# class_notrim <- train(Class ~ ., data = tr_dat, +# method = "C5.0", +# tuneGrid = data.frame(trials = 5, +# model = "tree", +# winnow = FALSE), +# 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('boosted rule', { +# skip_on_cran() +# library(caret) +# library(C50) +# +# set.seed(1) +# tr_dat <- twoClassSim(200) +# te_dat <- twoClassSim(200) +# +# set.seed(2) +# class_trim <- train(Class ~ ., data = tr_dat, +# method = "C5.0", +# tuneGrid = data.frame(trials = 5, +# model = "rules", +# winnow = FALSE), +# trControl = trainControl(method = "none", +# classProbs = TRUE, +# trim = TRUE)) +# +# set.seed(2) +# class_notrim <- train(Class ~ ., data = tr_dat, +# method = "C5.0", +# tuneGrid = data.frame(trials = 5, +# model = "rules", +# winnow = FALSE), +# 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) +# }) diff --git a/pkg/caret/tests/testthat/trim_bayesglm.R b/pkg/caret/tests/testthat/test_trim_bayesglm.R similarity index 96% rename from pkg/caret/tests/testthat/trim_bayesglm.R rename to pkg/caret/tests/testthat/test_trim_bayesglm.R index 70c219b7d..55c626d1e 100644 --- a/pkg/caret/tests/testthat/trim_bayesglm.R +++ b/pkg/caret/tests/testthat/test_trim_bayesglm.R @@ -1,7 +1,9 @@ -library(caret) test_that('bayesglm classification', { skip_on_cran() + library(caret) + library(arm) + set.seed(1) tr_dat <- twoClassSim(200) te_dat <- twoClassSim(200) @@ -33,6 +35,9 @@ test_that('bayesglm classification', { test_that('bayesglm regression', { skip_on_cran() + library(caret) + library(arm) + set.seed(1) tr_dat <- SLC14_1(200) te_dat <- SLC14_1(200) diff --git a/pkg/caret/tests/testthat/trim_glm.R b/pkg/caret/tests/testthat/test_trim_glm.R similarity index 98% rename from pkg/caret/tests/testthat/trim_glm.R rename to pkg/caret/tests/testthat/test_trim_glm.R index ae12d4fc7..1343b1f8e 100644 --- a/pkg/caret/tests/testthat/trim_glm.R +++ b/pkg/caret/tests/testthat/test_trim_glm.R @@ -1,7 +1,8 @@ -library(caret) test_that('glm classification', { skip_on_cran() + library(caret) + set.seed(1) tr_dat <- twoClassSim(200) te_dat <- twoClassSim(200) @@ -33,6 +34,8 @@ test_that('glm classification', { test_that('glm regression', { skip_on_cran() + library(caret) + set.seed(1) tr_dat <- SLC14_1(200) te_dat <- SLC14_1(200) diff --git a/pkg/caret/tests/testthat/test_trim_glmnet.R b/pkg/caret/tests/testthat/test_trim_glmnet.R new file mode 100644 index 000000000..48e21299c --- /dev/null +++ b/pkg/caret/tests/testthat/test_trim_glmnet.R @@ -0,0 +1,62 @@ +# +# test_that('glmnet classification', { +# skip_on_cran() +# library(caret) +# library(glmnet) +# +# set.seed(1) +# tr_dat <- twoClassSim(200) +# te_dat <- twoClassSim(200) +# +# set.seed(2) +# class_trim <- train(Class ~ ., data = tr_dat, +# method = "glmnet", +# tuneGrid = data.frame(lambda = .1, alpha = .5), +# trControl = trainControl(method = "none", +# classProbs = TRUE, +# trim = TRUE)) +# +# set.seed(2) +# class_notrim <- train(Class ~ ., data = tr_dat, +# method = "glmnet", +# tuneGrid = data.frame(lambda = .1, alpha = .5), +# 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('glmnet regression', { +# skip_on_cran() +# library(caret) +# library(glmnet) +# +# set.seed(1) +# tr_dat <- SLC14_1(200) +# te_dat <- SLC14_1(200) +# +# set.seed(2) +# reg_trim <- train(y ~ ., data = tr_dat, +# method = "glmnet", +# tuneGrid = data.frame(lambda = .1, alpha = .5), +# trControl = trainControl(method = "none", +# trim = TRUE)) +# +# set.seed(2) +# reg_notrim <- train(y ~ ., data = tr_dat, +# method = "glmnet", +# tuneGrid = data.frame(lambda = .1, alpha = .5), +# 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) +# }) +# diff --git a/pkg/caret/tests/testthat/test_trim_rpart.R b/pkg/caret/tests/testthat/test_trim_rpart.R new file mode 100644 index 000000000..fdfeb6f8d --- /dev/null +++ b/pkg/caret/tests/testthat/test_trim_rpart.R @@ -0,0 +1,121 @@ +# +# test_that('rpart classification', { +# skip_on_cran() +# library(caret) +# library(rpart) +# 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() +# library(caret) +# library(rpart) +# 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() +# library(caret) +# library(rpart) +# +# 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() +# library(caret) +# library(rpart) +# +# 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) +# }) diff --git a/pkg/caret/tests/testthat/trim_train.R b/pkg/caret/tests/testthat/test_trim_train.R similarity index 65% rename from pkg/caret/tests/testthat/trim_train.R rename to pkg/caret/tests/testthat/test_trim_train.R index 6eaaa80f2..e2fb285fb 100644 --- a/pkg/caret/tests/testthat/trim_train.R +++ b/pkg/caret/tests/testthat/test_trim_train.R @@ -1,62 +1,68 @@ -library(caret) +context('Caret model trimming works') test_that('train classification', { skip_on_cran() + library(caret) + library(rpart) + library(earth) 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), preProc = c("center", "bagImpute"), - trControl = trainControl(method = "none", + trControl = trainControl(method = "none", classProbs = TRUE, trim = TRUE)) class_trim <- caret:::trim.train(class_trim) - + set.seed(2) class_notrim <- train(Class ~ ., data = tr_dat, method = "rpart", tuneGrid = data.frame(cp = 0.22), preProc = c("center", "bagImpute"), - trControl = trainControl(method = "none", + 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) + + expect_less_than(object.size(class_trim), object.size(class_notrim)) }) test_that('train regression', { skip_on_cran() + library(caret) + library(rpart) + library(earth) 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", + trControl = trainControl(method = "none", trim = TRUE)) reg_trim <- caret:::trim.train(reg_trim) - + set.seed(2) reg_notrim <- train(y ~ ., data = tr_dat, method = "rpart", tuneGrid = data.frame(cp = 0.12), - trControl = trainControl(method = "none", + 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) + expect_less_than(object.size(reg_trim), object.size(reg_notrim)) }) @@ -65,56 +71,54 @@ test_that('train/earth classification', { set.seed(1) tr_dat <- twoClassSim(200) te_dat <- twoClassSim(200) - + set.seed(2) class_trim <- train(Class ~ ., data = tr_dat, method = "earth", tuneGrid = data.frame(nprune = 3, degree = 1), - trControl = trainControl(method = "none", + trControl = trainControl(method = "none", classProbs = TRUE, trim = TRUE)) class_trim <- caret:::trim.train(class_trim) - + set.seed(2) class_notrim <- train(Class ~ ., data = tr_dat, method = "earth", tuneGrid = data.frame(nprune = 3, degree = 1), - trControl = trainControl(method = "none", + 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('train/earth 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 = "earth", - tuneGrid = data.frame(nprune = 3, degree = 1), - trControl = trainControl(method = "none", - trim = TRUE)) - - set.seed(2) - reg_notrim <- train(y ~ ., data = tr_dat, - method = "earth", - tuneGrid = data.frame(nprune = 3, degree = 1), - 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) + expect_less_than(object.size(class_trim), object.size(class_notrim)) }) - - +#Currently no trim method for earth models +# test_that('train/earth 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 = "earth", +# tuneGrid = data.frame(nprune = 3, degree = 1), +# trControl = trainControl(method = "none", +# trim = TRUE)) +# +# set.seed(2) +# reg_notrim <- train(y ~ ., data = tr_dat, +# method = "earth", +# tuneGrid = data.frame(nprune = 3, degree = 1), +# 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)) +# }) diff --git a/pkg/caret/tests/testthat/test_trim_treebag.R b/pkg/caret/tests/testthat/test_trim_treebag.R new file mode 100644 index 000000000..78586b8ee --- /dev/null +++ b/pkg/caret/tests/testthat/test_trim_treebag.R @@ -0,0 +1,62 @@ +# +# test_that('treebag classification', { +# skip_on_cran() +# library(caret) +# library(ipred) +# library(plyr) +# set.seed(1) +# tr_dat <- twoClassSim(200) +# te_dat <- twoClassSim(200) +# +# set.seed(2) +# class_trim <- train(Class ~ ., data = tr_dat, +# method = "treebag", +# nbagg = 3, +# trControl = trainControl(method = "none", +# classProbs = TRUE, +# trim = TRUE)) +# +# set.seed(2) +# class_notrim <- train(Class ~ ., data = tr_dat, +# method = "treebag", +# nbagg = 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('rpart regression', { +# skip_on_cran() +# library(caret) +# library(ipred) +# library(plyr) +# set.seed(1) +# tr_dat <- SLC14_1(200) +# te_dat <- SLC14_1(200) +# +# set.seed(2) +# reg_trim <- train(y ~ ., data = tr_dat, +# method = "treebag", +# nbagg = 3, +# trControl = trainControl(method = "none", +# trim = TRUE)) +# +# set.seed(2) +# reg_notrim <- train(y ~ ., data = tr_dat, +# method = "treebag", +# nbagg = 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) +# }) +# diff --git a/pkg/caret/tests/testthat/trim_C5.R b/pkg/caret/tests/testthat/trim_C5.R deleted file mode 100644 index c245ab905..000000000 --- a/pkg/caret/tests/testthat/trim_C5.R +++ /dev/null @@ -1,144 +0,0 @@ -library(caret) - -test_that('single tree', { - 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 = "C5.0", - tuneGrid = data.frame(trials = 1, - model = "tree", - winnow = FALSE), - trControl = trainControl(method = "none", - classProbs = TRUE, - trim = TRUE)) - - set.seed(2) - class_notrim <- train(Class ~ ., data = tr_dat, - method = "C5.0", - tuneGrid = data.frame(trials = 1, - model = "tree", - winnow = FALSE), - 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('single rule', { - 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 = "C5.0", - tuneGrid = data.frame(trials = 1, - model = "rules", - winnow = FALSE), - trControl = trainControl(method = "none", - classProbs = TRUE, - trim = TRUE)) - - set.seed(2) - class_notrim <- train(Class ~ ., data = tr_dat, - method = "C5.0", - tuneGrid = data.frame(trials = 1, - model = "rules", - winnow = FALSE), - 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('boosted tree', { - 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 = "C5.0", - tuneGrid = data.frame(trials = 5, - model = "tree", - winnow = FALSE), - trControl = trainControl(method = "none", - classProbs = TRUE, - trim = TRUE)) - - set.seed(2) - class_notrim <- train(Class ~ ., data = tr_dat, - method = "C5.0", - tuneGrid = data.frame(trials = 5, - model = "tree", - winnow = FALSE), - 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('boosted rule', { - 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 = "C5.0", - tuneGrid = data.frame(trials = 5, - model = "rules", - winnow = FALSE), - trControl = trainControl(method = "none", - classProbs = TRUE, - trim = TRUE)) - - set.seed(2) - class_notrim <- train(Class ~ ., data = tr_dat, - method = "C5.0", - tuneGrid = data.frame(trials = 5, - model = "rules", - winnow = FALSE), - 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) -}) - - - diff --git a/pkg/caret/tests/testthat/trim_glmnet.R b/pkg/caret/tests/testthat/trim_glmnet.R deleted file mode 100644 index edba16e97..000000000 --- a/pkg/caret/tests/testthat/trim_glmnet.R +++ /dev/null @@ -1,57 +0,0 @@ -library(caret) - -test_that('glmnet 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 = "glmnet", - tuneGrid = data.frame(lambda = .1, alpha = .5), - trControl = trainControl(method = "none", - classProbs = TRUE, - trim = TRUE)) - - set.seed(2) - class_notrim <- train(Class ~ ., data = tr_dat, - method = "glmnet", - tuneGrid = data.frame(lambda = .1, alpha = .5), - 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('glmnet 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 = "glmnet", - tuneGrid = data.frame(lambda = .1, alpha = .5), - trControl = trainControl(method = "none", - trim = TRUE)) - - set.seed(2) - reg_notrim <- train(y ~ ., data = tr_dat, - method = "glmnet", - tuneGrid = data.frame(lambda = .1, alpha = .5), - 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) -}) - diff --git a/pkg/caret/tests/testthat/trim_rpart.R b/pkg/caret/tests/testthat/trim_rpart.R deleted file mode 100644 index 507f018ba..000000000 --- a/pkg/caret/tests/testthat/trim_rpart.R +++ /dev/null @@ -1,115 +0,0 @@ -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) -}) - - - diff --git a/pkg/caret/tests/testthat/trim_treebag.R b/pkg/caret/tests/testthat/trim_treebag.R deleted file mode 100644 index d520fcd2a..000000000 --- a/pkg/caret/tests/testthat/trim_treebag.R +++ /dev/null @@ -1,57 +0,0 @@ -library(caret) - -test_that('treebag 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 = "treebag", - nbagg = 3, - trControl = trainControl(method = "none", - classProbs = TRUE, - trim = TRUE)) - - set.seed(2) - class_notrim <- train(Class ~ ., data = tr_dat, - method = "treebag", - nbagg = 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('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 = "treebag", - nbagg = 3, - trControl = trainControl(method = "none", - trim = TRUE)) - - set.seed(2) - reg_notrim <- train(y ~ ., data = tr_dat, - method = "treebag", - nbagg = 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) -}) -