-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-CRAN.R
392 lines (380 loc) · 16.7 KB
/
test-CRAN.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
library(testthat)
library(data.table)
test_that("resampling error if no group", {
itask <- mlr3::TaskClassif$new("iris", iris, target="Species")
same_other <- mlr3resampling::ResamplingSameOtherCV$new()
expect_error({
same_other$instantiate(itask)
}, 'task has no subset, but at least one subset variable is required', fixed=TRUE)
})
test_that("resampling error if no strata", {
iris.dt <- data.table(iris)[, g := rep(1:3, l=.N)]
itask <- mlr3::TaskClassif$new("iris", iris.dt, target="Species")
itask$col_roles$subset <- "g"
same_other <- mlr3resampling::ResamplingSameOtherCV$new()
expect_error({
same_other$instantiate(itask)
}, 'task has no strata, but at least one stratum variable is required; at least assign the subset variable to a stratum', fixed=TRUE)
})
test_that("instantiation creates instance", {
iris.dt <- data.table(iris)[, g := rep(1:3, l=.N)]
itask <- mlr3::TaskClassif$new("iris", iris.dt, target="Species")
itask$col_roles$subset <- "g"
itask$col_roles$stratum <- "g"
same_other <- mlr3resampling::ResamplingSameOtherCV$new()
expect_identical(same_other$instance, NULL)
same_other$instantiate(itask)
expect_identical(same_other$instance$id.dt$g, iris.dt$g)
})
test_that("error for subset named subset", {
iris.dt <- data.table(iris)[, subset := rep(1:3, l=.N)]
itask <- mlr3::TaskClassif$new("iris", iris.dt, target="Species")
itask$col_roles$subset <- "subset"
itask$col_roles$stratum <- "subset"
same_other <- mlr3resampling::ResamplingSameOtherCV$new()
expect_identical(same_other$instance, NULL)
expect_error({
same_other$instantiate(itask)
}, "col with role subset must not be named subset; please fix by renaming subset col")
})
test_that("error for group named row_id", {
iris.dt <- data.table(iris)[, row_id := rep(1:3, l=.N)]
itask <- mlr3::TaskClassif$new("iris", iris.dt, target="Species")
itask$col_roles$subset <- "row_id"
itask$col_roles$stratum <- "row_id"
same_other <- mlr3resampling::ResamplingSameOtherCV$new()
expect_identical(same_other$instance, NULL)
expect_error({
same_other$instantiate(itask)
}, "col with role subset must not be named row_id; please fix by renaming row_id col")
})
test_that("error for group named fold", {
iris.dt <- data.table(iris)[, fold := rep(1:3, l=.N)]
itask <- mlr3::TaskClassif$new("iris", iris.dt, target="Species")
itask$col_roles$subset <- "fold"
itask$col_roles$stratum <- "fold"
same_other <- mlr3resampling::ResamplingSameOtherCV$new()
expect_identical(same_other$instance, NULL)
expect_error({
same_other$instantiate(itask)
}, "col with role subset must not be named fold; please fix by renaming fold col")
})
test_that("error for group named display_row", {
iris.dt <- data.table(iris)[, display_row := rep(1:3, l=.N)]
itask <- mlr3::TaskClassif$new("iris", iris.dt, target="Species")
itask$col_roles$subset <- "display_row"
itask$col_roles$stratum <- "display_row"
same_other <- mlr3resampling::ResamplingSameOtherCV$new()
expect_identical(same_other$instance, NULL)
expect_error({
same_other$instantiate(itask)
}, "col with role subset must not be named display_row; please fix by renaming display_row col")
})
test_that("error for group named test", {
iris.dt <- data.table(iris)[, test := rep(1:3, l=.N)]
itask <- mlr3::TaskClassif$new("iris", iris.dt, target="Species")
itask$col_roles$subset <- "test"
itask$col_roles$stratum <- "test"
same_other <- mlr3resampling::ResamplingSameOtherCV$new()
expect_identical(same_other$instance, NULL)
expect_error({
same_other$instantiate(itask)
}, "col with role subset must not be named test; please fix by renaming test col")
})
test_that("errors and result for 10 train data in small stratum", {
size_cv <- mlr3resampling::ResamplingVariableSizeTrainCV$new()
size_cv$param_set$values$folds <- 2
i10.dt <- data.table(iris)[1:70]
i10.task <- mlr3::TaskClassif$new(
"i10", i10.dt, target="Species"
)$set_col_roles("Species",c("target","stratum"))
expect_error({
size_cv$instantiate(i10.task)
},
"max_train_data=10 (in smallest stratum) but should be larger than min_train_data=10, please fix by decreasing min_train_data",
fixed=TRUE)
size_cv$param_set$values$min_train_data <- 9
expect_error({
size_cv$instantiate(i10.task)
},
"train sizes not unique, please decrease train_sizes",
fixed=TRUE)
size_cv$param_set$values$train_sizes <- 2
size_cv$instantiate(i10.task)
size.tab <- table(size_cv$instance$iteration.dt[["small_stratum_size"]])
expect_identical(names(size.tab), c("9","10"))
})
test_that("strata respected in all sizes", {
size_cv <- mlr3resampling::ResamplingVariableSizeTrainCV$new()
size_cv$param_set$values$min_train_data <- 5
size_cv$param_set$values$folds <- 5
N <- 100
imbalance <- 4
strat.vec <- ifelse((1:imbalance)<imbalance, "A","B")
istrat.dt <- data.table(iris[1:N,], strat=factor(rep(strat.vec, l=N)))
smallest.size.tab <- table(
istrat.dt[["strat"]]
)/N*imbalance*size_cv$param_set$values$min_train_data
istrat.task <- mlr3::TaskClassif$new(
"istrat", istrat.dt, target="Species"
)$set_col_roles("strat", "stratum")
size_cv$instantiate(istrat.task)
min.dt <- size_cv$instance$iteration.dt[train_size==min(train_size)]
for(min.i in 1:nrow(min.dt)){
min.row <- min.dt[min.i]
train.i <- min.row$train[[1]]
strat.tab <- table(istrat.dt[train.i, strat])
expect_equal(strat.tab, smallest.size.tab)
}
})
test_that("train set max size 67 for 100 data", {
size_cv <- mlr3resampling::ResamplingVariableSizeTrainCV$new()
i100.dt <- data.table(iris)[1:100]
i100.task <- mlr3::TaskClassif$new("i10", i100.dt, target="Species")
size_cv$instantiate(i100.task)
inst <- size_cv$instance
computed.counts <- inst$id.dt[, .(rows=.N), keyby=fold]
expected.counts <- data.table(
fold=1:3,
rows=as.integer(c(34,33,33)),
key="fold")
expect_equal(computed.counts, expected.counts)
l.train <- sapply(inst$iteration.dt$train, length)
expect_equal(l.train, inst$iteration.dt$train_size)
expect_equal(max(l.train), 67)
})
test_that("test fold 1 for iteration 1", {
set.seed(1)
size_cv <- mlr3resampling::ResamplingVariableSizeTrainCV$new()
i100.dt <- data.table(iris)[1:100]
i100.task <- mlr3::TaskClassif$new("i10", i100.dt, target="Species")
size_cv$instantiate(i100.task)
inst <- size_cv$instance
expect_equal(inst$iteration.dt$test.fold[1], 1)
})
## ResamplingSameOtherSizesCV
N <- 2100
abs.x <- 20
set.seed(1)
x.vec <- sort(runif(N, -abs.x, abs.x))
(task.dt <- data.table(
x=x.vec,
y = sin(x.vec)+rnorm(N,sd=0.5)))
atomic.group.size <- 2
task.dt[, agroup := rep(seq(1, N/atomic.group.size), each=atomic.group.size)][]
task.dt[, random_group := rep(
rep(c("A","B","B","C","C","C","C"), each=atomic.group.size),
l=.N
)][]
group.tab <- table(task.dt$random_group)
get_props <- function(x)x/sum(x)
prop.tab <- get_props(group.tab)
get_prop_mat <- function(ilist){
sapply(ilist, function(i)get_props(table(task.dt[i, random_group])))
}
test_that("ResamplingSameOtherSizesCV no subset, no group, no stratum", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=-1, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
expect_equal(computed[["test.fold"]], 1:n.folds)
full.train.size <- N*(n.folds-1)/n.folds
expect_equal(computed[["n.train.groups"]], rep(full.train.size, n.folds))
})
test_that("ResamplingSameOtherSizesCV no subset, yes group, no stratum", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=-1, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
expect_equal(computed[["test.fold"]], 1:n.folds)
full.train.size <- N*(n.folds-1)/n.folds
expect_equal(computed[["n.train.groups"]], rep(full.train.size/atomic.group.size, n.folds))
expect_equal(sapply(computed[["train"]], length), rep(full.train.size, n.folds))
expected.props <- matrix(
prop.tab, length(prop.tab), n.folds, dimnames=list(names(prop.tab),NULL))
computed.train <- get_prop_mat(computed[["train"]])
expect_false(identical(computed.train, expected.props))
computed.test <- get_prop_mat(computed[["test"]])
expect_false(identical(computed.test, expected.props))
})
test_that("ResamplingSameOtherSizesCV no subset, yes group, yes stratum", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
reg.task$col_roles$stratum <- "random_group"
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=-1, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
expect_equal(computed[["test.fold"]], 1:n.folds)
full.train.size <- N*(n.folds-1)/n.folds
expect_equal(computed[["n.train.groups"]], rep(full.train.size/atomic.group.size, n.folds))
expect_equal(sapply(computed[["train"]], length), rep(full.train.size, n.folds))
expected.props <- matrix(
prop.tab, length(prop.tab), n.folds, dimnames=list(names(prop.tab),NULL))
computed.train <- get_prop_mat(computed[["train"]])
expect_identical(computed.train, expected.props)
computed.test <- get_prop_mat(computed[["test"]])
expect_identical(computed.test, expected.props)
})
test_that("ResamplingSameOtherSizesCV yes subset, yes group, yes stratum, ignore_subset", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
reg.task$col_roles$stratum <- "random_group"
reg.task$col_roles$subset <- "random_group"
n.subsets <- length(unique(task.dt$random_group))
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=-1, ignore_subset=TRUE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
## same as no subset.
expect_equal(computed[["test.fold"]], 1:n.folds)
full.train.size <- N*(n.folds-1)/n.folds
expect_equal(computed[["n.train.groups"]], rep(full.train.size/atomic.group.size, n.folds))
expect_equal(sapply(computed[["train"]], length), rep(full.train.size, n.folds))
expected.props <- matrix(
prop.tab, length(prop.tab), n.folds, dimnames=list(names(prop.tab),NULL))
computed.train <- get_prop_mat(computed[["train"]])
expect_identical(computed.train, expected.props)
computed.test <- get_prop_mat(computed[["test"]])
expect_identical(computed.test, expected.props)
})
test_that("ResamplingSameOtherSizesCV no subset, yes group, yes stratum, sizes=0", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
reg.task$col_roles$stratum <- "random_group"
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=0, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
expect_equal(computed[["test.fold"]], 1:n.folds)
full.train.size <- N*(n.folds-1)/n.folds
expect_equal(computed[["n.train.groups"]], rep(full.train.size/atomic.group.size, n.folds))
expect_equal(sapply(computed[["train"]], length), rep(full.train.size, n.folds))
expected.props <- matrix(
prop.tab, length(prop.tab), n.folds, dimnames=list(names(prop.tab),NULL))
computed.train <- get_prop_mat(computed[["train"]])
expect_identical(computed.train, expected.props)
computed.test <- get_prop_mat(computed[["test"]])
expect_identical(computed.test, expected.props)
})
test_that("ResamplingSameOtherSizesCV no subset, yes group, yes stratum, sizes=1", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
reg.task$col_roles$stratum <- "random_group"
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=1, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
expect_equal(computed[["test.fold"]], rep(1:n.folds,each=2))
full.train.size <- N*(n.folds-1)/n.folds
expected.n <- (full.train.size/atomic.group.size)/c(2,1)
expect_equal(computed[["n.train.groups"]], rep(expected.n, n.folds))
expected.props <- matrix(
prop.tab, length(prop.tab), n.folds*2, dimnames=list(names(prop.tab),NULL))
computed.test <- get_prop_mat(computed[["test"]])
expect_identical(computed.test, expected.props)
})
test_that("ResamplingSameOtherSizesCV yes subset, yes group, yes stratum", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
reg.task$col_roles$stratum <- "random_group"
reg.task$col_roles$subset <- "random_group"
n.subsets <- length(unique(task.dt$random_group))
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=-1, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
expected.subsets <- list(all=c("A","B","C"),other=c("B","C"),same="A")
expect_equal(nrow(computed), n.folds*n.subsets*length(expected.subsets))
three <- computed[
test.fold==1 & seed==1 & test.subset=="A"
][order(train.subsets)]
expect_equal(three[["train.subsets"]], names(expected.subsets))
expect_identical(three[["test"]][[1]], three[["test"]][[2]])
expect_identical(three[["test"]][[1]], three[["test"]][[3]])
exp.prop.list <- unname(lapply(expected.subsets, function(N)get_props(group.tab[N,drop=FALSE])))
three.prop.list <- get_prop_mat(three[["train"]])
expect_identical(three.prop.list, exp.prop.list)
})
test_that("ResamplingSameOtherSizesCV yes subset, yes group, yes stratum, sizes=0", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
reg.task$col_roles$stratum <- "random_group"
reg.task$col_roles$subset <- "random_group"
n.subsets <- length(unique(task.dt$random_group))
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=0, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
n.train.per.test <- 6
expect_equal(nrow(computed), n.folds*n.subsets*n.train.per.test)
})
test_that("ResamplingSameOtherSizesCV yes subset, yes group, yes stratum, sizes=1", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
reg.task$col_roles$stratum <- "random_group"
reg.task$col_roles$subset <- "random_group"
n.subsets <- length(unique(task.dt$random_group))
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=1, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
n.train.per.test <- 9
expect_equal(nrow(computed), n.folds*n.subsets*n.train.per.test)
})
test_that("ResamplingSameOtherSizesCV yes subset, yes group, yes stratum, sizes=2", {
reg.task <- mlr3::TaskRegr$new(
"sin", task.dt, target="y")
reg.task$col_roles$feature <- "x"
reg.task$col_roles$group <- "agroup"
reg.task$col_roles$stratum <- "random_group"
reg.task$col_roles$subset <- "random_group"
n.subsets <- length(unique(task.dt$random_group))
same_other_sizes_cv <- mlr3resampling::ResamplingSameOtherSizesCV$new()
n.folds <- 3
same_other_sizes_cv$param_set$values <- list(
folds=n.folds, seeds=1, ratio=0.5, sizes=2, ignore_subset=FALSE)
same_other_sizes_cv$instantiate(reg.task)
computed <- same_other_sizes_cv$instance$iteration.dt
n.train.per.test <- 12
expect_equal(nrow(computed), n.folds*n.subsets*n.train.per.test)
})