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
|
instantiateResampleInstance = function(desc, size, task) {
UseMethod("instantiateResampleInstance")
}
instantiateResampleInstance.HoldoutDesc = function(desc, size, task = NULL) {
inds = sample(size, size * desc$split)
makeResampleInstanceInternal(desc, size, train.inds = list(inds))
}
instantiateResampleInstance.CVDesc = function(desc, size, task = NULL) {
if (desc$iters > size)
stopf("Cannot use more folds (%i) than size (%i)!", desc$iters, size)
test.inds = chunk(seq_len(size), shuffle = TRUE, n.chunks = desc$iters)
makeResampleInstanceInternal(desc, size, test.inds = test.inds)
}
instantiateResampleInstance.SpCVDesc = function(desc, size, task = NULL) {
if (is.null(task)) {
stopf("Please provide a task.")
}
if (is.null(task$coordinates)) {
stopf("Please provide suitable coordinates for SpCV. See ?Task for help.")
}
# perform kmeans clustering
inds = kmeans(task$coordinates, centers = desc$iters)
inds = factor(inds$cluster)
# uses resulting factor levels from kmeans clustering to set up a list of
# length x (x = folds) with row indices of the data referring to which fold
# each observations is assigned to
test.inds = lapply(levels(inds), function(x, spl)
which(spl == x), spl = inds)
makeResampleInstanceInternal(desc, size, test.inds = test.inds)
}
instantiateResampleInstance.LOODesc = function(desc, size, task = NULL) {
desc$iters = size
makeResampleInstanceInternal(desc, size, test.inds = as.list(seq_len(size)))
}
instantiateResampleInstance.SubsampleDesc = function(desc, size, task = NULL) {
inds = lapply(seq_len(desc$iters), function(x) sample(size, size * desc$split))
makeResampleInstanceInternal(desc, size, train.inds = inds)
}
instantiateResampleInstance.BootstrapDesc = function(desc, size, task = NULL) {
inds = lapply(seq_len(desc$iters), function(x) sample(size, size, replace = TRUE))
makeResampleInstanceInternal(desc, size, train.inds = inds)
}
instantiateResampleInstance.RepCVDesc = function(desc, size, task = NULL) {
folds = desc$iters / desc$reps
d = makeResampleDesc("CV", iters = folds)
i = replicate(desc$reps, makeResampleInstance(d, size = size), simplify = FALSE)
train.inds = Reduce(c, lapply(i, function(j) j$train.inds))
test.inds = Reduce(c, lapply(i, function(j) j$test.inds))
g = as.factor(rep(seq_len(desc$reps), each = folds))
makeResampleInstanceInternal(desc, size, train.inds = train.inds, test.inds = test.inds, group = g)
}
instantiateResampleInstance.SpRepCVDesc = function(desc, size, task = NULL) {
folds = desc$iters / desc$reps
d = makeResampleDesc("SpCV", iters = folds)
i = replicate(desc$reps, makeResampleInstance(d, task = task), simplify = FALSE)
train.inds = Reduce(c, lapply(i, function(j) j$train.inds))
test.inds = Reduce(c, lapply(i, function(j) j$test.inds))
g = as.factor(rep(seq_len(desc$reps), each = folds))
makeResampleInstanceInternal(desc, size, train.inds = train.inds, test.inds = test.inds, group = g)
}
instantiateResampleInstance.FixedWindowCVDesc = function(desc, size, task = NULL, coords) {
makeResamplingWindow(desc, size, task, coords, "FixedWindowCV")
}
instantiateResampleInstance.GrowingWindowCVDesc = function(desc, size, task = NULL, coords) {
makeResamplingWindow(desc, size, task, coords, "GrowingWindowCV")
}
|