<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/you-should-re-encode-high-cardinality-categorical-variables/"></div></div><div><a data-url="https://www.r-bloggers.com/you-should-re-encode-high-cardinality-categorical-variables/" data-counturl="https://www.r-bloggers.com/you-should-re-encode-high-cardinality-categorical-variables/" data-count="horizontal" data-text="You should re-encode high cardinality categorical variables" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2016/11/you-should-re-encode-high-cardinality-categorical-variables/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="http://www.win-vector.com/site/staff/nina-zumel/">Nina Zumel</a> and <a target="_blank" rel="nofollow" href="http://www.win-vector.com/site/staff/john-mount/">I</a> have been doing a lot of writing on the (important) details of re-encoding high cardinality categorical variables for predictive modeling. These are variables that essentially take on string-values (also called levels or factors) and vary through many such levels. Typical examples include zip-codes, vendor IDs, and product codes.</p>
<p>In a sort of “burying the lede” way I feel we may not have sufficiently emphasized that you really do need to perform such re-encodings. Below is a graph (generated in <a target="_blank" rel="nofollow" href="https://cran.r-project.org/">R</a>, code available <a target="_blank" rel="nofollow" href="https://github.com/WinVector/BreakingNestedModelBias/blob/master/ShowDirect.md">here</a>) of the kind of disaster you see if you throw such variables into a model without any pre-processing or post-controls.</p>
<p><img title="NewImage.png" alt="NewImage" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2016/11/NewImage-6.png?w=660" border="0" data-recalc-dims="1"></p>
<p>In the above graph each dot represents the performance of a model fit on synthetic data. The x-axis is model performance (in this case pseudo R-squared, 1 being perfect and below zero worse than using an average). The training pane represents performance on the training data (perfect, but over-fit) and the test pane represents performance on held-out test data (an attempt to simulate future application data). Notice the test performance implies these models are dangerously worse than useless.</p>
<p>Please read on for how to fix this.<span></span></p>
<p>
<p>First: remember the disasters you see are better than those you don’t. In the synthetic data we see failure to model a relation (even though there is one, by design). But it could easily be that some column lurking in a complex model is quietly degrading model performance, without being detected by fully ruining the model.</p>
<p>The reason Nina and I have written so much on the possible side-effects of re-encoding high cardinality categorical variables is that you don’t want to introduce more problems as you attempt to fix things. Also once you intervene, by supplying advice or a solution, you feel everything will be your fault. That being said, here is our advice:</p>
<blockquote><p>Re-encode high categorical variable using impact or effects based ideas as we describe and implement in the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=vtreat">vtreat</a> R library.</p>
</blockquote>
<p>Get your data science, predictive analytics, or machine learning house in order by fixing how you are treating incoming features and data. This is where the largest opportunities for improvement are available in real-world applications. In particular:</p>
<ul>
<li>Do not ignore large cardinality categorical variables.</li>
<li>Do not blindly add large cardinality categorical variables to your model.</li>
<li>Do not <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2014/12/a-comment-on-preparing-data-for-classifiers/">hash-encode</a> large cardinality categorical variables.</li>
<li>Consider using large cardinality categorical variables as join keys to pull in columns from external data sets.</li>
</ul>
<p>Our advice: use <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=vtreat">vtreat</a>. You will more and more often going forward be competing with models that use this library or similar concepts.</p>
<p>Once you have gotten to this level of operation then worry (as we do) about the statistical details of which processing steps are justifiable, safe, useful, and best. That is the topic we have been studying and writing on in depth (we call the potential bad issues over-fitting and nested model bias). Articles include:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2016/03/more-on-preparing-data/">More on preparing data</a> (a great article on the concepts).</li>
<li><a target="_blank" rel="nofollow" href="https://github.com/WinVector/NestedModelsTalk">Model evaluation and nested models</a> (two recent talks we presented on these topics).</li>
<li>Chapters 3,4,5 and 6 of <a target="_blank" rel="nofollow" href="http://practicaldatascience.com/"><em>Practical Data Science with R</em>, (Zumel, Mount; Manning 2014)</a> (where work through examining data, fixing data problems, evaluating models, and reasoning about data columns as single variable models in disguise).</li>
<li><a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2016/11/laplace-noising-versus-simulated-out-of-sample-methods-cross-frames/">Laplace noising versus simulated out of sample methods (cross frames)</a> (where this example came from).</li>
</ul>
<p>Or invite us in to re-present one of our talks or work with your predictive analytics or data science team to adapt these techniques to your workflow, software, and problem domain. We have gotten very good results with the general methods in our vtreat library, but knowing a specific domain or problem structure can often let you do much more (for example: <a target="_blank" rel="nofollow" href="http://www.win-vector.com/blog/2016/05/pcr_part2_yaware/">Nina’s work on y-aware scaling</a> for geometric problems such as nearest neighbor classification and clustering).</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/you-should-re-encode-high-cardinality-categorical-variables/"></div></div><div><a data-url="https://www.r-bloggers.com/you-should-re-encode-high-cardinality-categorical-variables/" data-counturl="https://www.r-bloggers.com/you-should-re-encode-high-cardinality-categorical-variables/" data-count="vertical" data-text="You should re-encode high cardinality categorical variables" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2016/11/you-should-re-encode-high-cardinality-categorical-variables/"> R – Win-Vector Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/gwshyg77l0g"></a></div>