When working with caret, how can I save a model after training, and load it later (e.g. in a different session) for prediction?
Asked
Active
Viewed 4.6k times
3 Answers
62
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)

lbcommer
- 985
- 1
- 11
- 20
23
The correct syntax would be to use:
save(model, file="model.Rdata")
Thereafter, it can be loaded using the load() command.

Dan Ibanez
- 247
- 2
- 3
-
1Hmm, yeah that is interesting that the other answer went two and a half years unchanged and incorrect. Haha – Rich Scriven Sep 24 '15 at 23:55
11
The following code assumes that your model's variable name is 'model':
save(model, "model.RData")
This will save your model as "model.RData" in the current working directory. You can find out what the working directory is by issuing the following:
getwd()
To load it back in, ensure that your model is saved in your working directory and issue:
load("model.RData")

Nick Evans
- 535
- 3
- 12
-
3it my case I needed to explicitly set file by `save(model, file = "model.RData")` – Boern Oct 05 '15 at 07:03