@@ -2,109 +2,141 @@ library(testthat)
22context(" descriptor variables" )
33library(parsnip )
44
5- template <- function (col , pred , ob , lev , fact )
6- list (cols = col , preds = pred , obs = ob , levs = lev , facts = fact )
5+ template <- function (col , pred , ob , lev , fact , dat , x , y ) {
6+ list (.n_cols = col , .n_preds = pred , .n_obs = ob ,
7+ .n_levs = lev , .n_facts = fact , .dat = dat , .x = x , .y = y )
8+ }
9+
10+ eval_descrs <- function (descrs ) {
11+ lapply(descrs , do.call , list ())
12+ }
713
814species_tab <- table(iris $ Species , dnn = NULL )
915
1016# ------------------------------------------------------------------------------
1117
1218context(" Should descriptors be created?" )
1319
14- test_that(" make_descr" , {
15- expect_false(parsnip ::: make_descr(rand_forest()))
16- expect_false(parsnip ::: make_descr(rand_forest(mtry = 3 )))
17- expect_false(parsnip ::: make_descr(rand_forest(mtry = varying())))
18- expect_true(parsnip ::: make_descr(rand_forest(mtry = expr(..num ))))
19- expect_false(parsnip ::: make_descr(rand_forest(mtry = expr(3 ))))
20- expect_false(parsnip ::: make_descr(rand_forest(mtry = quote(3 ))))
21- expect_true(parsnip ::: make_descr(rand_forest(mtry = quote(..num ))))
22-
23- expect_false(parsnip ::: make_descr(rand_forest(others = list (arrrg = 3 ))))
24- expect_false(parsnip ::: make_descr(rand_forest(others = list (arrrg = varying()))))
25- expect_true(parsnip ::: make_descr(rand_forest(others = list (arrrg = expr(..num )))))
26- expect_false(parsnip ::: make_descr(rand_forest(others = list (arrrg = expr(3 )))))
27- expect_false(parsnip ::: make_descr(rand_forest(others = list (arrrg = quote(3 )))))
28- expect_true(parsnip ::: make_descr(rand_forest(others = list (arrrg = quote(..num )))))
20+ test_that(" requires_descrs" , {
21+
22+ # embedded in a function
23+ fn <- function () {
24+ .n_cols()
25+ }
26+
27+ # doubly embedded
28+ fn2 <- function () {
29+ fn()
30+ }
31+
32+ # core args
33+ expect_false(requires_descrs(rand_forest()))
34+ expect_false(requires_descrs(rand_forest(mtry = 3 )))
35+ expect_false(requires_descrs(rand_forest(mtry = varying())))
36+ expect_true(requires_descrs(rand_forest(mtry = .n_cols())))
37+ expect_false(requires_descrs(rand_forest(mtry = expr(3 ))))
38+ expect_false(requires_descrs(rand_forest(mtry = quote(3 ))))
39+ expect_true(requires_descrs(rand_forest(mtry = fn())))
40+ expect_true(requires_descrs(rand_forest(mtry = fn2())))
41+
42+ # descriptors in `others`
43+ expect_false(requires_descrs(rand_forest(arrrg = 3 )))
44+ expect_false(requires_descrs(rand_forest(arrrg = varying())))
45+ expect_true(requires_descrs(rand_forest(arrrg = .n_obs())))
46+ expect_false(requires_descrs(rand_forest(arrrg = expr(3 ))))
47+ expect_true(requires_descrs(rand_forest(arrrg = fn())))
48+ expect_true(requires_descrs(rand_forest(arrrg = fn2())))
49+
50+ # mixed
2951 expect_true(
30- parsnip ::: make_descr (
52+ requires_descrs (
3153 rand_forest(
3254 mtry = 3 ,
33- others = list ( arrrg = quote( ..num ) ))
55+ arrrg = fn2( ))
3456 )
3557 )
58+
3659 expect_true(
37- parsnip ::: make_descr (
60+ requires_descrs (
3861 rand_forest(
39- mtry = quote( ..num ),
40- others = list ( arrrg = 3 ) )
62+ mtry = .n_cols( ),
63+ arrrg = 3 )
4164 )
4265 )
4366})
4467
4568
46-
4769# ------------------------------------------------------------------------------
4870
4971context(" Testing formula -> xy conversion" )
5072
5173test_that(" numeric y and dummy vars" , {
5274 expect_equal(
53- template(4 , 5 , 150 , NA , 1 ),
54- parsnip ::: get_descr_form(Sepal.Width ~ . , data = iris )
75+ template(4 , 5 , 150 , NA , 1 , iris , iris [ - 2 ], iris [, " Sepal.Width " ] ),
76+ eval_descrs( get_descr_form(Sepal.Width ~ . , data = iris ) )
5577 )
5678 expect_equal(
57- template(1 , 2 , 150 , NA , 1 ),
58- parsnip ::: get_descr_form(Sepal.Width ~ Species , data = iris )
79+ template(1 , 2 , 150 , NA , 1 , iris , iris [ " Species " ], iris [, " Sepal.Width " ] ),
80+ eval_descrs( get_descr_form(Sepal.Width ~ Species , data = iris ) )
5981 )
6082})
6183
6284test_that(" numeric y and x" , {
6385 expect_equal(
64- template(1 , 1 , 150 , NA , 0 ),
65- parsnip ::: get_descr_form(Sepal.Width ~ Sepal.Length , data = iris )
86+ template(1 , 1 , 150 , NA , 0 , iris , iris [ " Sepal.Length " ], iris [, " Sepal.Width " ] ),
87+ eval_descrs( get_descr_form(Sepal.Width ~ Sepal.Length , data = iris ) )
6688 )
6789 expect_equal(
68- template(1 , 1 , 150 , NA , 0 ),
69- parsnip ::: get_descr_form(Sepal.Width ~ log(Sepal.Length ), data = iris )
90+ {
91+ log_sep <- iris [" Sepal.Length" ]
92+ log_sep [[" Sepal.Length" ]] <- log(log_sep [[" Sepal.Length" ]])
93+ names(log_sep ) <- " log(Sepal.Length)"
94+ template(1 , 1 , 150 , NA , 0 , iris , log_sep , iris [," Sepal.Width" ])
95+ },
96+ eval_descrs(get_descr_form(Sepal.Width ~ log(Sepal.Length ), data = iris ))
7097 )
7198})
7299
73100test_that(" factor y" , {
74101 expect_equal(
75- template(4 , 4 , 150 , species_tab , 0 ),
76- parsnip ::: get_descr_form(Species ~ . , data = iris )
102+ template(4 , 4 , 150 , species_tab , 0 , iris , iris [ - 5 ], iris [, " Species " ] ),
103+ eval_descrs( get_descr_form(Species ~ . , data = iris ) )
77104 )
78105 expect_equal(
79- template(1 , 1 , 150 , species_tab , 0 ),
80- parsnip ::: get_descr_form(Species ~ Sepal.Length , data = iris )
106+ template(1 , 1 , 150 , species_tab , 0 , iris , iris [ " Sepal.Length " ], iris [, " Species " ] ),
107+ eval_descrs( get_descr_form(Species ~ Sepal.Length , data = iris ) )
81108 )
82109})
83110
84111test_that(" factors all the way down" , {
112+ dat <- npk [,1 : 4 ]
85113 expect_equal(
86- template(3 , 7 , 24 , table(npk $ K , dnn = NULL ), 3 ),
87- parsnip ::: get_descr_form(K ~ . , data = npk [, 1 : 4 ] )
114+ template(3 , 7 , 24 , table(npk $ K , dnn = NULL ), 3 , dat , dat [ - 4 ], dat [, " K " ] ),
115+ eval_descrs( get_descr_form(K ~ . , data = dat ) )
88116 )
89117})
90118
91119test_that(" weird cases" , {
92120 # So model.frame ignores - signs in a model formula so Species is not removed
93121 # prior to model.matrix; otherwise this should have n_cols = 3
94122 expect_equal(
95- template(4 , 3 , 150 , NA , 1 ),
96- parsnip ::: get_descr_form(Sepal.Width ~ . - Species , data = iris )
123+ template(4 , 3 , 150 , NA , 1 , iris , iris [ - 2 ], iris [, " Sepal.Width " ] ),
124+ eval_descrs( get_descr_form(Sepal.Width ~ . - Species , data = iris ) )
97125 )
126+
98127 # Oy ve! Before going to model.matrix, model.frame produces a data frame
99128 # with one column and that column is a matrix (with the results from
100129 # `poly(Sepal.Length, 3)`
130+ x <- model.frame(~ poly(Sepal.Length , 3 ), iris )
131+ attributes(x ) <- attributes(as.data.frame(x ))[c(" names" , " class" , " row.names" )]
101132 expect_equal(
102- template(1 , 3 , 150 , NA , 0 ),
103- parsnip ::: get_descr_form(Sepal.Width ~ poly(Sepal.Length , 3 ), data = iris )
133+ template(1 , 3 , 150 , NA , 0 , iris , x , iris [, " Sepal.Width " ] ),
134+ eval_descrs( get_descr_form(Sepal.Width ~ poly(Sepal.Length , 3 ), data = iris ) )
104135 )
136+
105137 expect_equal(
106- template(0 , 0 , 150 , NA , 0 ),
107- parsnip ::: get_descr_form(Sepal.Width ~ 1 , data = iris )
138+ template(0 , 0 , 150 , NA , 0 , iris , iris [, numeric ()], iris [, " Sepal.Width " ] ),
139+ eval_descrs( get_descr_form(Sepal.Width ~ 1 , data = iris ) )
108140 )
109141})
110142
@@ -113,17 +145,24 @@ test_that("weird cases", {
113145context(" Testing xy -> formula conversion" )
114146
115147test_that(" numeric y and dummy vars" , {
148+ iris2 <- dplyr :: rename(iris , ..y = Species )
149+ rownames(iris2 ) <- rownames(iris2 ) # convert to char
116150 expect_equal(
117- template(4 , 4 , 150 , species_tab , 0 ),
118- parsnip ::: get_descr_xy(x = iris [, 1 : 4 ], y = iris $ Species )
151+ template(4 , 4 , 150 , species_tab , 0 , iris2 , iris [, 1 : 4 ], iris $ Species ),
152+ eval_descrs( get_descr_xy(x = iris [, 1 : 4 ], y = iris $ Species ) )
119153 )
154+
155+ iris2 <- iris [,c(4 ,5 ,1 ,2 )]
156+ rownames(iris2 ) <- rownames(iris2 )
120157 expect_equal(
121- template(2 , 2 , 150 , NA , 1 ),
122- parsnip ::: get_descr_xy(x = iris [, 4 : 5 ], y = iris [, 1 : 2 ])
158+ template(2 , 2 , 150 , NA , 1 , iris2 , iris [, 4 : 5 ], iris [, 1 : 2 ] ),
159+ eval_descrs( get_descr_xy(x = iris [, 4 : 5 ], y = iris [, 1 : 2 ]) )
123160 )
161+
162+ iris3 <- iris2 [,c(" Petal.Width" , " Species" , " Sepal.Length" )]
124163 expect_equal(
125- template(2 , 2 , 150 , NA , 1 ),
126- parsnip ::: get_descr_xy(x = iris [, 4 : 5 ], y = iris [, 1 , drop = FALSE ])
164+ template(2 , 2 , 150 , NA , 1 , iris3 , iris [, 4 : 5 ], iris [, 1 , drop = FALSE ] ),
165+ eval_descrs( get_descr_xy(x = iris [, 4 : 5 ], y = iris [, 1 , drop = FALSE ]) )
127166 )
128167})
129168
@@ -147,27 +186,27 @@ test_that("spark descriptor", {
147186
148187 expect_equal(
149188 template(4 , 5 , 150 , NA , 1 ),
150- parsnip ::: get_descr_form(Sepal_Width ~ . , data = iris_descr )
189+ get_descr_form(Sepal_Width ~ . , data = iris_descr )
151190 )
152191 expect_equal(
153192 template(1 , 2 , 150 , NA , 1 ),
154- parsnip ::: get_descr_form(Sepal_Width ~ Species , data = iris_descr )
193+ get_descr_form(Sepal_Width ~ Species , data = iris_descr )
155194 )
156195 expect_equal(
157196 template(1 , 1 , 150 , NA , 0 ),
158- parsnip ::: get_descr_form(Sepal_Width ~ Sepal_Length , data = iris_descr )
197+ get_descr_form(Sepal_Width ~ Sepal_Length , data = iris_descr )
159198 )
160199 expect_equivalent(
161200 template(4 , 4 , 150 , species_tab , 0 ),
162- parsnip ::: get_descr_form(Species ~ . , data = iris_descr )
201+ get_descr_form(Species ~ . , data = iris_descr )
163202 )
164203 expect_equal(
165204 template(1 , 1 , 150 , species_tab , 0 ),
166- parsnip ::: get_descr_form(Species ~ Sepal_Length , data = iris_descr )
205+ get_descr_form(Species ~ Sepal_Length , data = iris_descr )
167206 )
168207 expect_equivalent(
169208 template(3 , 7 , 24 , rev(table(npk $ K , dnn = NULL )), 3 ),
170- parsnip ::: get_descr_form(K ~ . , data = npk_descr )
209+ get_descr_form(K ~ . , data = npk_descr )
171210 )
172211
173212})
0 commit comments