r 函数到 pmml 成功但在容器中未被识别

r function to pmml success but not recognized in container

这是一个使用几个库的 R 脚本示例

library(pmml)
library(pmmlTransformations)
data(iris)
irisBox <- WrapData(iris)
irisBox <- FunctionXform(irisBox,origFieldName="Sepal.Length",
           newFieldName="Sepal.Length.Transformed",
           formulaText="ifelse(Sepal.Length>5,Sepal.Length*1.2, Sepal.Length*.8)")
mod1 <- lm(Sepal.Length.Transformed ~ Petal.Length, irisBox$data)
pmml(mod1, transform = irisBox)

该函数运行良好,并创建了一个不错的 pmml 输出。但是ifelse语句并不是pmml中可识别的函数4_3。有人可以推荐上述脚本的替代方案来生成 pmml 可行命令吗?

我知道pmmlTransformations 包中推荐使用DiscretizeXform,但它非常麻烦,所以我不愿意使用,因为它必须从外部文件读取断点。

pmmlTransformations 包不知道如何处理 "ifelse" R 函数,并按原样传递它。这就是为什么生成的 PMML 文档包含 Apply@function="ifelse" 而它应该包含 Apply@function="if".

PMML "if" built-in function 完全可以表示 if-else 分支逻辑:

Implements IF-THEN-ELSE logic. The ELSE part is optional. If the ELSE part is absent and the boolean value is false then a missing value is returned.

至于解决方案,请考虑切换到 r2pmml package,它允许您在公式内部进行特征工程(而不是 "boxing" data.frame 对象),包括全面支持对于 "ifelse" R 函数:

library("randomForest")
library("r2pmml")

iris.rf = randomForest(Species ~ ifelse(Sepal.Length>5,Sepal.Length*1.2, Sepal.Length*.8) + ., data = iris)

r2pmml(iris.rf, "iris.pmml")

看起来简单的替换就可以了。

irisBox <- WrapData(iris)
irisBox <- FunctionXform(irisBox,origFieldName="Sepal.Length",
       newFieldName="Sepal.Length.Transformed",
       formulaText="if(Sepal.Length>5) Sepal.Length*1.2 else Sepal.Length*.8")
mod1 <- lm(Sepal.Length.Transformed ~ Petal.Length, irisBox$data)
pmml(mod1, transform = irisBox)