Kuri R API

Getting your R code to work with Kuri is as simple as loading the library and incorporating three function calls (two of which are optional) into your code.

Load Dependencies

kuri::load_depedencies(c("pkgName1", "pkgName2", ...))

The load_dependencies function is the only function that is required as part of using R with Kuri. It provides Kuri with a list of libraries required to run your model, ensuring that they are installed prior to the running of your R script. Whether you are running a local instance or on a remote server, Kuri does the installation of any libraries you list as part of your load_dependencies call.

Record Metrics

kuri::record_metrics(metricsList)

Kuri provides the ability to track metrics over time. The type and number of these metrics is entirely configured by the user. The only requirement is that metrics be passed to record_metrics as a list.

As an example, consider the case of predicting who will default on a home loan. A simple logistic regression model will predict whether or not a particular individual is a zero (not predicted to default) or a one (predicted to default). One metric that might be of interest is the percentage of individuals that are predicted to default; a second metric might be the total number of individuals predicted to default. As calculated in your code, they might look like this:

percentDefaults <- totalDefaults/totalLoans #Percentage of people predicted to default

metricsList <- list()
metricsList$percentDefaults <- percentDefaults
metricsList$totalDefaults <- totalDefaults

kuri::record_metrics(metricsList)

Attach File

kuri::attach_file()

With Kuri, you have the ability to specify files that you'd like to include as attachments to the reporting email that is sent at the conclusion of each job. For any files that are generated as part of your R script, simply call kuri::attach_file(filename) and they will be included as an email attachment.

Example Script

The example below shows how the API described above might be used together.

# Example R code for integration with the Kuri Model Management and Monitoring Tool

# This script takes the model that was built in hmeqBuild.R and puts it into production.

# load_dependencies function is required to make sure all packages needed to run your script are loaded properly.
library(kuri)
kuri::load_dependencies(c("rpart", "rpart.plot", "plyr", "dplyr", "stringr"))

# Load in model that was created previously
load(file="decTreeModel.Rdata")

# Load in data that we want to run through the model and sample
load(file="test_hmeq.Rdata")

# Use the predict function to run new data through the model
sampleTestData <- sample_n(testData, 0.30*nrow(testData), replace=FALSE)
predTestDefault <- predict(decTree, newdata=sampleTestData)

# Calculate a metric that we can use without feedback loop.
thresh <- 0.5
testPred <- as.integer(unlist(lapply(predTestDefault[,2], function(x) if (x >= thresh) 1 else 0)))
pctOnes <- sum(testPred)/length(testPred)
numberOnes <- sum(testPred)

# Call kuri::record_metrics to make sure metrics are recorded each time script is run.
# kuri::record_metrics takes a list of metric/value pairs.
metricsList <- list()
metricsList$pctOnes <- pctOnes
metricsList$numberOnes <- numberOnes

kuri::record_metrics(metricsList)

#Generate a new .csv of data to include as an email attachment
newCustLoanData <- data.frame(custID=row.names(sampleTestData),
                              loanAmount=sampleTestData$LOAN,
                              mortgageDue=sampleTestData$MORTDUEIMP,
                              job=sampleTestData$JOBIMP,
                              debtIncomeRatio=sampleTestData$DEBTINCIMP,
                              defaultProbability=predTestDefault[,2],
                              loanDefaultPred=testPred)
write.csv(newCustLoanData, file="new_cust_loan_data.csv")

# Call kuri::attach_file to make sure that this .csv file is included as part of the report sent via email
kuri::attach_file("new_cust_loan_data.csv")

# Create a plot of the generated decision tree and call kuri::attach_file to include in emailed report.
cairo_pdf("loan_default_dec_tree.pdf", width=11, height=8)
prp(decTree, type=2, extra=101)
dev.off()
kuri::attach_file("loan_default_dec_tree.pdf")