Cookies help us display personalized product recommendations and ensure you have great shopping experience.

By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
SmartData CollectiveSmartData Collective
  • Analytics
    AnalyticsShow More
    data driven insights
    How Data-Driven Insights Are Addressing Gaps in Patient Communication and Equity
    8 Min Read
    pexels pavel danilyuk 8112119
    Data Analytics Is Revolutionizing Medical Credentialing
    8 Min Read
    data and seo
    Maximize SEO Success with Powerful Data Analytics Insights
    8 Min Read
    data analytics for trademark registration
    Optimizing Trademark Registration with Data Analytics
    6 Min Read
    data analytics for finding zip codes
    Unlocking Zip Code Insights with Data Analytics
    6 Min Read
  • Big Data
  • BI
  • Exclusive
  • IT
  • Marketing
  • Software
Search
© 2008-25 SmartData Collective. All Rights Reserved.
Reading: Converting Arbitrary R Expressions to PMML
Share
Notification
Font ResizerAa
SmartData CollectiveSmartData Collective
Font ResizerAa
Search
  • About
  • Help
  • Privacy
Follow US
© 2008-23 SmartData Collective. All Rights Reserved.
SmartData Collective > Uncategorized > Converting Arbitrary R Expressions to PMML
Uncategorized

Converting Arbitrary R Expressions to PMML

MichaelZeller
MichaelZeller
8 Min Read
SHARE

The pmmlTransformations R package can be used to transform data and add new features to be used in predictive PMML models.

Contents
How it worksSingle numeric fieldMultiple input fieldsPMML for arbitrary functionsConclusionReferences

In this blog post, we will focus on FunctionXform, a function introduced in version 1.3.0 of pmmlTransformations, and present a few examples of using it to create new data features.

The pmmlTransformations R package can be used to transform data and add new features to be used in predictive PMML models.

In this blog post, we will focus on FunctionXform, a function introduced in version 1.3.0 of pmmlTransformations, and present a few examples of using it to create new data features.

How it works

Transformations in the pmmlTransformations package work in the following manner: given a WrapData object and a transformation name, the code calculates data for a new feature and creates a new WrapData object. This new object is then passed in as the data argument when training an R model with a compatible R package. When PMML is produced with pmml::pmml(), the transformation is inserted into the LocalTransformations node as a DerivedField. Any original fields used by transformations are added to the appropriate nodes in the resulting PMML file.

While other transformations in the package transform only one field, FunctionXform makes it possible to use multiple data fields and functions to produce a new feature.

Note that while FunctionXform is part of the pmmlTransformations package, the code to produce PMML from R is in the pmml package. The following examples require both packages to be installed to work.

To make tables more readable in this blog post, we are using the kable function (part of knitr).

Single numeric field

Using the iris dataset as an example, let’s construct a new numeric feature by transforming one variable.

First, load the required libraries:

library(pmml)
library(pmmlTransformations)
library(knitr)

Then load the data and display the first 3 lines:

data(iris)
kable(head(iris,3))
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa

Create the irisBox wrapper object with WrapData:

irisBox <- WrapData(iris)

irisBox contains the data and transform information that will be used to produce PMML later. The original data is in irisBox$data. Any new features created with a transformation are added as columns to this data frame.

kable(head(irisBox$data,3))
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa

Transform and field information is in irisBox$fieldData. The fieldData data frame contains information on every field in the dataset, as well as every transform used. The functionXform column contains expressions used in the FunctionXform transform. Here we’ll show only a few of the columns:

kable(irisBox$fieldData[,1:5])
 typedataTypeorigFieldNamesampleMinsampleMax
Sepal.LengthoriginalnumericNANANA
Sepal.WidthoriginalnumericNANANA
Petal.LengthoriginalnumericNANANA
Petal.WidthoriginalnumericNANANA
SpeciesoriginalfactorNANANA

Now add a new feature, Sepal.Length.Sqrt, using FunctionXform:

irisBox <- FunctionXform(irisBox,origFieldName="Sepal.Length",
newFieldName="Sepal.Length.Sqrt",
formulaText="sqrt(Sepal.Length)")

The new feature is calculated and added as a column to the irisBox$data data frame:

kable(head(irisBox$data,3))
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpeciesSepal.Length.Sqrt
5.13.51.40.2setosa2.258318
4.93.01.40.2setosa2.213594
4.73.21.30.2setosa2.167948

irisBox$fieldData now contains a new row with the transformation expression in the functionXform column:

kable(irisBox$fieldData[6,c(1:3,14)])
 typedataTypeorigFieldNamefunctionXform
Sepal.Length.SqrtderivednumericSepal.Lengthsqrt(Sepal.Length)

Construct a linear model to predict Petal.Width using this new feature, and convert it to PMML:

fit <- lm(Petal.Width ~ Sepal.Length.Sqrt, data=irisBox$data)
fit_pmml <- pmml(fit, transform=irisBox)

Since the model predicts Petal.Width using a variable based on Sepal.Length, Sepal.Length will be added to the DataDictionary and MiningSchema nodes in the resulting PMML. We can take a look at the relevant parts of the output like so:

fit_pmml[[2]] #Data Dictionary node
#>
#>
#>
#>
fit_pmml[[3]][[1]] #Mining Schema node
#>
#>
#>
#>

The LocalTransformations node contains Sepal.Length.Sqrt as a derived field:

fit_pmml[[3]][[3]]
#>
#>
#>
#>
#>
#>
#>

The PMML model can now be deployed and consumed. For any input data, the new Sepal.Length.Sqrt feature will be created when the data is scored against the model.

Multiple input fields

It is also possible to create new features by combining several fields. Using the same iris dataset, let’s create a new field using squares of Sepal.Length and Petal.Length:

irisBox <- WrapData(iris)
irisBox <- FunctionXform(irisBox,origFieldName="Sepal.Length,Petal.Length",
newFieldName="Squared.Length.Ratio",
formulaText="(Sepal.Length / Petal.Length)^2")

As before, the new field is added as a column to the irisBox$data data frame:

kable(head(irisBox$data,3))
Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpeciesSquared.Length.Ratio
5.13.51.40.2setosa13.27041
4.93.01.40.2setosa12.25000
4.73.21.30.2setosa13.07101

Fit a linear model for Petal.Length using this new feature, and convert it to PMML:

fit <- lm(Petal.Width ~ Squared.Length.Ratio, data=irisBox$data)
fit_pmml <- pmml(fit, transform=irisBox)

The PMML will contain Sepal.Length and Petal.Length in the DataDictionary and MiningSchema, since these were used in FormulaXform:

fit_pmml[[2]] #Data Dictionary node
#>
#>
#>
#>
#>
fit_pmml[[3]][[1]] #Mining Schema node
#>
#>
#>
#>
#>

The Local.Transformations node contains Squared.Length.Ratio as a derived field:

fit_pmml[[3]][[3]]
#>
#>
#>
#>
#>
#>
#>
#> 2
#>
#>
#>

PMML for arbitrary functions

The function functionToPMML (part of the pmml package) makes it possible to convert an R expression into PMML directly, without creating a model or calculating values. This can be useful for debugging.

As long as the expression passed to the function is a valid R expression (e.g., no unbalanced parentheses), it can contain arbitrary function names not defined in R. Constants in the expression passed to FunctionXform are always assumed to be of type double. Variables in the expression are always assumed to be field names, and not substituted. That is, even if x has a value in the R environment, the resulting expression will still use x.

functionToPMML("1 + 2")
#>
#> 1
#> 2
#>

x <- 3
functionToPMML("foo(bar(x * y))")
#>
#>
#>
#>
#>
#>
#>
#>

functionToPMML("if(a<2) else if (a>3) {'four'} else ")
#>
#>
#>
#> 2
#>
#>
#>
#> 3
#>
#>
#>
#>
#> 3
#>
#> four
#> 5
#>
#>

Conclusion

functionXform makes it possible to easily create new features for PMML models with R.

The pmmlTransformations functionXform vignette contains additional examples, including transforming categorical data, using transformed features in another transform, unsupported functions, and notes on limitations of the function.

References

  • pmmlTransformations on CRAN
  • PMML 4.2.1 official specification
  • In-depth discussion of pmmlTransformations
Share This Article
Facebook Pinterest LinkedIn
Share

Follow us on Facebook

Latest News

langgraph and genai
LangGraph Orchestrator Agents: Streamlining AI Workflow Automation
Artificial Intelligence Exclusive
ai fitness app
Will AI Replace Personal Trainers? A Data-Driven Look at the Future of Fitness Careers
Artificial Intelligence Big Data Exclusive
crypto marketing
How a Crypto Marketing Agency Can Use AI to Create Powerful Native Advertising Strategies
Blockchain Exclusive Marketing
data driven insights
How Data-Driven Insights Are Addressing Gaps in Patient Communication and Equity
Analytics Big Data Exclusive

Stay Connected

1.2kFollowersLike
33.7kFollowersFollow
222FollowersPin

You Might also Like

Top Six Traits of a Data Champion

6 Min Read
Image
Uncategorized

The Rise of Embedded BI

3 Min Read

Reasons why Foursquare is the hottest Social Network on the web

4 Min Read

My Thoughts on the “Washington’s Tech Titans” List

5 Min Read

SmartData Collective is one of the largest & trusted community covering technical content about Big Data, BI, Cloud, Analytics, Artificial Intelligence, IoT & more.

giveaway chatbots
How To Get An Award Winning Giveaway Bot
Big Data Chatbots Exclusive
data-driven web design
5 Great Tips for Using Data Analytics for Website UX
Big Data

Quick Link

  • About
  • Contact
  • Privacy
Follow US
© 2008-25 SmartData Collective. All Rights Reserved.
Go to mobile version
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?