Tutorial: Develop a predictive model in R with SQL machine learning
Applies to: SQL Server 2016 (13.x) and later Azure SQL Managed Instance
In this four-part tutorial series, you use R and a machine learning model in SQL Server Machine Learning Services or on Big Data Clusters to predict the number of ski rentals.
In this four-part tutorial series, you use R and a machine learning model in SQL Server Machine Learning Services to predict the number of ski rentals.
In this four-part tutorial series, you use R and a machine learning model in SQL Server R Services to predict the number of ski rentals.
In this four-part tutorial series, you use R and a machine learning model in Azure SQL Managed Instance Machine Learning Services to predict the number of ski rentals.
Imagine you own a ski rental business and you want to predict the number of rentals that you'll have on a future date. This information will help you get your stock, staff, and facilities ready.
In the first part of this series, you'll get set up with the prerequisites. In parts two and three, you'll develop some R scripts in a notebook to prepare your data and train a machine learning model. Then, in part three, you'll run those R scripts inside a database using T-SQL stored procedures.
In this article, you'll learn how to:
- Restore a sample database
In part two, you'll learn how to load the data from a database into a Python data frame, and prepare the data in R.
In part three, you'll learn how to train a machine learning model in R.
In part four, you'll learn how to store the model in a database, and then create stored procedures from the R scripts you developed in parts two and three. The stored procedures run on the server to make predictions based on new data.
Prerequisites
- SQL Server Machine Learning Services - To install Machine Learning Services, see the Windows installation guide or the Linux installation guide. You can also enable Machine Learning Services on SQL Server Big Data Clusters.
- SQL Server Machine Learning Services - To install Machine Learning Services, see the Windows installation guide.
- SQL Server 2016 R Services - To install R Services, see the Windows installation guide.
Azure SQL Managed Instance Machine Learning Services. For information, see the Azure SQL Managed Instance Machine Learning Services overview.
SQL Server Management Studio (SSMS) - Use SSMS to restore the sample database to Azure SQL Managed Instance. To download, see SQL Server Management Studio.
R IDE - This tutorial uses RStudio Desktop.
RODBC - This driver is used in the R scripts you develop in this tutorial. If it's not already installed, install it using the R command
install.packages("RODBC")
. For more information on RODBC, see CRAN - Package RODBC.SQL query tool - This tutorial assumes you're using Azure Data Studio. For more information, see How to use notebooks in Azure Data Studio.
Restore the sample database
The sample database used in this tutorial has been saved to a .bak
database backup file for you to download and use.
Note
If you are using Machine Learning Services on Big Data Clusters, see how to Restore a database into the SQL Server big data cluster master instance.
Download the file TutorialDB.bak.
Follow the directions in Restore a database from a backup file in Azure Data Studio, using these details:
- Import from the
TutorialDB.bak
file you downloaded. - Name the target database
TutorialDB
.
- Import from the
You can verify that the restored database exists by querying the
dbo.rental_data
table:USE TutorialDB; SELECT * FROM [dbo].[rental_data];
Download the file TutorialDB.bak.
Follow the directions in Restore a database to Azure SQL Managed Instance in SQL Server Management Studio, using these details:
- Import from the
TutorialDB.bak
file you downloaded. - Name the target database
TutorialDB
.
- Import from the
You can verify that the restored database exists by querying the
dbo.rental_data
table:USE TutorialDB; SELECT * FROM [dbo].[rental_data];
Clean up resources
If you're not going to continue with this tutorial, delete the TutorialDB database.
Next step
In part one of this tutorial series, you completed these steps:
- Installed the prerequisites
- Restored a sample database
To prepare the data for the machine learning model, follow part two of this tutorial series: