Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving and loading a model in R

Tags:

r

r-caret

When working with caret, how can I save a model after training, and load it later (e.g. in a different session) for prediction?

like image 407
Amelio Vazquez-Reina Avatar asked Feb 07 '13 21:02

Amelio Vazquez-Reina


People also ask

How do I load a dataset in R?

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.

How do I save an object for later in R?

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.

How do you save a data file in R?

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.


1 Answers

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)

like image 79
lbcommer Avatar answered Sep 24 '22 20:09

lbcommer