Analytical web services

Steph Locke

2017-04-20

Objective

Take analysis to production

  • What code
  • Why do it
  • Who should do it
  • Where to put it
  • When to do it
  • How to implement

Steph Locke

github.com/StephLocke | itsalocke.com | T: SteffLocke

What code

Code that does stuff!

  • Post-modelling
  • Making predictions

Why do it

  • You (and future people) have R skills
  • Saves re-writing stuff
  • Already “fast enough”
  • Blackbox to other systems

Who should do it

  • Small teams
  • Low IT availability
  • People looking to move fast

Where to put it

  • shiny
  • Rserve
  • rApache
  • DeployR
  • AzureML

shiny

Shiny is the interactive dashboard system

shiny.rstudio.com

  • Good for providing a GUI
  • Bad for programmatic access

Rserve

Rserve is an API system for R that allows access to an R environment

rforge.net/Rserve/

  • Good for other languages to use R commands
  • Bad for doing a lot of grunt work in R

rApache

rApache is an apache module that serves the results of R scripts

rapache.net

  • Good for executing big scripts to return results in XML / HTML / JSON etc
  • Bad for people who don’t know apache

DeployR

DeployR is an API system for accessing R and R scripts

deployr.revolutionanalytics.com

  • Good for providing access to multiple applications
  • Bad for people who need to get things in “yesterday”

AzureML

AzureML is a GUI analysis system that can transform your model to a web service

studio.azureml.net

  • Good for people with low stats & R knowledge
  • Bad for cloud-phobes

How to implement

Ideal

  • Write a package
  • Add unit tests
  • Use source control
  • Use best of breed packages
  • Do DataOps
  • Deploy to right architecture for your business

How to implement

Quick & dirty

  • Write some code as a function
  • Use AzureML package to publish code

How to implement - AzureML demo

Setup

library("AzureML")
MLID<-"16ed88890b0644239ea800a90063bc0f"
AuthToken<-"e1e2d4f99b0041269c6435c0c6ccb07d"

How to implement - AzureML demo

Make a model

myLM<-lm(data = iris,
         Sepal.Length~Sepal.Width,
         model = FALSE)

adhocPred<-predict(myLM,
                   newdata = data.frame(
                     Sepal.Width=5.7)
                   )
adhocPred

How to implement - AzureML demo

Make a predict function

myPrediction<-function(SW=0){
  # You would store coeficients
  # In place of modelling every time!
  myCoefsLM<-coef(lm(data = iris,
           Sepal.Length~Sepal.Width,
           model = FALSE))
  myCoefScores<-crossprod(myCoefsLM,c(1,SW))
  return(sum(myCoefScores))
  
}

How to implement - AzureML demo

Publish a predict function

response <- publishWebService(
  "myPrediction", 
  "myPredictionOnline", 
  list("SW"="float"), 
  list("r"="float"), 
  MLID, AuthToken)

How to implement - AzureML demo

Use a published functions

webservices <- getWebServices(MLID, AuthToken)

endpoints <- getEndpoints(MLID, 
                          AuthToken, 
                          webservices[[1]]$Id)

response <- consumeLists(
  endpoints[[1]]$PrimaryKey, 
  endpoints[[1]]$ApiLocation, 
  list("SW"=9))

response

C’est fini!