concept vtreat in category R

This is an excerpt from Manning's book Practical Data Science with R, Second Edition.
Non-statisticians are often surprised that you can use non-numeric columns (such as strings or factors) as inputs to or variables in models. This can be accomplished a number of ways, and the most common one is a method called introducing indicators or dummy variables. In R this encoding is often automatic and unseen. In other systems (such as Python’s scikit-learn), the analyst must specify an encoding (through a method name such as “one-hot”). In this book, we will use this encoding and additional, more sophisticated encodings from the vtreat package. The SCHL column can be explicitly converted into basic dummy variables as we show next. This recoding strategy will be used both implicitly and explicitly in the book, so we will demonstrate it here:
vtreat is an R package designed to prepare real-world data for supervised learning or predictive modeling. It is designed to deal with a lot of common issues, so the data scientist doesn’t have to. This leaves them much more time to find and work on unique domain-dependent issues. vtreat is an excellent realization of the concepts discussed in chapter 4 as well as many other concepts. One of the goals of chapter 4 was to give you an understanding of some of the issues we can run into working with data, and principled steps to take in dealing with such data. vtreat automates these steps into a high-performance production-capable package, and is a formally citable methodology you can incorporate into your own work. We can’t succinctly explain everything vtreat does with data, as it does a lot; for details please see the long-form documentation here: https://arxiv.org/abs/1611.09477. In addition, vtreat has many explanatory vignettes and worked examples here: https://CRAN.R-project.org/package=vtreat.
We will work through vtreat’s capabilities in this chapter using an example of predicting account cancellation (called customer churn) using the KDD Cup 2009 dataset. In this example scenario, we will use vtreat to prepare the data for use in later modeling steps. Some of the issues vtreat helps with include the following:
The basic vtreat workflow (shown in figure 8.2) is to use some of the training data to create a treatment plan that records key characteristics of the data such as relationships between individual variables and the outcome. This treatment plan is then used to prepare data that will be used to fit the model, as well as to prepare data that the model will be applied to. The idea is that this prepared or treated data will be “safe,” with no missing or unexpected values, and will possibly have new synthetic variables that will improve the model fitting. In this sense, vtreat itself looks a lot like a model.
We saw a simple use of vtreat in chapter 4 to treat missing values. In this chapter, we will use vtreat’s full coding power on our customer churn example. For motivation, we will solve the KDD Cup 2009 problem, and then we will discuss how to use vtreat in general.
Listing 8.5. Preparing data with vtreat
dTrain_treated <- prepare(treatment_plan, dTrain, parallelCluster = parallel_cluster) head(colnames(dTrain)) ## [1] "Var1" "Var2" "Var3" "Var4" "Var5" "Var6" head(colnames(dTrain_treated)) #1 ## [1] "Var1" "Var1_isBAD" "Var2" "Var2_isBAD" "Var3" ## [6] "Var3_isBAD"
In addition to many domain-specific or problem-specific problems, you may find that in your data, there are a number of common problems that should be anticipated and dealt with systematically. vtreat is a package specialized for preparing data for supervised machine learning or predictive modeling tasks. It can also reduce your project documentation requirements through its citable documentation.[20] However, remember that tools are not an excuse to avoid looking at your data.