1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/features/r-services/getting-started/rental-prediction

Build a predictive model with SQL Server R Services

This sample shows how to create a predictive model in R and operationalize it with SQL Server 2016 or vNext.

Contents

About this sample
Before you begin
Sample details
Related links

About this sample

Predictive modeling is a powerful way to add intelligence to your application. It enables applications to predict outcomes against new data. The act of incorporating predictive analytics into your applications involves two major phases: model training and model operationalization.

In this sample, you will learn how to create a predictive model in R and operationalize it with SQL Server 2016.

Follow the step by step tutorial here to walk through this sample.

  • Applies to: SQL Server 2016 (or higher)
  • Key features: SQL Server R Services
  • Workload: SQL Server R Services
  • Programming Language: T-SQL, R, JavaScript (NodeJS)
  • Authors: Nellie Gustafsson
  • Update history: Getting started tutorial for R Services

Before you begin

To run this sample, you need the following prerequisites. Section 1 in the tutorial covers the prerequisites. After that, you can download a DB backup file and restore it using Setup.sql. Download DB

Software prerequisites:

  1. SQL Server 2016 (or higher) with R Services installed
  2. SQL Server Management Studio
  3. R IDE Tool like Visual Studio RTVS

Run this sample app

  1. From SQL Server Management Studio or SQL Server Data Tools connect to your SQL Server 2016 or vNext SQL database and execute setup.sql to restore the sample DB

  2. From SQL Server Management Studio or SQL Server Data Tools, execute Predictive Model.sql script to set up tables, train model, predict using that model etc. This is all covered step by step in the tutorial

  3. Navigate to the folder where you have downloaded sample and run npm install in command window, or run setup.bat if you are on Windows operating system. This command will install necessary npm packages defined in project.json.

  4. Locate db.js file in the project, change database connection info in createConnection() method to reference your database. the following tokens should be replaced:

  5. SERVERNAME - name of the database server.

  6. DATABASE - Name of database where Todo table is stored.

  7. USERNAME - SQL Server login that can access table data and execute stored procedures.

  8. PASSWORD - Password associated to SQL Server login.

    var config = {
        server  : "SERVER.database.windows.net",
        userName: "USER",
        password: "PASSWORD",
        // If you have a named instance, you can put the instance name here:
        options: { encrypt: true, database: 'DATABASE' }
    };
  1. Run sample app by opening a command window, navigate to the location where here you have downloaded sample and run node bin\www
  2. Go to a browser and navigate to the following [link] (http://localhost:3000/client.html). You should now see an HTML table containing the predictions generated using R in SQL Server.

Sample details

This sample application shows how to create a predictive model and generate predictions using the model. It also shows how to build a simple REST API service tthat gets data from the DB. NodeJS REST API is used to implement REST Service in the example.

Predictive Model.R

The R script that generates a predictive model and uses it to predict rental counts

Predictive Model.SQL

Takes the R code in PredictiveModel.R and deploys it inside SQL Server. Creating stored procedures and tables for training, storing models and creating stored procedures for prediction.

app.js

File that contains startup code.

db.js

File that contains functions that wrap Tedious library

predictions.js

File that contains action that will be called to get the predictions

Service uses Tedious library for data access and built-in JSON functionalities that are available in SQL Server 2016 and Azure SQL Database.

Disclaimers

The code included in this sample is not intended demonstrate some general guidance and architectural patterns for web development. It contains minimal code required to create a REST API. You can easily modify this code to fit the architecture of your application.

For additional content, see these articles:

SQL Server R Services - Upgrade and Installation FAQ
Other SQL Server R Services Tutorials
Watch a presentation about predictive modeling in SQL Server, that also goes through this sample