How to Save Machine Learning Models in R

Saving machine learning models in R is essential for preserving trained models for future use, such as making predictions on new data or deploying the models in production environments. This article covers various methods to save and load machine learning models in R Programming Language ensuring you can efficiently manage and utilize your models.

Save Machine Learning Models in R

Machine learning models can be computationally intensive to train, especially with large datasets and complex algorithms. Once trained, it’s crucial to save these models to avoid retraining them every time they are needed. R provides several ways to save and load machine learning models, making it easier to handle model persistence and reuse.

Methods to Save Machine Learning Models in R:

  1. Using save and load Functions
  2. Using saveRDS and readRDS Functions
  3. Using the caret Package

1. Save Machine Learning Models Using save and load Functions

The save and load functions in R allow you to save multiple objects, including machine learning models, into a single file. This method is useful when you want to save several related objects together.

R
# Train a sample model
model <- lm(mpg ~ wt + hp, data = mtcars)

# Save the model to a file
save(model, file = "linear_model.RData")

we saved the model now we Loading a machine learning model in R involves reading the saved model file into your R environment. Depending on how the model was saved, you can use different methods to load it.

R
# Load the model from the file
load("linear_model.RData")

# The model is now available in the environment
summary(model)

Output:

Call:
lm(formula = mpg ~ wt + hp, data = mtcars)

Residuals:
Min 1Q Median 3Q Max
-3.941 -1.600 -0.182 1.050 5.854

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
wt -3.87783 0.63273 -6.129 1.12e-06 ***
hp -0.03177 0.00903 -3.519 0.00145 **
---
Signif. codes: 0 β€˜***’ 0.001 β€˜**’ 0.01 β€˜*’ 0.05 β€˜.’ 0.1 β€˜ ’ 1

Residual standard error: 2.593 on 29 degrees of freedom
Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12

2. Save Machine Learning Models Using saveRDS and readRDS Functions

The saveRDS and readRDS functions are designed to save and load a single R object. This method is more convenient for saving individual models and offers more flexibility compared to save and load.

R
# Train a sample model
model <- lm(mpg ~ wt + hp, data = mtcars)

# Save the model to a file
saveRDS(model, file = "linear_model.rds")

we saved the model now we Loading a machine learning model in R involves reading the saved model file into your R environment. Depending on how the model was saved, you can use different methods to load it.

R
# Load the model from the file
model <- readRDS("linear_model.rds")

# The model is now available in the environment
summary(model)

Output:

Call:
lm(formula = mpg ~ wt + hp, data = mtcars)

Residuals:
Min 1Q Median 3Q Max
-3.941 -1.600 -0.182 1.050 5.854

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
wt -3.87783 0.63273 -6.129 1.12e-06 ***
hp -0.03177 0.00903 -3.519 0.00145 **
---
Signif. codes: 0 β€˜***’ 0.001 β€˜**’ 0.01 β€˜*’ 0.05 β€˜.’ 0.1 β€˜ ’ 1

Residual standard error: 2.593 on 29 degrees of freedom
Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12

3. Save Machine Learning Models Using the caret Package

The caret package is a popular framework for machine learning in R, offering a unified interface for training and evaluating models. It also provides functions to save and load models.

R
# Install and load caret package
install.packages("caret")
library(caret)

# Train a sample model using caret
model <- train(mpg ~ wt + hp, data = mtcars, method = "lm")

# Save the model to a file
saveRDS(model, file = "caret_model.rds")

we saved the model now we Loading a machine learning model in R involves reading the saved model file into your R environment. Depending on how the model was saved, you can use different methods to load it.

R
# Load the model from the file
model <- readRDS("caret_model.rds")

# The model is now available in the environment
print(model)

Output:

Linear Regression 

32 samples
2 predictor

No pre-processing
Resampling: Bootstrapped (25 reps)
Summary of sample sizes: 32, 32, 32, 32, 32, 32, ...
Resampling results:

RMSE Rsquared MAE
2.807584 0.838469 2.206361

Tuning parameter 'intercept' was held constant at a value of TRUE

Practical Example: Saving and Loading a Random Forest Model

Let’s consider a more comprehensive example using a Random Forest model. We will use the randomForest package to train a model, save it, and load it.

R
# Install and load randomForest package
install.packages("randomForest")
library(randomForest)

# Train a Random Forest model
set.seed(42)
model_rf <- randomForest(mpg ~ ., data = mtcars)

# Display the model summary
print(model_rf)

Output:

Call:
randomForest(formula = mpg ~ ., data = mtcars)
Type of random forest: regression
Number of trees: 500
No. of variables tried at each split: 3

Mean of squared residuals: 5.6973
% Var explained: 83.81

we saved the model now we Loading a machine learning model in R involves reading the saved model file into your R environment. Depending on how the model was saved, you can use different methods to load it.

R
# Save the Random Forest model to a file
saveRDS(model_rf, file = "random_forest_model.rds")

# Load the Random Forest model from the file
model_rf_loaded <- readRDS("random_forest_model.rds")

# The model is now available in the environment
print(model_rf_loaded)

Output:

Call:
randomForest(formula = mpg ~ ., data = mtcars)
Type of random forest: regression
Number of trees: 500
No. of variables tried at each split: 3

Mean of squared residuals: 5.6973
% Var explained: 83.81

Conclusion

Saving and loading machine learning models in R is a straightforward process that can significantly streamline your workflow. By using functions such as save, load, saveRDS, and readRDS, or leveraging the caret package, you can easily preserve and reuse your trained models. This ensures that your models are always ready for prediction tasks without the need for retraining, saving both time and computational resources.