<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/missing-values-data-science-and-r/"></div></div><div><a data-url="https://www.r-bloggers.com/missing-values-data-science-and-r/" data-counturl="https://www.r-bloggers.com/missing-values-data-science-and-r/" data-count="horizontal" data-text="Missing Values, Data Science and R" 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="https://www.rstudio.com/rviews/2016/11/30/missing-values-data-science-and-r/"> RStudio</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>

<p>by Joseph Rickert</p>
<p>One great advantages of working in R is the quantity and sophistication of the statistical functions and techniques available. For example, R’s <code>quantile()</code> function allows you to select one of the nine <a target="_blank" rel="nofollow" href="https://www.amherst.edu/media/view/129116/original/Sample+Quantiles.pdf">different methods</a> for computing quantiles. Who would have thought there could be so many ways to do something that seems to be so simple? The issue here is not unnecessary complication, but rather an appreciation of the nuances associated with inference problems gained over the last hundred years of modern statistical practice. Much of this knowledge is reflected in R. As I imagine it, if R’s functions were laid out like city streets there would be many broad avenues displaying commonly used algorithms, but these would flow into many thousands of small, rarely-visited alleys, containing treasures for those who seek them out.</p>
<p>My impression is that most data science is done on the avenues, but that data scientists could benefit from occasionally venturing deeper into the alleys of statistical practice. <a target="_blank" rel="nofollow" href="http://andrewgelman.com/2013/11/14/statistics-least-important-part-data-science/">Statistics might be the least important part of data science</a> but there are times when coming to grips with sound statistical inference is essential. Consider the problem of dealing with missing values for example. It is difficult to imagine any large, real-world data set that wouldn’t require a strategy for imputing missing values. The obvious first step in developing a strategy would be to form some ideas about why the data are missing. <a target="_blank" rel="nofollow" href="http://www.stat.columbia.edu/~gelman/arm/missing.pdf">Gelman and Hill</a> identify four different “missingness mechanisms”: (1) Missingness completely at random, (2) Missingness at random, (3) Missingness that depends on unobserved predictors, (4) Missingness that depends on the missing value itself; and they provide some advice on how to cope with each of them. In a large data set, there is no reason to believe that there is just one mechanism in play! Evaluating your data with respect to these categories and their combinations could require a frightening amount of exploratory work. So tools for looking at patterns in missing values are likely to be very helpful, even if using them requires sampling your data set.</p>
<p>The next step of deciding how to proceed in a statistically sound manner will likely pose considerable technical challenges, and opting for simple solutions may not be advisable, or even possible. Even with big data, ignoring observations with missing values could result in a catastrophic loss of information, and simple approaches to imputation such as replacing missing the missing values of each variable with the variable mean, or a common value, will produce a “completed” data set reflecting an artificial amount of certainty that will likely underestimate the variability of the data and bias results. R can’t eliminate the hard work, but it does provide a formidable array of missing value imputation tools that can get you in and out of the statistical alleys and help you to decide what techniques might be useful.</p>
<p>Before evaluating the various methods, it is helpful to make a couple of distinctions about imputation methods for multivariate data. The first is between single and multiple imputation. In single imputation, a particular algorithm or technique is used to produce a single estimate for each missing value. Multiple imputation methods, on the other hand, develop distributions for missing values and estimate individual missing values by drawing from this distribution. In general, these algorithms proceed in three steps. In the first imputation step, multiple draws are made from the distribution of missing values for each variable. This process results in several “completed” data sets where each missing value has been estimated by a plausible value. In the second step, the intended analysis is performed on each completed data set. In the last step, a “pooling”algorithm estimates the final values for the statistics of interest.</p>
<p>The <a target="_blank" rel="nofollow" href="http://smm.sagepub.com/content/16/3/219.abstract">second distinction</a> is between Joint Modeling (JM) and Fully Conditional Specification (FCS) imputing. In JM, the data are assumed to follow a multivariate parametric distribution of some sort. This is theoretically sound but may not be flexible enough to adequately model the data. In FCS, the multivariate data model is specified by developing a separate conditional model for each variable with missing values.</p>
<p>Here is a short list of R packages for missing value imputation. I have selected these to give some idea of the variety of tools available.</p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/Amelia/">Amelia</a> implements the <a target="_blank" rel="nofollow" href="http://gking.harvard.edu/amelia">Amelia II</a> algorithm which assumes that the complete data set (missing and observed data) are multivariate normal. Imputations are done via the EMB (expectation-maximization with bootstrapping) algorithm. The <a target="_blank" rel="nofollow" href="https://www.jstatsoft.org/article/view/v045i07">JSS paper</a> describes a strategy for combining the models resulting from each imputed data set. The <a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/web/packages/Amelia/vignettes/amelia.pdf">Amelia vignette</a> contains examples.</p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/BaBooN/">BaBoon</a> provides two variants of the the <a target="_blank" rel="nofollow" href="https://projecteuclid.org/euclid.aos/1176345338">Bayesian Bootstrap</a> predictive mean matching to impute multiple missing values. Originally developed for survey data, the imputation algorithms are described as being robust with respect to imputation model misspecification. The best description and rationale for the algorithms seems to be the <a target="_blank" rel="nofollow" href="https://opus4.kobv.de/opus4-bamberg/frontdoor/deliver/index/docId/200/file/thesiskollermeinfelderaop.pdf">PhD thesis</a> of one of the package authors.</p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/Hmisc/">Hmisc</a> contains several functions that are helpful for missing value imputation including <code>agreImpute()</code>, <code>impute()</code> and <code>transcan()</code>. Documentation on Hmisc can be found <a target="_blank" rel="nofollow" href="http://biostat.mc.vanderbilt.edu/wiki/Main/Hmisc">here</a>.</p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/mi/">mi</a> takes a Bayesian approach to imputing missing values. The imputation algorithm runs multiple MCMC chains to iteratively draw imputed values from conditional distributions of observed and imputed data. In addition to imputation algorithm, the package contains functions for visualizing the pattern of missing values in a data set and assessing the convergence of the MCMC chains. A <a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/web/packages/mi/vignettes/mi_vignette.pdf">vignette</a> shows a worked example and the associated <a target="_blank" rel="nofollow" href="https://www.jstatsoft.org/article/view/v045i02">JSS paper</a> delves deeper into the theory and the mechanics of using the method.</p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/mice/">mice</a> which is an acronym for multivariate imputation of chained equations, formalizes the multiple implementation process outline above and is probably the gold standard for FCS multiple imputation. Package features include:</p>
<ul>
<li>Columnwise specification of the imputation model</li>
<li>Support for arbitrary patterns of missing data</li>
<li>Passive imputation techniques that maintain consistency among data transformations</li>
<li>Subset selection of predictors</li>
<li>Support of arbitrary complete-data methods</li>
<li>Support pooling various types of statistics</li>
<li>Diagnostics for imputations</li>
<li>Callable user-written imputation functions</li>
</ul>
<p>The <a target="_blank" rel="nofollow" href="https://www.jstatsoft.org/article/view/v045i03">JSS paper</a> describes how the desire to provide a separate imputation model for each variable led to the development of the chained equation technique where a Gibbs sampler fills out the missing data. <a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/miceadds/">miceAdds</a> provides additional functions to be used with mice including plausible value imputation, multilevel imputation functions, imputation using partial least squares (PLS) for high dimensional predictors, nested multiple imputation, and two-way imputation.</p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/MissingDataGUI/">missingDataGUI</a> implements a nice graphical interface for exploring missing data patterns with numeric and graphical summaries for numeric and categorical missing values and implements a number of imputation methods. The figure below shows the missing value map for the HouseVotes84 data set in the mlbench package.</p>
<p><img src="https://i0.wp.com/www.rstudio.com/wp-content/uploads/2016/11/missing_values1.png?w=456" data-recalc-dims="1"></p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/missMDA/">missMDA</a> performs principal component methods on incomplete data sets obtaining scores, loadings and graphical representations despite missing values. The package also includes functions to perform single and multiple imputation. The <a target="_blank" rel="nofollow" href="https://www.jstatsoft.org/article/view/v070i01">JSS paper</a> provides the details.</p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/VIM/">VIM</a> provides tools for visualizing missing or imputed values. Before imputation they can be used to study the pattern of missing values, afterwards these same tools can be used as diagnostics. <a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/VIMGUI/">VIMGUI</a> puts a front end on the VIM functions and helps with handling the plot and imputation functions. The <a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/web/packages/VIMGUI/vignettes/VIM-Imputation.pdf">vignette</a> is thorough and provides some nice examples of how one might look at missing value distributions.</p>
<p><img src="https://i2.wp.com/www.rstudio.com/wp-content/uploads/2016/11/missing_values2.png?w=456" data-recalc-dims="1"></p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/vtreat/">vtreat</a> provides tools to assist in the statistically sound preparation of data sets. It is not a package explicitly devoted to missing value imputation, but it can produce “cleaned” data sets that have no “Infinite/NA/NaN in the effective variable columns”. I include it here to emphasize that proper data preparation can simplify the missing value problem. The package has several vignettes.</p>
<p><a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/yaImpute/">yaImpute</a> takes what might be thought of as a machine learning approach to imputing missing values by using the k-nearest neighbor (kNN) algorithm to impute missing values. The <a target="_blank" rel="nofollow" href="https://www.jstatsoft.org/article/view/v023i10">JSS paper</a> covers the theory and explains the package using a forestry application.</p>
<p>For additional R packages see Stef van Buuren’s <a target="_blank" rel="nofollow" href="http://stefvanbuuren.nl/mi/software.html">webpage cataloging software for imputation</a> which lists twelve R packages that implement some method of single imputation and eighteen R packages concerned with multiple imputation and provides a brief explanation of each of these. Also, have a look on the <a target="_blank" rel="nofollow" href="https://www.analyticsvidhya.com/blog/2016/03/tutorial-powerful-packages-imputing-missing-values/">post on analyticsvidha.com</a> that provides informative short write ups on amelia, Hmisc, mi, mice and <a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/package/missForest/">missForest</a> that include some sample code.</p>
<p>I also recommend looking at <a target="_blank" rel="nofollow" href="http://missdata2015.agrocampus-ouest.fr/infoglueDeliverLive/digitalAssets/79082_Rennes_2015_-_Stef_van_Buuren.pdf">Stef van Buuren presentation</a> on the history and future of Fully Conditional Specification from the 2015 Rennes missing value conference and Julie Josse’s <a target="_blank" rel="nofollow" href="http://juliejosse.com/?p=96">tutorial at useR! 2016</a>.</p>
<p>Even with the best of tools there is no doubt that dealing with missing values in a statistically sound manner is difficult. However, this is the that kind of work that helps to put the “science” in data science, and R is the most helpful environment you are likely to find.</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/missing-values-data-science-and-r/"></div></div><div><a data-url="https://www.r-bloggers.com/missing-values-data-science-and-r/" data-counturl="https://www.r-bloggers.com/missing-values-data-science-and-r/" data-count="vertical" data-text="Missing Values, Data Science and R" 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="https://www.rstudio.com/rviews/2016/11/30/missing-values-data-science-and-r/"> RStudio</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/FBQL7cO4SD4"></a></div>