I am using BNLEARN package in R to predict a variable. I noticed each time I run the code, the predicted values keep updating. Can you explain why this happens?
my code is below:
res=hc(training, start = NULL, whitelist = NULL, blacklist = NULL, score = "loglik-g", debug = FALSE, restart = 0, perturb = 1000, max.iter = Inf, maxp = Inf, optimized = TRUE)
graphviz.plot(res) fitted = bn.fit(res,
training,
keep.fitted = TRUE,
debug = FALSE)
pred_train = predict(fitted,
node = "beta",
data=training,
method = "bayes-lw",
n=5)
pred_test = predict(fitted,
node = "beta",
data=test,
method = "bayes-lw",
n=5)
The documentation provides a good source of information. Specifically, when the method is
"bayes-lw"To get reproducible results between
predictcalls you can useset.seed().An example based on
?bnlearn::predict.bn.fit:And to make reproducible use
set.seedbefore eachpredictcall