+ All Categories
Home > Documents > Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd...

Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd...

Date post: 31-Mar-2015
Category:
Upload: madilyn-spillers
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
136
Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012
Transcript
Page 1: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Designing and Creating applications built on RRichard Pugh, Andy Nicholls & Chris Campbell23rd October 2012

Page 2: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Thank you for the invitation to speak tonight

Page 3: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Andy NichollsSenior R Consultant

Chris CampbellSenior R Consultant

Richard PughPrincipal R

Consultant & Co-Founder

Page 4: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Agenda

• Who are Mango Solutions?• Why Build Analytic Applications on R?• Formal R Application Development• Case Studies• The R Community• Discussion

Page 5: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Who are Mango Solutions?

Page 6: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Overview of Mango Solutions

• Private Company formed in 2002• Global Team of ~70• Cross-Sector Software and Services• ISO 9001 Accredited

Page 7: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Located here ...

Bath, UK London, UK Shanghai, CN Basel, CH

Page 8: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Spend a lot of time here ...

Page 9: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

The Beginning: October 2002

• Started by 2 ex-Insightful colleagues• Sales Guy (BO, Cognos

etc)• Techy Guy (S+, SAS, R

etc)

• Idea to deploy predictive analytics to business users

Page 10: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Why Mango?

• Early awful ideas• DataStatz• Stats Entertainment• VizUStat• Stats2U

• In the end, named after my colleagues cat

Page 11: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Growth of Mango Solutions

Number of Employees

Other

Support

Testers

PMs

Developers

Consultants

Total = 10

2006

Other

Support

Testers

PMs

Developers

Consultants

Total = 28

2010

Other

Support

Testers

PMs

Developers

Consultants

0 2 4 6 8 10 12 14 16 18 20 22

Total = 71

2012

Page 12: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

What we do?

R Training

Code Creation

Validation

Support

Consultants

Page 13: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

What we do?

Consultants Developers

Analytic Application Development

Page 14: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Mango Key Industries

• Mango work across sectors:• Pharmaceuticals

• Mango Imaging• Finance• Energy• Sensory

Page 15: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Why Build Analytic Applications on R?

Page 16: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Why Analytics?

• Analytics can help people answer all sorts of questions

• I believe there is no company in the world today who cannot benefit from analytics in some way

• More and more people are realising it

Page 17: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Who is a good driver? How do we win more games?What bonus should I pay?

Will someone like this? What are they likely to want?When might this break?

Page 18: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Why build Analytic Applications?

• 3 key reasons we see:• To deploy analytical tools to decision

makers• To make an analysts life more efficient• To add rigour to an analysts workflow

Page 19: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Deploying Analytics

• Adding analytics into a business process can mean more informed decisions can be made

• Complex analytics shouldn’t be attempted by non-analysts

• Means there is a communication between the decision maker and the analyst

Page 20: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Deploying Analytics

• If we build an application which …• is easy for the decision maker to use• contains the correct analysis to apply• communicates analytical results in suitable

manner• … this leads to some major benefits!

Page 21: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Benefits for the Analyst

Benefits for the Decision Maker

No need to wait for information

Can perform “what if” analysis

Decision not dependent on analyst availability

Less need to perform often-repetitive tasks

Comfortable that the “right” analysis is being

run

Can get on with more strategic things?

Page 22: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

AnalyticEngine

UserInterface

Data

AnalyticOutputs

DataStorage

AnalyticCode

Code Mgment

Analytic App Structure

Page 23: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Why build Analytic Applications on R?Building applications requires installing analytic engine on desktops, servers, clusters, cloudsR is license free

Building analytic applications involves integrating an analytic engine with other technologies (data sources, UI etc)R’s open nature means it can be readily integrated

Page 24: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Why build Analytic Applications on R?We want a programmable engine so that it can be readily extended (i.e. no black boxes please)R can be extended by the developer as needed

We often want to be able to deploy new algorithms and techniques as they become availableR is rapidly developed

Page 25: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Formal R Application Development

Page 26: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Formal R Development

• Creating sophisticated analytic applications requires a formal development approach

• This mostly means taking standard development practices and applying it to analytics

• Mango’s formal R development procedures and structure has been evolving since its inception ~2004

Page 27: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Project Mgment Requirements

Behaviour Driven

Code Review

Review board

StatET

runitroxygen2

Continuous Integration

Issue TrackingQuality Manual

Dev Procedures

R Coding Standards

mangoUtils

Knowledge Mgment

Page 28: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Project Mgment Requirements

Behaviour Driven

Code Review

Review board

StatET

runitroxygen2

Continuous Integration

Issue TrackingQuality Manual

Dev Procedures

Coding Standards

mangoUtils

Knowledge Mgment

Page 29: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Project Mgment Requirements

Behaviour Driven

Code Review

Review board

StatET

testthatroxygen2

Continuous Integration

Issue TrackingQuality Manual

Dev Procedures

R Coding Standards

mangoUtils

Knowledge Mgment

Page 30: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Studies

Page 31: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Studies

• These are examples of applications we’ve built that use R in some way

• We’re presented a range of information about each including:• Business Reason for the application• Technical Approach• Some Technical Detail where applicable• Things that worked well / things that didn’t

Page 32: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Studies

• Ranges from information we can fully disclose to only being able to say vague things about the customer

• Only so much info we can give today – please see us after or contact us and we can step through things in more detail

Richard Pugh = [email protected]

Andy Nicholls = [email protected]

Chris Campbell = [email protected]

Page 33: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Studies

• PKPD Web Modelling Platform• M&S Workflow Platform• Non-Compartmental Analysis Application• Coffee Blend Optimisation Tool• Pipeline Corrosion Forecasting Application• Backtesting Application

Page 34: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

CASE STUDYPKPD WEB PLATFORM

Page 35: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: PKPD ModellingOverview• Pharmacokinetics-

pharmacodynamics (PKPD) is the study of the manner in which a drug transitions through the body and its impact on a target disease

• PK is highly complex, involving sophisticated non-linear mixed effects modelling approaches

Page 36: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: PKPD ModellingOverview• Modellers use “NONMEM” software in order to

fit these models• Inputs and outputs to NONMEM are a mixture

of structured and unstructured textual files• R often used to analyse the outputs in order to

assess model fit (see “xpose4” library)

Page 37: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: PKPD ModellingOverview• PKPD is an evolving and exciting area, with

modellers needing flexibility and a variety of tools

• However, being within life sciences, rigour around workflows is key in order to satisfy regulatory requirements

Page 38: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: PKPD ModellingThe Challenge• Build a modern modelling platform that provides

rigour whilst allowing the modellers the flexibility they need

• Range of technical users from “everything is a shell script” to “which button do I click”

• Execution of third party tools (NONMEM, R, SAS, PsN, …) in a controlled manner

• Interface to generate reproducible graphics, tables and reports

Page 39: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: PKPD ModellingThe “R” bit• Where does R fit in?

• Many users use R and want to be able to develop scripts and execute them on an internal grid

• R used as the graphics engine to support the model evaluation and reporting processes

• Users want to be able to execute R interactively with objects in their project

Page 40: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

The App

App Server

Execution Server(s)

MIFMIF Queue

Cloud

Grid

+ Others

+ Others

RPool Mgr

Page 41: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: PKPD ModellingWhat is a “Report Item Definition”• Definition of a graph or table that can be

executed from Navigator• Consists of snippet of R code, options that may

be presented to the user, required columns, and a few other bits

• Can be used in a number of situations in the application

• Originally XML then stored in Db (XML shown to give a feel for structure on next slide)

Page 42: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Report Options

Source Data

Command Definition

Page 43: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

The App / RPool Manager

Data GraphTableText

DataItem

GraphItem

TableItem

TextItem

Data Frame

GraphicsTable

ObjectCharacter

xmlMethod

xmlMethod

xmlMethod

xmlMethod

Page 44: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Execution Engine (Java)

CommandDefinitions

CommandResultsV

ers

ion

Contr

ol

Page 45: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: PKPD ModellingHow are “RIDs” used?• Created, managed by Super Users (under version

control)• Called in a few places in the application:

• Directly (create this graph with this data)• In “Run Views” (reports)• In “Comparison Views” (reports that compare

models)• In “Template Reports” (tagged docx files)

Page 46: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: PKPD ModellingOutcome• The app in general was a big success• The “R” part was created as a separate service that we

have since reused in a number of other applications (e.g. Lloyds Risk Platform!)

• Shame that regulatory rules forced some design which we’re now building alternatives too

• Next: interactive graphical presentation

Page 47: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

CASE STUDYM&S WORKFLOW PLATFORM

Page 48: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: M&S Workflow PlatformOverview• Exciting project for major pharmaceutical company• Possibly the closest we’ve come to deploying an

analysts workflow in a scalable platform

• Hundreds of pre-clinical (animal) studies are run by a team of ~400 scientists

• Analysis performed by roughly 15 advanced modellers• Outcome: most studies not analysed!

Page 49: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: M&S Workflow PlatformThe Challenge• Idea to create a truly scalable platform to allow bench

scientists to run their own analysis• Modeller publishes an analysis “protocol” containing

analysis paths, code, and support documentation• Desktop application pulls from central set of protocols

and “derives” the interface which is presented to the user

• Modelling can put in checks to ensure things look right (e.g. data is of right format, model fit is particularly poor but user seems keep to create predictions from it)

Page 50: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: M&S Workflow PlatformThe Solution• Eclipse RCP application executing R and NONMEM

scripts on an internal LSF grid, with protocols and code held in SVN

• Generated workflow “protocol” definition (XML) detailing possible paths in a step, linked to R scripts and NONMEM model code with corresponding dialog

• Built “Protocol Developer” Eclipse interface onto repository

• RCP application derives analysis paths, UI, options and commentary to guide the end user

Page 51: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Protocol Metadata

Workflow

Analysis StepData Check Step Commentary

RScript

RScript

NM ModelFile

Options Options

Page 52: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Modeller Scientist

NONMEM

NONMEM

LSF GridProtocol Server

FileSystem

Page 53: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Possible Models

Derived Options Commentary

Page 54: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.
Page 55: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: M&S Workflow PlatformHow did it go• Technical solution was very strong and

applicable to other areas• RCP good technology, but steep learning curve• Testing was complex • Agile project – pros and cons• Ultimately, not deployed (site closure)

Page 56: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

CASE STUDYNON-COMPARTMENTAL ANALYSIS

Page 57: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

RapidNCA, the non-compartmental analysis workflow tool• Need for RapidNCA

• Using .NET

• RapidNCA Structure

• Code Quality

• Connections with R.NET

• Complete & Deploy RapidNCA

Page 58: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Need for RapidNCA

• Customer needed to send monthly reports to dozens of trial centres

• Small team, so time limited

• Predefined non-compartmentalanalysis

• Standardized report

Page 59: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Using .NET What is .NET?

• Object-oriented environment to develop applications

• Safe execution environment

• Choice of programming languages

• Framework consisting of:• runtime• class library

• Developed with Visual Studio

Page 60: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Using .NET Visual Studio

• A graphical programming tool (IDE)

• Visual Studio Express - free version

Page 61: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Using .NETChoice of languages

• C# is the main one

• F# is a functional language (similar concepts to OCaml)

• XAML (a Microsoft declarative XML language) for interactive graphics

• C++/CLI useful for legacy and bespoke parallel processing (including GPGPU)

• Other possibilities...

• Vb.Net is very like C# (no advantage over it)

• Third parties have added languages to the CLI platform

Page 62: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Using .NET“Ajar Source” Platform

• Not exactly open source, but…

• Most CLI third party languages are open

• C# and VB.Net are not, but many open source projects based on them

• Microsoft have made F# open source

• Compiler is free

• Other editors / IDEs are available

Page 63: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Using .NETPerformance

• Performance is very good

• On graphics (millions of data points will plot with ease and zoom smoothly)

• Computation is fast enough in C#, calling R adds little overhead

• Standard Maths library is limited; third parties and MS maths for “drawing” are better

• Data parallel computation is possible on the desktop (GPGPU)

• F# provides further “big data” capabilities

Page 64: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

UserInterface

Data

AnalyticOutputs

DataStorage

AnalyticCode

Code Mgment

AnalyticEngine

Data Service

RapidNCA Structure

Page 65: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

RapidNCA StructureMangoNca Analytic Code

AnalyseElement

DoAnalysis

GetAnalysis

UnitTests

DataChecks

Page 66: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

RapidNCA StructureMangoNca Analytic Code

Page 67: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Code QualityUnit Tests

• Ensure product works!

• User/Customer/Payer trust

• Ease of maintenance/extension

Page 68: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Code QualityRun Code, Check Output

• Working Cases

> test1 <- ncaAnalysis(Conc = c(4, 9, 8, 6, 4:1, 1), + Time = 0:8, Dose = 100, Dof = 2) > checkEquals(test1[1, "ROutput_adjr2"], 0.9714937901,+ tol = 1e-8)[1] TRUE

> require(RUnit) > # there are other automated test packages!

Page 69: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Code QualityError Case Unit Tests

• Use try

• Handled Error Cases

> test7 <- try(AUCLast(Conc = 1:10, Time = 9:0), + silent = TRUE)> checkEquals(test7, + "Error in checkOrderedVector(Time, ... ")[1] TRUE

> test26 <- ncaAnalysis(Conc = c(4, 9, 8, 6, 4:1, 1),+ Time = 0:8, Dof = 1)> checkEquals(test26[, "ROutput_Error"], + "Error in checkSingleNumeric(Dose, ... ")[1] TRUE

Page 70: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Connections with R.NET

• What will be provided to R?

• What will be returned from R?

• What happens if something goes wrong?

Page 71: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Connections with R.NETUsing the R Service

• R.NET allows R calls to be submitted to an R service

• R.NET connects to R down to Expression level

• Data from return objects passed back into .NET

Page 72: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Connections with R.NETData Checks

• Function may be passed data outside its anticipated structure

> checkOrderedVector(c(0, 1, 3, 2, 4), + description = "Time") Error in checkOrderedVector(c(0, 1, 3, 2, 4), description = "Time") : Error: Time is not ordered. Actual value is 0 1 3 2 4>

Page 73: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Connections with R.NETData Checks

• The tool expects a certain return object

• An error in an R call should be trapped by the communicating function

• Return object passed as normal

• An error checking element of the return object can report information about the error

> check01 <- try(checkOrderedVector(Time, + description = "Time"), silent = TRUE)> if (is(check01, "try-error")) { return(object) }

Page 74: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Connections with R.NET

_pluginsManager = new RPluginManager(PluginLocation, RLocation);_pluginsManager.SetActivePlugin();_session = _pluginsManager.GetSession();bool sessionOk = _pluginsManager.TryMakeSession(out _session);

• R is efficiently accessed, via R.Net (as pictured in Visual Studio) via a Plugin (as above)

Page 75: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Connections with R.NET

UserInterface

Data

AnalyticOutputs

DataStorage

AnalyticCode

Code Mgment

AnalyticEngine

Data Service

R.NET

Page 76: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

AnalysisDisplay

Get PK Params

DataService

DialogService

AppLogger

Status BarService

App Config

Mgment

DataImporters

ProjectWizard

Validators

Receive R

Output

Create R Expressns

Connections with R.NET.NET Data Service

R.NET

Page 77: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Connections with R.NETUsing the framework

_pluginsManager = new RPluginManager(PluginLocation, RLocation);_pluginsManager.SetActivePlugin();_session = _pluginsManager.GetSession();bool sessionOk = _pluginsManager.TryMakeSession(out _session);

_session.SetNumericSymbol("TimePtVector", CheckTimePointData(toAnalyse));_session.SetNumericSymbol("ConcVector", CheckConcentrationPointData(toAnalyse));

var evalString = string.Format("ncaAnalysis(TimePtVector, ConcVector, …

MathEngineDataRowDto<double> ncaGetBack = _session.PerformNumericEvaluation(evalString, "ROutput_Error");_lastErrors = ncaGetBack.ErrorStrings;

_session.FlushConsole();_pluginsManager.RelinquishSession();

Page 78: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Complete & Deploy RapidNCA

• Can users understand how to use tool?

• How confident are we in tool output?

• On-going code review• Independent test team• Installation Qualification• Operational Qualification• Performance Qualification

Page 79: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Deploy Tool

Page 80: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Data Import

Page 81: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Map Variables

Page 82: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Review Analysis

Page 83: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Review Grouping

Page 84: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Generate Report

Page 85: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Select Report Type

Page 86: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Add Group Comments

Page 87: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

View Report

Page 88: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Conclusions

• Great graphical interfaces can be built using .NET

• Intuitive interactive features are available

• R.NET allows R analysis to be accessed as a service

• Good coding practice will ensure application is robust

• Work on a well engineered framework will be rewarded with desktop solutions created at high speed

Page 89: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

CASE STUDYCOFFEE BLEND OPTIMIZATION

Page 90: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Company Background

• A global chocolatier, biscuit baker, candy maker and maker of gum.

Page 91: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Business /Technical Situation

• The client was using a desktop SPLUS application to simulate and optimise coffee blends for their manufacturing teams

• Hugely successful application saving the company $millions

• They wanted to make improvements and expand the usage beyond Global Statistics and beyond coffee

• Also keen to remove the license fee

Page 92: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Application Workflow

Import Data from Excel

Graphical Visualisations

Export Data

Run Blend Optimiser

Simulate Blends

Audit Log

Page 93: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

System Architecture

Functions for GUI

Functions for Analysis

R Package

Optimizer

Data Import

Data Export

Page 94: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Approach• Development phase split into three separate pieces:

• Code conversion

• GUI creation

• Development and integration of a new optimiser

• Each required the generation of unit and system tests and appropriate documentation, including help files

• Design specifications captured prior to development

• Project estimated at c90 man days over 3 months

Page 95: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Creation of new GUI

Page 96: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

GUI ChoicesSome R/R-based technologies we could have used...

• tcltk is R’s ‘recommended’ menu builder

• Glade, RGtk2

• gWidgets

• rpanel

• Deducer

• manipulate (Rstudio)

• ...

Page 97: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

GUI Choices

Other options:

• Choice is almost limitless

• Often they require a knowledge of other languages such as Java or C

• Possibly warrants a standalone talk...

Page 98: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Creation of a New GUI using RGtk2• RGtk2 adapter for R of the GTK+ engine

• Gimp Toolkit

• Glade can be used to trial new features

• GTK allows for automated testing of the GUI• Huge time saving

Page 99: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.
Page 100: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.
Page 101: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Code Conversion

Mango took a test-based approach for the code conversion (RUnit)

• Allows for automated testing in future revisions

• Simple PASS/FAIL reporting

• SPLUS knowledge not required for R code development

Page 102: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.
Page 103: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Optimization

• The original SPLUS application used the SPLUS NuOpt optimizer

• R NuOpt exists but only on license

• Mango used an open source optimiser that we integrated into the R GUI

• Mango implemented a ‘quick run’ option to allow quick comparisons with the simulation piece

Page 104: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.
Page 105: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Primary Benefits

• New departments are now benefitting from the application

• The application is now in the hands of the manufacturing teams, reducing the burden on Global Statistics

• Test-based approach facilitates future development of the application

Page 106: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

CASE STUDYPIPELINE CORROSION APP

Page 107: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Background

• One of the biggest companies in the world with thousands of staff

• Oilfield Exploration Team based in the UK but with responsibility for complex exploration areas• Alaska, shale fields etc

Page 108: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Business Situation

• Thousands of miles of pipeline corroding in freezing, isolated areas

• How do you choose how often to inspect them?

• The cost of a leak can run into many billions of £s

Page 109: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Technical Situation

• Customer Team were analysing data using S-PLUS Insightful Miner with many non-analytical workarounds

• Process was messy and took a long time to run

Page 110: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

System Architecture• This piece is one of several in a continuous

workflow

• All information is fed back into the database

Functions for GUI

Functions for Analysis

R Package

Access Database

General Workflow

Read

Write

Page 111: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Approach• Consulting engagement to improve programming

techniques and statistical methodology

• Create an R package for the code

• Construct a GUI in order to deploy to non-technical users on the frontline

Page 112: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.
Page 113: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.
Page 114: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

An Interesting Challenge:Converting S-Plus Code to R

Page 115: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

This is Easy, Right?

Some (true?) statements:

• R can be considered as a different implementation of S

• There are some important differences, but much code written for S runs unaltered under R

Discuss...

Source: www.r-project.org

Page 116: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Considerations

S+ applications can generally be split into two pieces:

• An underlying library of code

• A set of functions defining the menu system and help pages

Page 117: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Approach

There are essentially two approaches to code conversion:

• Direct Conversion

• Test-based Conversion

Page 118: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Direct Conversion

• Requires knowledge of both languages (stdev vs sd)

• Relatively quick to achieve

• Difficult to prove the new code does what the old code did

Page 119: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Test-based Conversion

• Generating unit tests in S+ requires some S+ knowledge

• Takes some time to generate and document tests but better in the long-run

• Unit tests give a definitive PASS/FAIL result

• Can often be automated

Page 120: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Code Conversion Challenges

• The application upgrade usually coincides with an operating system upgrade

• Windows (or other) version and R version need to be determined in advance

• It is almost guaranteed that the new system will produce different results for the same test data!

Page 121: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

What is “different”?

• Often this is simply rounding• Still require agreement on precision: 0.049782 vs

0.050436

• If simulation is involved this can be VERY difficult to define!!!

• Appearance of graphics may also differ

Page 122: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Other Challenges

As the business owner I want to use the opportunity to improve the application:

• New menu items

• New functionality

• Modifications to existing functionality

All of these require careful planning

Page 123: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Primary Benefits for Customer

• Rationalised code base means the analysis is quicker and extensible by end-users

• Construction of a front-end has enable rollout to users on the font-line in Alaska

• Conversion to R has removed license cost

Page 124: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

CASE STUDYBACKTESTING APP FOR HEDGE FUND

Page 125: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: Backtesting AppOverview• Backtesting has a key role to play in the testing

of automated trading strategies• Asked by a Hedge Fund Manager to build for his

team of users (who love Excel)• Mango were asked to build a backtesting

platform that was more sophisticated that what was on offer from other vendors

• Sorry that the details may be occasionally sketchy in this section

Page 126: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: Backtesting AppThe Challenge• Key parts of the challenge included:

• Integration with standard finance data streams

• Advanced portfolio optimisation• Flexibility to define automated strategy• Transaction-cost based benefit analysis• Leverage of financial hurdle• ARCH-style error incorporation• Advanced reporting

Page 127: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

AlphaStorage

DataStorage

DataFlow

.NETInterface

RdotNet

CInterface!

.RdaFiles

Page 128: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

How I learnt apply functions!!Some hacky code here …

Page 129: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Case Study: Backtesting AppThe Outcome• Very successful hedge fund• Convinced the users to use R – UI dropped!

Page 130: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

The R Community

Page 131: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

IP Considerations

• IP based on R includes:• New libraries & code• New scripts

• Mango attempt to open source (with client permission) any “R-side” generic functionality

• Also feedback and assist library authors

UserInterface

AnalyticCode

New RLibraries

Page 132: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Great Example

• MSToolkit library built for Pfizer• Funded by Pfizer, built by Mango• Released as open source library• Since extended by other companies

Page 133: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

R Community

• Contribute code where allowed/useful• Sponsor R conferences and events• Provide free training courses / webinars• Organise and fund many R user groups

(LondonR, BaselR, ZurichR, ShanghaiR, NewJerseyR, …)

Page 134: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

The End!

Page 135: Designing and Creating applications built on R Richard Pugh, Andy Nicholls & Chris Campbell 23 rd October 2012.

Summary

• Thank you for the invitation• Hope the discussion was useful

• We could only cover certain amount of detail in time, so ask us for more if interested!


Recommended