<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/machine-learning-for-drug-adverse-event-discovery/"></div></div><div><a data-url="https://www.r-bloggers.com/machine-learning-for-drug-adverse-event-discovery/" data-counturl="https://www.r-bloggers.com/machine-learning-for-drug-adverse-event-discovery/" data-count="horizontal" data-text="Machine Learning for Drug Adverse Event Discovery" 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/machine-learning-for-drug-adverse-event-discovery/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We can use unsupervised machine learning to identify which drugs are associated with which adverse events. Specifically, machine learning can help us to create clusters based on gender, age, outcome of adverse event, route drug was administered, purpose the drug was used for, body mass index, etc. This can help for quickly discovering hidden associations between drugs and adverse events.</p>
<p>Clustering is a non-supervised learning technique which has wide applications. Some examples where clustering is commonly applied are market segmentation, social network analytics, and astronomical data analysis. Clustering is grouping of data into sub-groups so that objects within a cluster have high similarity in comparison to other objects in that cluster, but are very dissimilar to objects in other classes. For clustering, each pattern is represented as a vector in multidimensional space and a distance measure is used to find the dissimilarity between the instances. In this post, we will see how we can use hierarchical clustering to identify drug adverse events. You can read about hierarchical clustering from <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Hierarchical_clustering">Wikipedia</a>.</p>
<h2>Data</h2>
<p>Let’s create fake drug adverse event data where we can visually identify the clusters and see if our machine learning algorithm can identify the clusters. If we have millions of rows of adverse event data, clustering can help us to summarize the data and get insights quickly.</p>
<p>Let’s assume a drug AAA results in adverse events shown below. We will see in which group (cluster) the drug results in what kind of reactions (adverse events).<br>
In the table shown below, I have created four clusters: </p>
<blockquote><ul>
<li>Route=ORAL, Age=60s, Sex=M, Outcome code=OT, Indication=RHEUMATOID ARTHRITIS and Reaction=VASCULITIC RASH + some noise</li>
<li>Route=TOPICAL, Age=early 20s, Sex=F, Outcome code=HO, Indication=URINARY TRACT INFECTION and Reaction=VOMITING + some noise</li>
<li>Route=INTRAVENOUS, Age=about 5, Sex=F, Outcome code=LT, Indication=TONSILLITIS and Reaction=VOMITING + some noise</li>
<li>Route=OPHTHALMIC, Age=early 50s, Sex=F, Outcome code=DE, Indication=Senile osteoporosis and Reaction=Sepsis + some noise</li>
</ul>
</blockquote>
<p>Below is a preview of my data. You can download the data <a target="_blank" href="http://datascienceplus.com/wp-content/uploads/2016/09/my_data.csv">here</a></p>
<pre>
head(my_data)<em>
route age sex outc_cod indi_pt pt
1 ORAL 63 M OT RHEUMATOID ARTHRITIS VASCULITIC RASH
2 ORAL 66 F OT RHEUMATOID ARTHRITIS VASCULITIC RASH
3 ORAL 66 M OT RHEUMATOID ARTHRITIS VASCULITIC RASH
4 ORAL 57 M OT RHEUMATOID ARTHRITIS VASCULITIC RASH
5 ORAL 66 M OT RHEUMATOID ARTHRITIS VASCULITIC RASH
6 ORAL 66 M OT RHEUMATOID ARTHRITIS VASCULITIC RASH
</em></pre>
<h2>Hierarchical Clustering</h2>
<p>To perform hierarchical clustering, we need to change the text to numeric values so that we can calculate distances. Since age is numeric, we will remove it from the rest of the variables and change the character variables to multidimensional numeric space.</p>
<pre>
age = my_data$age
my_data = select(my_data,-age)
</pre>
<h3>Create a Matrix</h3>
<pre>my_matrix = as.data.frame(do.call(cbind, lapply(mydata, function(x) table(1:nrow(mydata), x))))</pre>
<p>Now, we can add the age column:</p>
<pre>my_matrix$Age=age
head(my_matrix)<em>
INTRAVENOUS OPHTHALMIC ORAL TOPICAL F M DE HO LT OT RHEUMATOID ARTHRITIS Senile osteoporosis
1 0 0 1 0 0 1 0 0 0 1 1 0
2 0 0 1 0 1 0 0 0 0 1 1 0
3 0 0 1 0 0 1 0 0 0 1 1 0
4 0 0 1 0 0 1 0 0 0 1 1 0
5 0 0 1 0 0 1 0 0 0 1 1 0
6 0 0 1 0 0 1 0 0 0 1 1 0
TONSILLITIS URINARY TRACT INFECTION Sepsis VASCULITIC RASH VOMITING Age
1 0 0 0 1 0 63
2 0 0 0 1 0 66
3 0 0 0 1 0 66
4 0 0 0 1 0 57
5 0 0 0 1 0 66
6 0 0 0 1 0 66</em></pre>
<p>Let’s normalize our variables using <em>caret package</em>.</p>
<pre>library(caret)
preproc = preProcess(my_matrix)
my_matrixNorm = as.matrix(predict(preproc, my_matrix))</pre>
<p>Next, let’s calculate distance and apply hierarchical clustering and plot the dendrogram.</p>
<pre>distances = dist(my_matrixNorm, method = &quot;euclidean&quot;)
clusterdrug = hclust(distances, method = &quot;ward.D&quot;) 
plot(clusterdrug, cex=0.5, labels = FALSE,cex=0.5,xlab = &quot;&quot;, sub = &quot;&quot;,cex=1.2)</pre>
<p>You will get this plot:<br>
<a title="" data-rel="lightbox-0" target="_blank" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/09/fig1.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="fig1" class="wp-image-4710" src="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/09/fig1-490x307.png?resize=456%2C286" data-recalc-dims="1"></a></p>
<p>From the dendrogram shown above, we see that four distinct clusters can be created from the fake data we created. Let’s use different colors to identify the four clusters.</p>
<pre>
library(dendextend)
dend &lt;- as.dendrogram(clusterdrug) 
install.packages(&quot;dendextend&quot;) 
library(dendextend)
# Color the branches based on the clusters:
dend &lt;- color_branches(dend, k=4) #, groupLabels=iris_species)
# We hang the dendrogram a bit:
dend &lt;- hang.dendrogram(dend,hang_height=0.1)
# reduce the size of the labels:
# dend &lt;- assign_values_to_leaves_nodePar(dend, 0.5, &quot;lab.cex&quot;)
dend &lt;- set(dend, &quot;labels_cex&quot;, 0.5)
plot(dend)</pre>
<p>Here is the plot:<br>
<a title="" data-rel="lightbox-1" target="_blank" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/09/fig2.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="fig2" class="wp-image-4711" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/09/fig2-490x294.png?resize=456%2C274" data-recalc-dims="1"></a></p>
<p>Now, let’s create cluster groups with four clusters.</p>
<pre>clusterGroups = cutree(clusterdrug, k = 4)</pre>
<p>Now, let’s add the clusterGroups column to the original data.</p>
<pre>my_data= cbind(data.frame(Cluster=clusterGroups), my_data, age)
head(my_data)<em>
Cluster route sex outc_cod indi_pt pt age
1 1 ORAL M OT RHEUMATOID ARTHRITIS VASCULITIC RASH 63
2 1 ORAL F OT RHEUMATOID ARTHRITIS VASCULITIC RASH 66
3 1 ORAL M OT RHEUMATOID ARTHRITIS VASCULITIC RASH 66
4 1 ORAL M OT RHEUMATOID ARTHRITIS VASCULITIC RASH 57
5 1 ORAL M OT RHEUMATOID ARTHRITIS VASCULITIC RASH 66
6 1 ORAL M OT RHEUMATOID ARTHRITIS VASCULITIC RASH 66</em></pre>
<h3>Number of Observations in Each Cluster</h3>
<pre>observationsH=c()
for (i in seq(1,4)){
  observationsH=c(observationsH,length(subset(clusterdrug, clusterGroups==i)))
}
observationsH =as.data.frame(list(cluster=c(1:4),Number_of_observations=observationsH))
observationsH <em>
cluster Number_of_observations
1 1 20
2 2 13
3 3 15
4 4 24</em></pre>
<h3>What is the most common observation in each cluster?</h3>
<p>Let’s calculate column average for each cluster.</p>
<pre>z=do.call(cbind,lapply(1:4, function(i) round(colMeans(subset(my_matrix,clusterGroups==i)),2)))
colnames(z)=paste0('cluster',seq(1,4))
z<em>
cluster1 cluster2 cluster3 cluster4
INTRAVENOUS 0.00 0.00 1.00 0.00
OPHTHALMIC 0.00 0.00 0.00 0.92
ORAL 1.00 0.08 0.00 0.08
TOPICAL 0.00 0.92 0.00 0.00
F 0.10 0.85 0.80 1.00
M 0.90 0.15 0.20 0.00
DE 0.00 0.00 0.00 0.83
.....</em></pre>
<p>Next, most common observation in each cluster:</p>
<pre>Age=z[nrow(z),]
z=z[1:(nrow(z)-1),]
my_result=matrix(0,ncol=4,nrow=ncol(mydata))
for(i in seq(1,4)){
    for(j in seq(1,ncol(mydata))){
q = names(mydata)[j]
q = as.vector(as.matrix(unique(mydata[q])))
my_result[j,i]=names(sort(z[q,i],decreasing = TRUE)[1])
    }}

colnames(my_result)=paste0('Cluster',seq(1,4))
rownames(my_result)=names(mydata)
my_result=rbind(Age,my_result)
my_result &lt;- cbind(Attribute =c(&quot;Age&quot;,&quot;Route&quot;,&quot;Sex&quot;,&quot;Outcome Code&quot;,&quot;Indication preferred term&quot;,&quot;Adverse event&quot;), my_result)
rownames(my_result) &lt;- NULL
my_result
<em>
Attribute	cluster1	cluster2	cluster3	cluster4
Age	61.8	17.54	5.8	44.62
Route	ORAL	TOPICAL	INTRAVENOUS	OPHTHALMIC
Sex	M	F	F	F
Outcome Code	OT	HO	LT	DE
Indication preferred term	RHEUMATOID ARTHRITIS URINARY TRACT INFECTION	TONSILLITIS Senile osteoporosis
Adverse event VASCULITIC RASH	VOMITING	VOMITING	Sepsis</em>

</pre>
<h2>Summary</h2>
<p>We see that we have created the clusters using hierarchical clustering. From cluster 1, for male in the 60s, the drug results in vasculitic rash when taken for rheumatoid arthritis. We can interpret the other clusters similarly. Remember, this data is not real data. It is fake data made to show the application of clustering for drug adverse event study. From, this short post, we see that clustering can be used for knowledge discovery in drug adverse event reactions. Specially in cases where the data has millions of observations, where we cannot get any insight visually, clustering becomes handy for summarizing our data, for getting statistical insights and for discovering new knowledge.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toGoodReads: Exploratory data analysis and sentiment analysis (Part 2)" target="_blank" rel="bookmark" href="http://datascienceplus.com/exploratory-data-analysis-and-sentiment-analysis/">GoodReads: Exploratory data analysis and sentiment analysis (Part 2)</a></li>
<li><a title="Permanent Link toGoodReads: Webscraping and Text Analysis with R (Part 1)" target="_blank" rel="bookmark" href="http://datascienceplus.com/goodreads-webscraping-and-text-analysis-with-r-part-1/">GoodReads: Webscraping and Text Analysis with R (Part 1)</a></li>
<li><a title="Permanent Link toEuro 2016 analytics: Who’s playing the toughest game?" target="_blank" rel="bookmark" href="http://datascienceplus.com/euro-2016-analytics-whos-playing-the-toughest-game/">Euro 2016 analytics: Who’s playing the toughest game?</a></li>
<li><a title="Permanent Link toIntegrating R with Apache Hadoop" target="_blank" rel="bookmark" href="http://datascienceplus.com/integrating-r-with-apache-hadoop/">Integrating R with Apache Hadoop</a></li>
<li><a title="Permanent Link toPlotting App for ggplot2" target="_blank" rel="bookmark" href="http://datascienceplus.com/plotting-app-for-ggplot2/">Plotting App for ggplot2</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=_m3dbh9Gljc:aC-s5hMuZKc:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=_m3dbh9Gljc:aC-s5hMuZKc:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=_m3dbh9Gljc:aC-s5hMuZKc: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=_m3dbh9Gljc:aC-s5hMuZKc: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="https://www.r-bloggers.com/machine-learning-for-drug-adverse-event-discovery/"></div></div><div><a data-url="https://www.r-bloggers.com/machine-learning-for-drug-adverse-event-discovery/" data-counturl="https://www.r-bloggers.com/machine-learning-for-drug-adverse-event-discovery/" data-count="vertical" data-text="Machine Learning for Drug Adverse Event Discovery" 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/machine-learning-for-drug-adverse-event-discovery/"> 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/?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/j54_5e9kQ_4"></a></div>