How to load a R model?

This recipe helps you load a R model

Recipe Objective

Once we have trained a model and tested it's performance to be satisfactory, we should save the model. The trained model is lost as soon as we close the session. Additionally, with large dataset, training a model is quite time-consuming since you have to run the algorithm again and again. Hence, it is ideal to train and save the model which can be loaded later to predict the outcome on the new dataset. ​

In this recipe, we will demonstrate how to load an existing Regression Tree model and predict the test values ​

Learn About the Application of ARCH and GARCH models in Real-World

STEP 1: Loading the model

There are two ways to save and load the model: ​

  1. using save(), load(): When we use save(), we will have to load it using the same name.
  2. using saveRDS(), loadRDS(): saveRDS() does not save the model name and we have the flexibilty to load the model in any other name. Bur saveRDS() can only save one object at a time as it is lower-level function.

Most people prefer saveRDS() over save() as it is serialise the object. ​

Syntax: readRDS(file =) ​

where: file = path with the file extension .rda ​

#loading the model model_regression_Tree = readRDS("C:/Users/Divit/Desktop/Internship/R-recipes_Jan/R_160 onwards/Decision Tree_classifier/model.rda") #checking whether the model has been loaded with different name ls()

STEP 2: Load the test dataset

# For data manipulation library(tidyverse) # Install readxl R package for reading excel sheets install.packages("readxl") library("readxl") #reading the dataset test = read_excel('R_254_df_test_regression.xlsx') # scaling the dataset as the model was built on scaled data test_scaled = scale(test[2:6]) # using cbind() function to add a new column Outcome to the scaled independent values test_scaled = data.frame(cbind(test_scaled, Outcome = test$Cost)) glimpse(test_scaled)

Rows: 32
Columns: 6
$ Weight   0.72483012, 0.07204194, 0.17201851, 0.23082825, 0.35432872,...
$ Weight1  0.72445274, 0.08459639, 0.17756697, 0.23225555, 0.35803927,...
$ Length   0.69959684, 0.09077507, 0.24556027, 0.29715533, 0.34875040,...
$ Height   2.15715925, 0.03471101, 0.07758442, 0.14769072, 0.25564092,...
$ Width    1.87080937, -0.06904068, 0.29059599, 0.39466263, 0.22707121...
$ Outcome  1000.0, 200.0, 300.0, 300.0, 300.0, 430.0, 345.0, 456.0, 51...

STEP 3: Predict cost from test dataset

We use Predict() function to do the same.

Syntax: predict(fitted_model, df, type = '')

where:

  1. fitted_model = model fitted by train dataset
  2. df = test dataset

predict_test = predict(model_regression_Tree, test_scaled) predict_test %>% head()

1 700.909090909091
2 316.625
3 316.625
4 316.625
5 495.9
6 495.9

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Build an AI Quiz Generator from Video with OpenAI API
In this LLM project, you will build a model to automate the transcription of video content and generate interactive quizzes using OpenAI’s Whisper and GPT-4o.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.