<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/when-the-predictions-are-more-accurate-than-the-response/"></div></div><div><a data-url="http://www.r-bloggers.com/when-the-predictions-are-more-accurate-than-the-response/" data-counturl="http://www.r-bloggers.com/when-the-predictions-are-more-accurate-than-the-response/" data-count="horizontal" data-text="When the Predictions are more accurate than the Response" 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://scweiss.blogspot.com/2016/05/when-predictions-are-more-accurate-than.html"> sweissblaug</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<div dir="ltr">
<div>
<h1><span><span>The purpose of this post is to discuss the methodology of classifying paragraphs of documents, where the document is only assigned one topic. </span></span></h1>
<h1><span><span>In my usage, a document is an article that is assigned a Section (Business, Economics / Finance, Science, Europe, Middle East, etc). Of course each article can have multiple topics discussed. For Example; a discussion on Ukraine today might discuss the economics of its exchange rate along with European Union and Russian Military. I’m assuming each paragraph discusses on particular topic, and I’m interested in each which paragraphs discuss economics.</span></span></h1>
<div></div>
</div>
<div><span>The methodology I propose is to use the original classification to build a model and use this model to fit the paragraphs. The code below suggests this method increases classification performance.</span></div>
<pre><code><span>##Assume there are two classes in each document. Each paragraph has data - in this case a random normal - and the higher the value the higher probability it is associated with a particular topic. This could be thought of as word frequency </span><br><br><span>##create documents - each document consists of 100 paragraphs </span><br><span>documents</span><span>=</span><span>lapply</span><span>(</span><span>1</span><span>:</span><span>100</span>, <span>function</span><span>(</span><span>x</span><span>)</span> <span>rnorm</span><span>(</span><span>100</span><span>)</span><span>)</span><br><br><span>##create topics for each paragraph - 100 documents with a 100 paragraphs each</span><br><span>paragraph_classes</span><span>=</span><span>lapply</span><span>(</span><span>documents</span>, <span>function</span><span>(</span><span>x</span><span>)</span> <span>rbinom</span><span>(</span><span>100</span>,<span>size</span><span>=</span><span>1</span>,<span>prob</span><span>=</span><span>1</span><span>/</span><span>(</span><span>1</span><span>+</span><span>exp</span><span>(</span><span>-</span><span>x</span><span>)</span><span>)</span><span>)</span><span>)</span><br><br><span>## a document consists of several paragraphs but the document assigned the most common topic</span><br><span>document_classes</span><span>=</span><span>(</span><span>sapply</span><span>(</span><span>paragraph_classes</span>, <span>function</span><span>(</span><span>x</span><span>)</span> <span>sum</span><span>(</span><span>x</span><span>></span><span>0</span><span>)</span> <span>)</span><span>></span><span>50</span><span>)</span><br><br><span>unlisted_documents</span><span>=</span><span>unlist</span><span>(</span><span>documents</span><span>)</span><br><span>unlisted_class</span><span>=</span><span>rep</span><span>(</span><span>document_classes</span>, <span>each</span><span>=</span><span>100</span><span>)</span><br><br><span>##the original correct classficiation rate is around 54%</span><br><span>table</span><span>(</span><span>unlist</span><span>(</span><span>paragraph_classes</span><span>)</span>,<span>unlisted_class</span><span>)</span></code></pre>
<pre><code>##    unlisted_class<br>##     FALSE TRUE<br>##   0  2643 2353<br>##   1  2257 2747</code></pre>
<pre><code><span>sum</span><span>(</span><span>diag</span><span>(</span><span>table</span><span>(</span><span>unlist</span><span>(</span><span>paragraph_classes</span><span>)</span>,<span>unlisted_class</span><span>)</span><span>)</span><span>)</span><span>/</span><span>length</span><span>(</span><span>unlisted_class</span><span>)</span></code></pre>
<pre><code>## [1] 0.539</code></pre>
<pre><code><span>##build a model. each paragraph response variable is the original class assigned</span><br><span>glm</span><span>=</span><span>glm</span><span>(</span><span>unlisted_class</span><span>~</span><span>unlisted_documents</span>, <span>family</span><span>=</span><span>&quot;binomial&quot;</span><span>)</span><br><br><br><span>#the predicted classification using model on data - can see classification results are over 14% greater than original classficiations</span><br><span>predicted_values</span><span>=</span><span>scale</span><span>(</span><span>predict</span><span>(</span><span>glm</span><span>)</span><span>)</span><br><span>table</span><span>(</span><span>predicted_values</span><span>></span><span>0</span>,<span>unlist</span><span>(</span><span>paragraph_classes</span><span>)</span><span>></span><span>.5</span><span>)</span></code></pre>
<pre><code>##        <br>##         FALSE TRUE<br>##   FALSE  3386 1647<br>##   TRUE   1610 3357</code></pre>
<pre><code><span>sum</span><span>(</span><span>diag</span><span>(</span><span>table</span><span>(</span><span>predicted_values</span><span>></span><span>0</span>,<span>unlist</span><span>(</span><span>paragraph_classes</span><span>)</span><span>></span><span>.5</span><span>)</span><span>)</span><span>)</span><span>/</span><span>length</span><span>(</span><span>unlisted_class</span><span>)</span></code></pre>
<pre><code>## [1] 0.6743</code></pre>
</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/when-the-predictions-are-more-accurate-than-the-response/"></div></div><div><a data-url="http://www.r-bloggers.com/when-the-predictions-are-more-accurate-than-the-response/" data-counturl="http://www.r-bloggers.com/when-the-predictions-are-more-accurate-than-the-response/" data-count="vertical" data-text="When the Predictions are more accurate than the Response" 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://scweiss.blogspot.com/2016/05/when-predictions-are-more-accurate-than.html"> sweissblaug</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/7l7B5XMR2H4"></a></div>