I'm using the set.seed() function in R to achieve reproducability of my results. I compare different regression methods (e.g. RandomForest, SVM, GAM) by their MSE derived from a cross-validation procedure. To my surprise, I realized that results differ whether I place 'set.seed(123)' at the beginning of my code (and then running the whole script) or whether I place 'set.seed(123)' just before calling each method in the script.
To illustrate pls follow my example below (although the answer by 'Sean Easter' and the example given by 'Cliff AB' below should explain as well):
data(iris)
iris
myf<- Sepal.Length ~
Sepal.Width+
Petal.Length+
Petal.Width+
Species
# required packages
library(sperrorest)
library(randomForest)
library(rpart)
##### Regression Tree
set.seed(123)
ctrl <- rpart.control(cp = 0.001)
fit_rpart <- rpart(myf, data = iris, control = ctrl)
#5-repeated 10-fold CV
mypred.rpart <- function(object, newdata) predict(object, newdata)
eval_ns_rpart <- sperrorest(data = iris, formula = myf, model.fun=
rpart, model.args = list(control = ctrl),
pred.fun = mypred.rpart, smp.fun =
partition.cv, smp.args =
list(repetition=1:5, nfold=10))
summary(eval_ns_rpart$error)
##### Random Forest
#set.seed(123) # REMOVE HASH IN 2ND RUN!!!!
fit_rf <- randomForest(myf, data = iris, ntree=1000)
#5-repeated 10-fold CV
mypred.rf <- function(object, newdata) predict(object, newdata)
eval_ns_rf <- sperrorest(data = iris, formula = myf,
model.fun = randomForest,
pred.fun = mypred.rf,
smp.fun = partition.cv, smp.args= list(repetition=1:5, nfold=10))
summary(eval_ns_rf$error)
#### SUMMARIES Mean Squared Errors(MSE)
tr_MSE_rpart<-(summary(eval_ns_rpart$error)[3,1]) # MSE training error
# 0.08548725
t_MSE_rpart<-(summary(eval_ns_rpart$error)[10,1]) # MSE test error
# 0.1445583
tr_MSE_RF<-(summary(eval_ns_rf$error)[3,1]) # MSE training error
# 0.07241344 # 2nd run: 0.07266605
t_MSE_RF<-(summary(eval_ns_rf$error)[10,1]) # MSE test error
# 0.1403778 # 2nd run: 0.1358957