R Dependencies
Promote models can use third party libraries. Install and load the CRAN randomForest package to follow this example.
install.packages("randomForest")
library(randomForest)
This example trains a model to classify flower species from an iris dataset. With this data, the model is intended to predict the species using the other data available in the dataset.
fit <- randomForest(Species ~ ., data=iris)
print(fit)
Call:
randomForest(formula = Species ~ ., data = iris)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 2
OOB estimate of error rate: 4%
Confusion matrix:
setosa versicolor virginica class.error
setosa 50 0 0 0.00
versicolor 0 47 3 0.06
virginica 0 3 47 0.06
To prepare this model for deployment to the Promote server, include randomForest in model.require.
The input to model.transform will be a a new flower record coming from the API. The output of this function will be sent into the model.predict function.
library(promote)
promote.library('randomForest',src="CRAN")
# model.require <- function() {
# library(randomForest)
# }
model.predict <- function(df) {
data.frame("predictedSpecies"=predict(fit, newdata=df))
}
To deploy this model to the Promote server, add your username and apikey to the R session and execute promote.deploy.
promote.config <- c(
username="YOUR_USERNAME",
apikey="YOUR_APIKEY",
env="https://promote.yourcompany.com"
)
promote.deploy("irisPredictor")
To make predictions with this model, use promote.predict. Verify the results from the Promote server are the same as the results obtained from your local machine.
promote.predict(model_name="irisPredictor", iris[1,])
predict(fit, iris[1,])