|
| 1 | +library(testthat) |
| 2 | +library(parsnip) |
| 3 | +library(rlang) |
| 4 | +library(dplyr) |
| 5 | + |
| 6 | +context("varying parameters") |
| 7 | + |
| 8 | +load("recipes_examples.RData") |
| 9 | + |
| 10 | +test_that('main parsnip arguments', { |
| 11 | + mod_1 <- |
| 12 | + rand_forest() %>% |
| 13 | + varying_args(id = "") |
| 14 | + exp_1 <- |
| 15 | + tibble( |
| 16 | + name = c("mtry", "trees", "min_n"), |
| 17 | + varying = rep(FALSE, 3), |
| 18 | + id = rep("", 3), |
| 19 | + type = rep("model_spec", 3) |
| 20 | + ) |
| 21 | + expect_equal(mod_1, exp_1) |
| 22 | + |
| 23 | + mod_2 <- |
| 24 | + rand_forest(mtry = varying()) %>% |
| 25 | + varying_args(id = "") |
| 26 | + exp_2 <- exp_1 |
| 27 | + exp_2$varying[1] <- TRUE |
| 28 | + expect_equal(mod_2, exp_2) |
| 29 | + |
| 30 | + mod_3 <- |
| 31 | + rand_forest(mtry = varying(), trees = varying()) %>% |
| 32 | + varying_args(id = "wat") |
| 33 | + exp_3 <- exp_2 |
| 34 | + exp_3$varying[1:2] <- TRUE |
| 35 | + exp_3$id <- "wat" |
| 36 | + expect_equal(mod_3, exp_3) |
| 37 | +}) |
| 38 | + |
| 39 | + |
| 40 | +test_that('other parsnip arguments', { |
| 41 | + other_1 <- |
| 42 | + rand_forest(others = list(sample.fraction = varying())) %>% |
| 43 | + varying_args(id = "only others") |
| 44 | + exp_1 <- |
| 45 | + tibble( |
| 46 | + name = c("mtry", "trees", "min_n", "sample.fraction"), |
| 47 | + varying = c(rep(FALSE, 3), TRUE), |
| 48 | + id = rep("only others", 4), |
| 49 | + type = rep("model_spec", 4) |
| 50 | + ) |
| 51 | + expect_equal(other_1, exp_1) |
| 52 | + |
| 53 | + other_2 <- |
| 54 | + rand_forest(min_n = varying(), others = list(sample.fraction = varying())) %>% |
| 55 | + varying_args(id = "only others") |
| 56 | + exp_2 <- |
| 57 | + tibble( |
| 58 | + name = c("mtry", "trees", "min_n", "sample.fraction"), |
| 59 | + varying = c(rep(FALSE, 2), rep(TRUE, 2)), |
| 60 | + id = rep("only others", 4), |
| 61 | + type = rep("model_spec", 4) |
| 62 | + ) |
| 63 | + expect_equal(other_2, exp_2) |
| 64 | + |
| 65 | + other_3 <- |
| 66 | + rand_forest( |
| 67 | + others = list( |
| 68 | + strata = expr(Class), |
| 69 | + sampsize = c(varying(), varying()) |
| 70 | + ) |
| 71 | + ) %>% |
| 72 | + varying_args(id = "add an expr") |
| 73 | + exp_3 <- |
| 74 | + tibble( |
| 75 | + name = c("mtry", "trees", "min_n", "strata", "sampsize"), |
| 76 | + varying = c(rep(FALSE, 4), TRUE), |
| 77 | + id = rep("add an expr", 5), |
| 78 | + type = rep("model_spec", 5) |
| 79 | + ) |
| 80 | + expect_equal(other_3, exp_3) |
| 81 | + |
| 82 | + other_4 <- |
| 83 | + rand_forest( |
| 84 | + others = list( |
| 85 | + strata = expr(Class), |
| 86 | + sampsize = c(12, varying()) |
| 87 | + ) |
| 88 | + ) %>% |
| 89 | + varying_args(id = "num and varying in vec") |
| 90 | + exp_4 <- |
| 91 | + tibble( |
| 92 | + name = c("mtry", "trees", "min_n", "strata", "sampsize"), |
| 93 | + varying = c(rep(FALSE, 4), TRUE), |
| 94 | + id = rep("num and varying in vec", 5), |
| 95 | + type = rep("model_spec", 5) |
| 96 | + ) |
| 97 | + expect_equal(other_4, exp_4) |
| 98 | +}) |
| 99 | + |
| 100 | + |
| 101 | +test_that('recipe parameters', { |
| 102 | + rec_res_1 <- varying_args(rec_1) |
| 103 | + exp_1 <- |
| 104 | + tibble( |
| 105 | + name = c("K", "num", "threshold", "options"), |
| 106 | + varying = c(TRUE, TRUE, FALSE, FALSE), |
| 107 | + id = c("step_knnimpute", rep("step_pca", 3)), |
| 108 | + type = rep("step", 4) |
| 109 | + ) |
| 110 | + expect_equal(rec_res_1, exp_1) |
| 111 | + |
| 112 | + rec_res_2 <- varying_args(rec_2) |
| 113 | + exp_2 <- exp_1 |
| 114 | + expect_equal(rec_res_2, exp_2) |
| 115 | + |
| 116 | + rec_res_3 <- varying_args(rec_3) |
| 117 | + exp_3 <- exp_1 |
| 118 | + exp_3$varying <- FALSE |
| 119 | + expect_equal(rec_res_3, exp_3) |
| 120 | + |
| 121 | + rec_res_4 <- varying_args(rec_4) |
| 122 | + exp_4 <- tibble() |
| 123 | + expect_equal(rec_res_4, exp_4) |
| 124 | +}) |
| 125 | + |
0 commit comments