Title: | Convert R Models to PMML |
---|---|
Description: | R wrapper for the JPMML-R library <https://github.com/jpmml/jpmml-r>, which converts R models to Predictive Model Markup Language (PMML). |
Authors: | Villu Ruusmann [aut, cre] |
Maintainer: | Villu Ruusmann <[email protected]> |
License: | AGPL-3 |
Version: | 0.29.0 |
Built: | 2024-11-10 18:17:01 UTC |
Source: | https://github.com/jpmml/r2pmml |
Dispatches execution to the most appropriate XGBoost feature map generation function.
as.fmap(x)
as.fmap(x)
x |
A dataset object. |
Generates an XGBoost feature map based on feature data.
## S3 method for class 'data.frame' as.fmap(x)
## S3 method for class 'data.frame' as.fmap(x)
x |
A "data.frame" object with independent variables. |
A "data.frame" object.
data(iris) iris.df = iris[, 1:4] iris.fmap = as.fmap(iris.df)
data(iris) iris.df = iris[, 1:4] iris.fmap = as.fmap(iris.df)
Generates an XGBoost feature map based on feature data.
## S3 method for class 'matrix' as.fmap(x)
## S3 method for class 'matrix' as.fmap(x)
x |
A "matrix" object with independent variables. |
A "data.frame" object.
data(iris) iris.matrix = model.matrix(Species ~ . - 1, data = iris) iris.fmap = as.fmap(iris.matrix)
data(iris) iris.matrix = model.matrix(Species ~ . - 1, data = iris) iris.fmap = as.fmap(iris.matrix)
Converts a "glm" object to a "scorecard" object.
as.scorecard(glm, odds = 10, base_points = 500, pdo = 100)
as.scorecard(glm, odds = 10, base_points = 500, pdo = 100)
glm |
A "glm" object with binomial family link function. |
odds |
Odds ratio at base odds. |
base_points |
Points where odds ratio is defined. |
pdo |
Points to double the odds. |
A "scorecard" object.
Dispatches execution to the most appropriate model decoration function.
decorate(x, ...)
decorate(x, ...)
x |
A model object. |
... |
Arguments to pass on to the selected function. |
Decorates a model object with "preProcess" and "pmml_options" elements.
## Default S3 method: decorate(x, preProcess = NULL, pmml_options = NULL, ...)
## Default S3 method: decorate(x, preProcess = NULL, pmml_options = NULL, ...)
x |
The model object. |
preProcess |
A "train::preProcess" object. |
pmml_options |
A list of model type-dependent PMML conversion options. |
... |
Further arguments. |
Decorates an "earth" object with an "xlevels" element.
## S3 method for class 'earth' decorate(x, data, ...)
## S3 method for class 'earth' decorate(x, data, ...)
x |
An "earth" object. |
data |
The training dataset. |
... |
Arguments to pass on to the "decorate.default" function. |
Decorates an "elm" object with a "model" element.
## S3 method for class 'elm' decorate(x, data, ...)
## S3 method for class 'elm' decorate(x, data, ...)
x |
An "elm" object. |
data |
The training dataset. |
... |
Arguments to pass on to the "decorate.default" function. |
Decorates a "glmnet" object with a "lambda.s" element.
## S3 method for class 'glmnet' decorate(x, lambda.s, ...)
## S3 method for class 'glmnet' decorate(x, lambda.s, ...)
x |
A "glmnet" object. |
lambda.s |
The best lambda value. Must be one of listed "glmnet$lambda" values. |
... |
Arguments to pass on to the "decorate.default" function. |
library("glmnet") library("r2pmml") data(iris) iris_X = as.matrix(iris[, -ncol(iris)]) iris_y = iris[, ncol(iris)] iris.glmnet = glmnet(x = iris_X, y = iris_y, family = "multinomial") iris.glmnet = decorate(iris.glmnet, lambda.s = iris.glmnet$lambda[49]) r2pmml(iris.glmnet, file.path(tempdir(), "Iris-GLMNet.pmml"))
library("glmnet") library("r2pmml") data(iris) iris_X = as.matrix(iris[, -ncol(iris)]) iris_y = iris[, ncol(iris)] iris.glmnet = glmnet(x = iris_X, y = iris_y, family = "multinomial") iris.glmnet = decorate(iris.glmnet, lambda.s = iris.glmnet$lambda[49]) r2pmml(iris.glmnet, file.path(tempdir(), "Iris-GLMNet.pmml"))
Decorates a "party" object with a "predicted" element.
## S3 method for class 'party' decorate(x, ...)
## S3 method for class 'party' decorate(x, ...)
x |
A "party" object. |
... |
Arguments to pass on to the "decorate.default" function. |
library("evtree") library("r2pmml") data(iris) iris.party = evtree(Species ~ ., data = iris, control = evtree.control(max_depth = 3)) iris.party = decorate(iris.party) r2pmml(iris.party, file.path(tempdir(), "Iris-Party.pmml"))
library("evtree") library("r2pmml") data(iris) iris.party = evtree(Species ~ ., data = iris, control = evtree.control(max_depth = 3)) iris.party = decorate(iris.party) r2pmml(iris.party, file.path(tempdir(), "Iris-Party.pmml"))
Decorates a "randomForest" object with PMML conversion options.
## S3 method for class 'randomForest' decorate(x, compact = FALSE, ...)
## S3 method for class 'randomForest' decorate(x, compact = FALSE, ...)
x |
A "randomForest" object. |
compact |
A flag controlling if decision trees should be transformed from binary splits (FALSE) to multi-way splits (TRUE) representation. |
... |
Arguments to pass on to the "decorate.default" function. |
Decorates a "ranger" object with a "variable.levels" element.
## S3 method for class 'ranger' decorate(x, data, ...)
## S3 method for class 'ranger' decorate(x, data, ...)
x |
A "ranger" object. |
data |
The training dataset. |
... |
Arguments to pass on to the "decorate.default" function. |
library("ranger") library("r2pmml") data(iris) iris.ranger = ranger(Species ~ ., data = iris, num.trees = 17, write.forest = TRUE, probability = TRUE) iris.ranger = decorate(iris.ranger, data = iris) r2pmml(iris.ranger, file.path(tempdir(), "Iris-Ranger.pmml"))
library("ranger") library("r2pmml") data(iris) iris.ranger = ranger(Species ~ ., data = iris, num.trees = 17, write.forest = TRUE, probability = TRUE) iris.ranger = decorate(iris.ranger, data = iris) r2pmml(iris.ranger, file.path(tempdir(), "Iris-Ranger.pmml"))
Decorates a "svm.formula" object with an "xlevels" element.
## S3 method for class 'svm.formula' decorate(x, data, ...)
## S3 method for class 'svm.formula' decorate(x, data, ...)
x |
A "svm.formula" object. |
data |
The training dataset. |
... |
Arguments to pass on to the "decorate.default" function. |
Decorates the final model of a "train" object with model type-dependent elements.
## S3 method for class 'train' decorate(x, ...)
## S3 method for class 'train' decorate(x, ...)
x |
A "train" object. |
... |
Arguments to pass on to the "decorate.default" function. |
Decorates a "WrappedModel" object with "invert_levels" element. Additionally, decorates the learned model with model type-dependent elements.
## S3 method for class 'WrappedModel' decorate(x, invert_levels = FALSE, ...)
## S3 method for class 'WrappedModel' decorate(x, invert_levels = FALSE, ...)
x |
A "WrappedModel" object. |
invert_levels |
A flag indicating if the learned model should assume normal (FALSE) or inverted (TRUE) ordering of category values for the binary categorical target field. |
... |
Arguments to pass on to the "decorate.default" function |
Decorates an "xgb.Booster" object with "fmap", "schema", "ntreelimit" and "pmml_options" elements.
## S3 method for class 'xgb.Booster' decorate( x, fmap, response_name = NULL, response_levels = c(), missing = NULL, ntreelimit = NULL, compact = FALSE, ... )
## S3 method for class 'xgb.Booster' decorate( x, fmap, response_name = NULL, response_levels = c(), missing = NULL, ntreelimit = NULL, compact = FALSE, ... )
x |
An "xgb.Booster" object. |
fmap |
An XGBoost feature map as a "data.frame" object. |
response_name |
The name of the target field. |
response_levels |
A list of category values for a categorical target field. |
missing |
The string representation of missing input field values. |
ntreelimit |
The number of decision trees (aka boosting rounds) to convert. |
compact |
A flag controlling if decision trees should be transformed from binary splits (FALSE) to multi-way splits (TRUE) representation. |
... |
Arguments to pass on to the "decorate.default" function. |
library("xgboost") library("r2pmml") data(iris) iris_X = iris[, -ncol(iris)] iris_y = iris[, ncol(iris)] # Convert from factor to integer[0, num_class] iris_y = (as.integer(iris_y) - 1) iris.matrix = model.matrix(~ . - 1, data = iris_X) iris.DMatrix = xgb.DMatrix(iris.matrix, label = iris_y) iris.fmap = as.fmap(iris.matrix) iris.xgboost = xgboost(data = iris.DMatrix, objective = "multi:softprob", num_class = 3, nrounds = 11) iris.xgboost = decorate(iris.xgboost, iris.fmap, response_name = "Species", response_levels = c("setosa", "versicolor", "virginica")) pmmlFile = file.path(tempdir(), "Iris-XGBoost.pmml") r2pmml(iris.xgboost, pmmlFile, compact = FALSE) compactPmmlFile = file.path(tempdir(), "Iris-XGBoost-compact.pmml") r2pmml(iris.xgboost, compactPmmlFile, compact = TRUE)
library("xgboost") library("r2pmml") data(iris) iris_X = iris[, -ncol(iris)] iris_y = iris[, ncol(iris)] # Convert from factor to integer[0, num_class] iris_y = (as.integer(iris_y) - 1) iris.matrix = model.matrix(~ . - 1, data = iris_X) iris.DMatrix = xgb.DMatrix(iris.matrix, label = iris_y) iris.fmap = as.fmap(iris.matrix) iris.xgboost = xgboost(data = iris.DMatrix, objective = "multi:softprob", num_class = 3, nrounds = 11) iris.xgboost = decorate(iris.xgboost, iris.fmap, response_name = "Species", response_levels = c("setosa", "versicolor", "virginica")) pmmlFile = file.path(tempdir(), "Iris-XGBoost.pmml") r2pmml(iris.xgboost, pmmlFile, compact = FALSE) compactPmmlFile = file.path(tempdir(), "Iris-XGBoost-compact.pmml") r2pmml(iris.xgboost, compactPmmlFile, compact = TRUE)
Converts an R model object to PMML.
r2pmml( x, file, schema = NULL, converter = NULL, converter_classpath = NULL, verbose = FALSE, ... )
r2pmml( x, file, schema = NULL, converter = NULL, converter_classpath = NULL, verbose = FALSE, ... )
x |
An R model object. |
file |
A filesystem path to the result file. |
schema |
The PMML schema version for the PMML document. |
converter |
The name of a custom JPMML-R converter class. |
converter_classpath |
A list of filesystem paths to library JAR files that provide and support the custom JPMML-R converter class. |
verbose |
A flag controlling the verbosity of the conversion process. |
... |
Arguments to be passed on to the "r2pmml::decorate" function. |
library("mlbench") library("randomForest") library("r2pmml") data(iris) iris.rf = randomForest(Species ~ ., data = iris, ntree = 7) # Convert "randomForest" object to R-style (deep binary splits) MiningModel pmmlFile = file.path(tempdir(), "Iris-RandomForest.pmml") r2pmml(iris.rf, pmmlFile) # Convert "randomForest" object to PMML-style (shallow multi-way splits) MiningModel compactPmmlFile = file.path(tempdir(), "Iris-RandomForest-compact.pmml") r2pmml(iris.rf, compactPmmlFile, compact = TRUE) data(BostonHousing) housing.glm = glm(medv ~ ., data = BostonHousing, family = "gaussian") # Convert "glm" object into GeneralRegressionModel genRegPmmlFile = file.path(tempdir(), "Housing-GLM.pmml") r2pmml(housing.glm, genRegPmmlFile) # Convert "glm" object into RegressionModel regPmmlFile = file.path(tempdir(), "Housing-LM.pmml") r2pmml(housing.glm, regPmmlFile, converter = "org.jpmml.rexp.LMConverter")
library("mlbench") library("randomForest") library("r2pmml") data(iris) iris.rf = randomForest(Species ~ ., data = iris, ntree = 7) # Convert "randomForest" object to R-style (deep binary splits) MiningModel pmmlFile = file.path(tempdir(), "Iris-RandomForest.pmml") r2pmml(iris.rf, pmmlFile) # Convert "randomForest" object to PMML-style (shallow multi-way splits) MiningModel compactPmmlFile = file.path(tempdir(), "Iris-RandomForest-compact.pmml") r2pmml(iris.rf, compactPmmlFile, compact = TRUE) data(BostonHousing) housing.glm = glm(medv ~ ., data = BostonHousing, family = "gaussian") # Convert "glm" object into GeneralRegressionModel genRegPmmlFile = file.path(tempdir(), "Housing-GLM.pmml") r2pmml(housing.glm, genRegPmmlFile) # Convert "glm" object into RegressionModel regPmmlFile = file.path(tempdir(), "Housing-LM.pmml") r2pmml(housing.glm, regPmmlFile, converter = "org.jpmml.rexp.LMConverter")
Dispatches execution to the most appropriate model verification function.
verify(x, newdata, ...)
verify(x, newdata, ...)
x |
A model object. |
newdata |
The verification dataset. |
... |
Arguments to pass on to the selected function. |
Enhances a model object with verification data.
## Default S3 method: verify(x, newdata, ...)
## Default S3 method: verify(x, newdata, ...)
x |
A model object. |
newdata |
The verification dataset. |
... |
Further arguments. |
Enhances a "glm" object with verification data.
## S3 method for class 'glm' verify(x, newdata, precision = 1e-13, zeroThreshold = 1e-13, ...)
## S3 method for class 'glm' verify(x, newdata, precision = 1e-13, zeroThreshold = 1e-13, ...)
x |
A "glm" object. |
newdata |
The verification dataset. |
precision |
Maximal relative error. |
zeroThreshold |
Maximal absolute error near the zero value. |
... |
Further arguments. |
library("mlbench") library("r2pmml") data(BostonHousing) housing.glm = glm(medv ~ ., data = BostonHousing, family = "gaussian") housing.glm = verify(housing.glm, newdata = BostonHousing[sample(nrow(BostonHousing), 10), ]) r2pmml(housing.glm, file.path(tempdir(), "Housing-GLM-verified.pmml"))
library("mlbench") library("r2pmml") data(BostonHousing) housing.glm = glm(medv ~ ., data = BostonHousing, family = "gaussian") housing.glm = verify(housing.glm, newdata = BostonHousing[sample(nrow(BostonHousing), 10), ]) r2pmml(housing.glm, file.path(tempdir(), "Housing-GLM-verified.pmml"))
Enhances a "train" object with verification data.
## S3 method for class 'train' verify(x, newdata, precision = 1e-13, zeroThreshold = 1e-13, ...)
## S3 method for class 'train' verify(x, newdata, precision = 1e-13, zeroThreshold = 1e-13, ...)
x |
A "train" object. |
newdata |
The verification dataset. |
precision |
Maximal relative error. |
zeroThreshold |
Maximal absolute error near the zero value. |
... |
Arguments to pass on to the "predict.train" method. |
Enhances an "xgb.Booster" object with verification data.
## S3 method for class 'xgb.Booster' verify( x, newdata, precision = 1e-06, zeroThreshold = 1e-06, response_name = NULL, response_levels = c(), ... )
## S3 method for class 'xgb.Booster' verify( x, newdata, precision = 1e-06, zeroThreshold = 1e-06, response_name = NULL, response_levels = c(), ... )
x |
An "xgb.Booster" object. |
newdata |
The verification dataset. |
precision |
Maximal relative error. |
zeroThreshold |
Maximal absolute error near the zero value. |
response_name |
The name of the target field. |
response_levels |
A list of category values for a categorical target field. |
... |
Arguments to pass on to the "predict.xgb.Booster" method. |
Writes XGBoost feature map to a file.
write.fmap(fmap, file)
write.fmap(fmap, file)
fmap |
An XGBoost feature map as a "data.frame" object. |
file |
A filesystem path to the result file. |