<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/update-can-we-predict-flu-outcome-with-machine-learning-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shirinsplayground.netlify.com/2018/04/flu_prediction/"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Since I migrated my blog from <a rel="nofollow" href="https://shirinsplayground.netlify.com/2018/04/flu_prediction/shiring.github.io" target="_blank">Github Pages</a> to <a rel="nofollow" href="https://shirinsplayground.netlify.com/2018/04/flu_prediction/www.shirin-glander.de" target="_blank">blogdown and Netlify</a>, I wanted to start migrating (most of) my old posts too – and use that opportunity to update them and make sure the code still works.</p>
<p>Here I am updating my very first machine learning post from 27 Nov 2016: <a rel="nofollow" href="https://shiring.github.io/machine_learning/2016/11/27/flu_outcome_ML_post" target="_blank">Can we predict flu deaths with Machine Learning and R?</a>. Changes are marked as <strong>bold</strong> comments.</p>
<p>The main changes I made are:</p>
<ul>
<li>using the <code>tidyverse</code> more consistently throughout the analysis</li>
<li>focusing on comparing multiple imputations from the <code>mice</code> package, rather than comparing different algorithms</li>
<li>using <code>purrr</code>, <code>map()</code>, <code>nest()</code> and <code>unnest()</code> to model and predict the machine learning algorithm over the different imputed datasets</li>
</ul>
<hr>
<p>Among the many nice R packages containing data collections is the <a rel="nofollow" href="https://mran.microsoft.com/web/packages/outbreaks/outbreaks.pdf" target="_blank">outbreaks</a> package. It contains a dataset on epidemics and among them is data from the 2013 outbreak of <a rel="nofollow" href="http://www.who.int/influenza/human_animal_interface/faq_H7N9/en/" target="_blank">influenza A H7N9</a> in <a rel="nofollow" href="http://www.who.int/influenza/human_animal_interface/influenza_h7n9/ChinaH7N9JointMissionReport2013u.pdf?ua=1" target="_blank">China</a> as analysed by Kucharski et al. (2014):</p>
<blockquote>
<p>A. Kucharski, H. Mills, A. Pinsent, C. Fraser, M. Van Kerkhove, C. A. Donnelly, and S. Riley. 2014. Distinguishing between reservoir exposure and human-to-human transmission for emerging pathogens using case onset data. PLOS Currents Outbreaks. Mar 7, edition 1. doi: 10.1371/currents.outbreaks.e1473d9bfc99d080ca242139a06c455f.</p>
</blockquote>
<blockquote>
<p>A. Kucharski, H. Mills, A. Pinsent, C. Fraser, M. Van Kerkhove, C. A. Donnelly, and S. Riley. 2014. Data from: Distinguishing between reservoir exposure and human-to-human transmission for emerging pathogens using case onset data. Dryad Digital Repository. <a rel="nofollow" href="http://dx.doi.org/10.5061/dryad.2g43n" target="_blank">http://dx.doi.org/10.5061/dryad.2g43n</a>.</p>
</blockquote>
<p>I will be using their data as an example to show how to use Machine Learning algorithms for predicting disease outcome.</p>
<p></p>
<pre><code>library(outbreaks)
library(tidyverse)
library(plyr)
library(mice)
library(caret)
library(purrr)</code></pre>
<div>
<h1>The data</h1>
<p>The dataset contains case ID, date of onset, date of hospitalization, date of outcome, gender, age, province and of course outcome: Death or Recovery.</p>
<div>
<h2>Pre-processing</h2>
<p><strong>Change: variable names (i.e. column names) have been renamed, dots have been replaced with underscores, letters are all lower case now.</strong></p>
<p><strong>Change: I am using the tidyverse notation more consistently.</strong></p>
<p>First, I’m doing some preprocessing, including:</p>
<ul>
<li>renaming missing data as NA</li>
<li>adding an ID column</li>
<li>setting column types</li>
<li>gathering date columns</li>
<li>changing factor names of dates (to make them look nicer in plots) and of <code>province</code> (to combine provinces with few cases)</li>
</ul>
<pre><code>fluH7N9_china_2013$age[which(fluH7N9_china_2013$age == &quot;?&quot;)] &lt;- NA
fluH7N9_china_2013_gather &lt;- fluH7N9_china_2013 %>%
  mutate(case_id = paste(&quot;case&quot;, case_id, sep = &quot;_&quot;),
         age = as.numeric(age)) %>%
  gather(Group, Date, date_of_onset:date_of_outcome) %>%
  mutate(Group = as.factor(mapvalues(Group, from = c(&quot;date_of_onset&quot;, &quot;date_of_hospitalisation&quot;, &quot;date_of_outcome&quot;), 
          to = c(&quot;date of onset&quot;, &quot;date of hospitalisation&quot;, &quot;date of outcome&quot;))),
         province = mapvalues(province, from = c(&quot;Anhui&quot;, &quot;Beijing&quot;, &quot;Fujian&quot;, &quot;Guangdong&quot;, &quot;Hebei&quot;, &quot;Henan&quot;, &quot;Hunan&quot;, &quot;Jiangxi&quot;, &quot;Shandong&quot;, &quot;Taiwan&quot;), to = rep(&quot;Other&quot;, 10)))</code></pre>
<p>I’m also</p>
<ul>
<li>adding a third gender level for unknown gender</li>
</ul>
<pre><code>levels(fluH7N9_china_2013_gather$gender) &lt;- c(levels(fluH7N9_china_2013_gather$gender), &quot;unknown&quot;)
fluH7N9_china_2013_gather$gender[is.na(fluH7N9_china_2013_gather$gender)] &lt;- &quot;unknown&quot;
head(fluH7N9_china_2013_gather)</code></pre>
<pre><code>##   case_id outcome gender age province         Group       Date
## 1  case_1   Death      m  58 Shanghai date of onset 2013-02-19
## 2  case_2   Death      m   7 Shanghai date of onset 2013-02-27
## 3  case_3   Death      f  11    Other date of onset 2013-03-09
## 4  case_4    </code></pre></div></div>