Saves/loads random forests model to/from file
saveRF: the internal structure of given random forests model is saved to file.
loadRF: the internal structure of random forests model is loaded from given file and a model is created and returned.
1 2 3
The model structure as returned by
Name of the file to save/load the model to/from.
saveRF saves the internal structure of given random forests model to file.
The structures from C++ code are stored to the file with specified file, while internal structures
from R are stored to file named
model must be a valid structure returned by
loadRF loads the internal structure of random forests saved in a specified files and
returns access to it.
saveRF invisibly returns some debugging information, while
returns a loaded model as a list, similarly to
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
# use iris data set # build random forests model with certain parameters modelRF <- CoreModel(Species ~ ., iris, model="rf", selectionEstimator="MDL",minNodeWeightRF=5, rfNoTrees=100, maxThreads=1) print(modelRF) # prediction with node distribution pred <- predict(modelRF, iris, rfPredictClass=FALSE, type="both") # print(pred) # saves the random forests model to file saveRF(modelRF, "tempRF.txt") # restore the model to another model loadedRF = loadRF("tempRF.txt") # prediction should be the same predLoaded <- predict(loadedRF, iris, rfPredictClass=FALSE, type="both") # print(predLoaded) # sum of differences should be zero subject to numeric imprecision sum(pred$probabilities - predLoaded$probabilities) cat("Are predicted classes of original and retrieved models equal? ", all(pred$class == predLoaded$class), "\n" ) # cat("Are predicted probabilities of original and retrieved model equal? ", # all(pred$probabilities == predLoaded$probabilities), "\n" ) # clean up the models when no longer needed destroyModels(modelRF) destroyModels(loadedRF)