When working with caret, how can I save a model after training, and load it later (e.g. in a different session) for prediction?
If you look at the package listing in the Packages panel, you will find a package called datasets. Simply check the checkbox next to the package name to load the package and gain access to the datasets. You can also click on the package name and RStudio will open a help file describing the datasets in this package.
To save data as an RData object, use the save function. To save data as a RDS object, use the saveRDS function. In each case, the first argument should be the name of the R object you wish to save. You should then include a file argument that has the file name or file path you want to save the data set to.
R dataset files One of the simplest ways to save your data is by saving it into an RData file with the function save( ). R saves your data to the working folder on your computer disk in a binary file.
A better solution nowadays is to use saveRDS to save and readRDS to read:
saveRDS(model, "model.rds") my_model <- readRDS("model.rds")
This lets you to choose a new name for the object (you don't need to remember the name you used when you saved it)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With