<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/handling-missing-data-with-mice-package-a-simple-approach/"></div></div><div><a data-url="http://www.r-bloggers.com/handling-missing-data-with-mice-package-a-simple-approach/" data-counturl="http://www.r-bloggers.com/handling-missing-data-with-mice-package-a-simple-approach/" data-count="horizontal" data-text="Handling missing data with MICE package; a simple approach" 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://datascienceplus.com/handling-missing-data-with-mice-package-a-simple-approach/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This is a quick, short and concise tutorial on how to impute missing data. Previously, we have published <a target="_blank" href="http://datascienceplus.com/imputing-missing-data-with-r-mice-package/">an extensive tutorial on imputing missing values</a> with MICE package. Current tutorial aim to be simple and user friendly for those who just starting using R.</p>
<h2>Preparing the dataset</h2>
<p>I have created a simulated dataset, which you can load on your R environment by using the following code.</p>
<pre>dat &lt;- read.csv(url(&quot;http://goo.gl/19NKXV&quot;), header=TRUE, sep=&quot;,&quot;)</pre>
<p>Let’s see the header of dataset.</p>
<pre>head(dat)<em>
## Age Gender Cholesterol SystolicBP BMI Smoking Education
## 1 67.9 Female 236.4 129.8 26.4 Yes High
## 2 54.8 Female 256.3 133.4 28.4 No Medium
## 3 68.4 Male 198.7 158.5 24.1 Yes High
## 4 67.9 Male 205.0 136.0 19.9 No Low
## 5 60.9 Male 207.7 145.4 26.7 No Medium
## 6 44.9 Female 222.5 130.6 30.6 No Low</em></pre>
<p>Check the data for missing values.</p>
<pre>sapply(dat, function(x) sum(is.na(x)))<em>
## Age Gender Cholesterol SystolicBP BMI Smoking ## 0 0 0 0 0 0 ## Education ## 0</em></pre>
<p>Since there are no missings, I will add some <code>NA</code> in dataset, but before I will duplicate original dataset to evaluate the accuracy of imputation later.</p>
<pre>original &lt;- dat</pre>
<p>Now I will add some missings in few variables.</p>
<pre>set.seed(10)
dat[sample(1:nrow(dat), 20), &quot;Cholesterol&quot;] &lt;- NA
dat[sample(1:nrow(dat), 20), &quot;Smoking&quot;] &lt;- NA
dat[sample(1:nrow(dat), 20), &quot;Education&quot;] &lt;- NA
dat[sample(1:nrow(dat), 5), &quot;Age&quot;] &lt;- NA
dat[sample(1:nrow(dat), 5), &quot;BMI&quot;] &lt;- NA</pre>
<p>Confirm the presence of missings in the dataset.</p>
<pre>sapply(dat, function(x) sum(is.na(x)))<em>
## Age Gender Cholesterol SystolicBP BMI Smoking ## 5 0 20 0 5 20 ## Education ## 20</em></pre>
<p>Next step is to transform the variables in factors or numeric. For example, smoking and education are categorical variables, whereas cholesterol level is continuous.</p>
<pre>library(dplyr) 
dat &lt;- dat %>%
    mutate(Smoking = as.factor(Smoking)) %>% 
    mutate(Education = as.factor(Education)) %>% 
    mutate(Cholesterol = as.numeric(Cholesterol))</pre>
<p>Look the dataset structure.</p>
<pre>str(dat)<em>
## 'data.frame': 250 obs. of 7 variables:
## $ Age : num 67.9 54.8 68.4 67.9 60.9 44.9 49.9 NA 57.5 77.2 ...
## $ Gender : Factor w/ 2 levels &quot;Female&quot;,&quot;Male&quot;: 1 1 2 2 2 1 2 1 2 2 ...
## $ Cholesterol: num 236 256 199 205 208 ...
## $ SystolicBP : num 130 133 158 136 145 ...
## $ BMI : num 26.4 28.4 24.1 19.9 26.7 30.6 27.3 27.5 28.3 29.1 ...
## $ Smoking : Factor w/ 2 levels &quot;No&quot;,&quot;Yes&quot;: 2 1 2 1 1 1 1 1 1 1 ...
## $ Education : Factor w/ 3 levels &quot;High&quot;,&quot;Low&quot;,&quot;Medium&quot;: 1 3 1 NA NA 2 3 2 1 1 ...</em></pre>
<p>Everything looks OK, so lets proceed with imputation.</p>
<h2>Imputation</h2>
<p>Now that the dataset is ready for imputation, we will call the mice package. The code below is standard and you dont need to change anything besides the dataset name.</p>
<pre>library(mice)
init = mice(dat, maxit=0) 
meth = init$method
predM = init$predictorMatrix</pre>
<p>To impute the missing values, mice package use an algorithm in a such a way that use information from other variables in dataset to predict and impute the missing values. Therefore, you may not want to use certain variable as predictors. For example the ID variable does not have any predictive value.</p>
<p>The code below will remove the variable as predictor but still will be imputed. Just for illustration purposes I select the BMI variable to not be included as predictor during imputation.</p>
<pre>predM=0</pre>
<p>If you want to skip a variable from imputation use the code below. Keep in mind that this variable will be used for prediction.</p>
<pre>meth=&quot;&quot;</pre>
<p>Now let specify the methods for imputing the missing values. There are specific methods for continues, binary and ordinal variables. I set different methods for each variable. You can add more than one variable in each methods.</p>
<pre>meth=&quot;norm&quot; 
meth=&quot;logreg&quot; 
meth=&quot;polyreg&quot;</pre>
<p>Now it is time to run the multiple (m=5) imputation.</p>
<pre>set.seed(103)
imputed = mice(dat, method=meth, predictorMatrix=predM, m=5)<em>
## ## iter imp variable
## 1 1 Cholesterol BMI Smoking Education
## 1 2 Cholesterol BMI Smoking Education
## 1 3 Cholesterol BMI Smoking Education
## 1 4 Cholesterol BMI Smoking Education
## 1 5 Cholesterol BMI Smoking Education
## 2 1 Cholesterol BMI Smoking Education
## 2 2 Cholesterol BMI Smoking Education
...</em></pre>
<p>Create a dataset after imputation.</p>
<pre>imputed &lt;- complete(imputed)</pre>
<p>Check for missings in the imputed dataset.</p>
<pre>sapply(imputed, function(x) sum(is.na(x)))<em>
## Age Gender Cholesterol SystolicBP BMI Smoking ## 5 0 0 0 0 0 ## Education ## 0</em></pre>
<h2>Accuracy</h2>
<p>In this example, we know the actual values of missing data, since I added the missings. This indicate that we can check the accuracy of the imputation. However, we should acknowledge that this is an simulated dataset, and therefore, variables have no scientific meanings and are not correlated to each other. Therefore I expect a lower rate of accuracy for this imputation.</p>
<pre># Cholesterol
actual &lt;- original$Cholesterol[is.na(dat$Cholesterol)]
predicted &lt;- imputed$Cholesterol[is.na(dat$Cholesterol)]
mean(actual)<em>
## [1] 231.07</em>
mean(predicted)<em>
## [1] 231.3564</em>

# Smoking
actual &lt;- original$Smoking[is.na(dat$Smoking)] 
predicted &lt;- imputed$Smoking[is.na(dat$Smoking)] 
table(actuals)<em>
## actual
## No Yes
## 11 9</em>
table(predicted)<em>
## predicted
## No Yes
## 14 6</em>
</pre>
<p>The mean of actual and predicted for Cholesterol is almost identical, which shows a high accuracy of imputation, whereas for smoking is low.</p>
<p>That's it, I hope you find this tutorial useful. If you have any question feel free to comment below.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toBest packages for data manipulation in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/best-packages-for-data-manipulation-in-r/">Best packages for data manipulation in R</a></li>
<li><a title="Permanent Link toIdentify, describe, plot, and remove the outliers from the dataset" target="_blank" rel="bookmark" href="http://datascienceplus.com/identify-describe-plot-and-removing-the-outliers-from-the-dataset/">Identify, describe, plot, and remove the outliers from the dataset</a></li>
<li><a title="Permanent Link toLearn R By Intensive Practice – Part 2" target="_blank" rel="bookmark" href="http://datascienceplus.com/learn-r-by-intensive-practice-part-2/">Learn R By Intensive Practice – Part 2</a></li>
<li><a title="Permanent Link toWorking with databases in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/working-with-databases-in-r/">Working with databases in R</a></li>
<li><a title="Permanent Link toData manipulation with tidyr" target="_blank" rel="bookmark" href="http://datascienceplus.com/data-manipulation-with-tidyr/">Data manipulation with tidyr</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=mxxnHOjZ4B0:UdsVvPKOiOA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=mxxnHOjZ4B0:UdsVvPKOiOA:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=mxxnHOjZ4B0:UdsVvPKOiOA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=mxxnHOjZ4B0:UdsVvPKOiOA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/handling-missing-data-with-mice-package-a-simple-approach/"></div></div><div><a data-url="http://www.r-bloggers.com/handling-missing-data-with-mice-package-a-simple-approach/" data-counturl="http://www.r-bloggers.com/handling-missing-data-with-mice-package-a-simple-approach/" data-count="vertical" data-text="Handling missing data with MICE package; a simple approach" 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://datascienceplus.com/handling-missing-data-with-mice-package-a-simple-approach/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/0zgaRHqqkXk"></a></div>