<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/graphical-presentation-of-missing-data-vim-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/graphical-presentation-of-missing-data-vim-package/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Missing data is a problem that challenge data analysis methodologically and computationally in medical research. Patients of the clinical trials and cohort studies may drop out of the study, and therefore, generate missing data. The missing data could be at random when participants who drop out of study are not different from those who remained in study. For example, in the study of body mass index and cholesterol levels, participants who don’t measure their blood cholesterol have a comparable body mass index with participants who measure their blood cholesterol. </p>
<p>To handle missing data researchers often choose to conduct analysis among participants without missing data (i.e., complete case analysis), but sometimes they prefer to impute the data. In the previous tutorials (<a target="_blank" rel="nofollow" href="https://datascienceplus.com/imputing-missing-data-with-r-mice-package/">Tutorial 1</a>, <a target="_blank" rel="nofollow" href="https://datascienceplus.com/handling-missing-data-with-mice-package-a-simple-approach/">Tutorial 2</a>) published at DataScience+ we have shown how to impute the missing data by using MICE package. In this tutorial I will show how to graphically present the missing data, with only one purpose, to find whether missing is at random. Therefore, we will build plots by using the function <code>marginplot</code> from <code>VIM</code> package. This would be a short “how to” tutorial and have no intention to explain types of the missing data. You can learn more about types of missing data such as missing completely at random, missing at random, and not missing at random from the book <a target="_blank" rel="nofollow" href="http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471183865.html">Statistical Analysis with Missing Data</a>.</p>
<h2>Data Preparation</h2>
<p>I simulated a database with 250 observations for illustration purpose and there is no clinical relevance. There are five variables Age, Gender, Cholesterol, SystolicBP, BMI, Smoking, and Education. </p>
<p>Load the libraries and get the data by running the scrip below:</p>
<pre>
library(VIM)
library(mice)
library(dplyr)
library(tibble)
dat &lt;- read.csv(url(&quot;https://goo.gl/4DYzru&quot;), header=TRUE, sep=&quot;,&quot;)
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>In this database there are no missing. I will introduce missing not at random for the cholesterol variable. As it shown in the code below, missing values for cholesterol will include only participants with body mass index levels greater than 30 (i.e., participants with obesity).</p>
<pre>set.seed(10)
missing = rbinom(250, 1, 0.3)
dat$Cholesterol = with(dat, ifelse(BMI>=30&amp;missing==1, NA, Cholesterol))
sum(is.na(dat$Cholesterol))
<em>[1] 16</em></pre>
<p>We have 16 participants with missing values in cholesterol variable. I am going to <a target="_blank" rel="nofollow" href="https://datascienceplus.com/handling-missing-data-with-mice-package-a-simple-approach/">impute the missing values</a> by using MICE package and PMM (predictive mean matching) method.</p>
<pre>
init = mice(dat, maxit=0) 
meth = init$method
predM = init$predictorMatrix
meth[c(&quot;Cholesterol&quot;)]=&quot;pmm&quot; 
set.seed(101)
imputed = mice(dat, method=meth, predictorMatrix=predM, m=1)
imp = complete(imputed)</pre>
<p>Next, I will create a database with imputed data and an indicator variable which shows which observations are imputed. This is necessary for plotting by using <code>marginplot</code> function.</p>
<pre>dt1 = dat %>% 
  select(Cholesterol, BMI) %>% 
  rename(Cholesterol_imp = Cholesterol) %>% 
  mutate(
    Cholesterol_imp = as.logical(ifelse(is.na(Cholesterol_imp), &quot;TRUE&quot;, &quot;FALSE&quot;))
  ) %>% 
  rownames_to_column()

dt2 = imp %>% 
  select(Cholesterol, BMI) %>% 
  rownames_to_column()

dt = left_join(dt1, dt2)
head(dt)
<em>rowname Cholesterol_imp BMI Cholesterol
1 1 FALSE 26.4 236.4
2 2 FALSE 28.4 256.3
3 3 FALSE 24.1 198.7
4 4 FALSE 19.9 205.0
5 5 FALSE 26.7 207.7
6 6 FALSE 30.6 222.5</em></pre>
<h2>Graphical presentation of missing data</h2>
<p>Now that we have a database with the imputed variables and the corresponding indicator, whether the observation is imputed or not, we will plot it by using the function <code>marginplot</code> from VIM package. </p>
<pre>vars &lt;- c(&quot;BMI&quot;,&quot;Cholesterol&quot;,&quot;Cholesterol_imp&quot;)
marginplot(dt[,vars], delimiter=&quot;_imp&quot;, alpha=0.6, pch=c(19))</pre>
<p>This is the output of code above:<br>
<a title="" data-rel="lightbox-0" target="_blank" rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/04/marginplot1.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-6386" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/04/marginplot1-490x451.png?w=450&ssl=1" data-recalc-dims="1"></a></p>
<p>The blue color in the scatterplot above indicate the non-missing values for the cholesterol, and the orange shows the missing data which is imputed. As we can see participants with missing data in cholesterol have a higher body mass index that participants without missing data which indicate that missing is not at random. This scatterplot also shows the boxplot which ilustrate the distribution of data. For example the median of body mass index for participants with missing data is round 33, whereas for those without missing is round 26. </p>
<p>A missing at random will look like in the plot below<br>
<a title="" data-rel="lightbox-1" target="_blank" rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/04/marginplot2.png?ssl=1"><img sizes="(max-width: 490px) 100vw, 490px" alt="" class="wp-image-6387" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/04/marginplot2-490x451.png?w=450&ssl=1" data-recalc-dims="1"></a></p>
<h2>Conclusion</h2>
<p>In this post, we showed how to use <code>marginplot</code> of VIM package to identify whether your missing data is at random. To learn more about VIM package I suggest to read the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/VIMGUI/vignettes/VIM-Imputation.pdf">paper by Bernd Prantner</a>.</p>
<p>I hope you find this post useful. Leave comments below if you have questions.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toCreating Graphs with Python and GooPyCharts" target="_blank" rel="nofollow" href="https://datascienceplus.com/creating-graphs-with-python-and-goopycharts/">Creating Graphs with Python and GooPyCharts</a></li>
<li><a title="Permanent Link toVisualizing Tennis Grand Slam Winners Performances" target="_blank" rel="nofollow" href="https://datascienceplus.com/visualizing-tennis-grand-slam-winners-performances/">Visualizing Tennis Grand Slam Winners Performances</a></li>
<li><a title="Permanent Link toPlotting Data Online via Plotly and Python" target="_blank" rel="nofollow" href="https://datascienceplus.com/plotting-data-online-via-plotly-and-python/">Plotting Data Online via Plotly and Python</a></li>
<li><a title="Permanent Link toUsing PostgreSQL and shiny with a dynamic leaflet map: monitoring trash cans" target="_blank" rel="nofollow" href="https://datascienceplus.com/using-postgresql-and-shiny-with-a-dynamic-leaflet-map-monitoring-trash-cans/">Using PostgreSQL and shiny with a dynamic leaflet map: monitoring trash cans</a></li>
<li><a title="Permanent Link toVisualizing Streaming Data And Alert Notification with Shiny" target="_blank" rel="nofollow" href="https://datascienceplus.com/visualizing-streaming-data-with-shiny/">Visualizing Streaming Data And Alert Notification with Shiny</a></li>
</ol></strong>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/graphical-presentation-of-missing-data-vim-package/"> R Programming – DataScience+</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/YJxyVVFpiNU"></a></div>