admin管理员组

文章数量:1291116

Running caret:createFolds multiple times with a set.seed prior to running the first caret:createFolds call creates different folds. But while running caret:createFolds with set.seed running just before the call every single time gives me the same folds every single time. Why?

When using the following lines of code I get different folds (although when repeating the code I do get the same different folds), but when running the second snippet of code the folds are consistent... I don't understand it.

set.seed(18)
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))

gives:

212213
222311
211333
212132
312211
213231

This code:

lapply(1:5, function(x) {
+   set.seed(18)
+   head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
+ })

consistently gives 2 1 2 2 1 3

doing set.seed before every createFolds (without the lapply and function, but basically the same) gives the same consistent 2 1 2 2 1 3.

Why doesn't caret respect the seed when set?

Running caret:createFolds multiple times with a set.seed prior to running the first caret:createFolds call creates different folds. But while running caret:createFolds with set.seed running just before the call every single time gives me the same folds every single time. Why?

When using the following lines of code I get different folds (although when repeating the code I do get the same different folds), but when running the second snippet of code the folds are consistent... I don't understand it.

set.seed(18)
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))

gives:

212213
222311
211333
212132
312211
213231

This code:

lapply(1:5, function(x) {
+   set.seed(18)
+   head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
+ })

consistently gives 2 1 2 2 1 3

doing set.seed before every createFolds (without the lapply and function, but basically the same) gives the same consistent 2 1 2 2 1 3.

Why doesn't caret respect the seed when set?

Share Improve this question edited Mar 1 at 8:42 Jan 9,3486 gold badges20 silver badges33 bronze badges asked Feb 13 at 15:04 user29444121user29444121 111 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Caret is respecting the seed. When you set a seed, the output of the code that follows is then reproducible, which is why you consistently get 212213 222311 211333 212132 312211 213231 in that order. Setting seed does not mean that every function call that involves random number generation will give the same output, just that any sequence of function calls will give reproducible output.

本文标签: rDifferent folds using setseed and caretcreateFoldsStack Overflow