<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/text-mining-of-stack-overflow-questions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://juliasilge.com/blog/text-mining-stack-overflow/"> Rstats on Julia Silge</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>Note: Cross-posted <a rel="nofollow" href="https://stackoverflow.blog/2017/07/06/text-mining-stack-overflow-questions/" target="_blank">with the Stack Overflow blog</a>.</em></p>
<p>This week, my fellow Stack Overflow data scientist David Robinson and I are happy to announce the publication of our book <a rel="nofollow" href="http://amzn.to/2tZkmxG" target="_blank"><em>Text Mining with R</em></a> with <a rel="nofollow" href="http://www.jdoqocy.com/click-4428796-11290546?url=http%3A%2F%2Fshop.oreilly.com%2Fproduct%2F0636920067153.do%3Fcmp%3Daf-strata-books-video-product_cj_0636920067153_%25zp&cjsku=0636920067153" target="_blank">O’Reilly</a>. We are so excited to see this project out in the world, and so relieved to finally be finished with it! Text data is being generated all the time around us, in healthcare, finance, tech, and beyond; text mining allows us to transform that unstructured text data into real insight that can increase understanding and inform decision-making. In our book, we demonstrate how using tidy data principles can make text mining easier and more effective. Let’s mark this happy occasion with an exploration of Stack Overflow text data, and show how natural language processing techniques we cover in our book can be applied to real-world data to gain insight.</p>
<p>For this analysis, I’ll use Stack Overflow questions from <a rel="nofollow" href="https://www.kaggle.com/stackoverflow/stacksample/" target="_blank">StackSample</a>, a dataset of text from 10% of Stack Overflow questions and answers on programming topics that is freely available on <a rel="nofollow" href="https://www.kaggle.com/" target="_blank">Kaggle</a>. The code that I’m using in this post is available as a <a rel="nofollow" href="https://www.kaggle.com/juliasilge/topic-modeling-of-questions/" target="_blank">kernel on Kaggle</a>, so you can fork it for your own exploration.</p>
<p>This analysis focuses only on questions posted on Stack Overflow, and uses topic modeling to dig into the text.</p>
<h2>What is topic modeling?</h2>
<p>Topic modeling is a machine learning method for discovering “topics” that occur in a collection of documents; it is a powerful tool for organizing large collections of raw text. Topic modeling is an unsupervised method, which means that I as the analyst don’t decide ahead of time what the topics will be about; we can find topics within text even if we’re not sure what we’re looking for ahead of time. Topic modeling can be used to discover underlying structure within text. In the context of the kind of topic model I’ll implement (LDA topic modeling),</p>
<ul>
<li>every document is a mixture of topics and</li>
<li>every topic is a mixture of words.</li>
</ul>
<p>Documents can share topics, and topics can share words, in any proportions. In our case for this analysis, each Stack Overflow question is a document. Let’s imagine (for the sake of explanation) that there are two topics, one that is made up of the three words “table”, “select”, and “join” and a second that is made up of the three words “function”, “print”, and “return.” One question might be 100% topic 2, and another question might be 50% topic 1 and 50% topic 2. The statistical modeling process of topic modeling finds the topics in the text dataset we are dealing with, which words contribute to the topics, and which topics contribute to which documents.</p>
<h2>Modeling Stack Overflow questions</h2>
<p>For this blog post, I fit a model with 12 topics to this dataset. The question of how to choose the number of topics in topic modeling is a complicated one, but in this case, 12 topics gives us a good result for exploration. The process of building this topic model also involves cleaning text, removing stop words, and building a document-term matrix, all considerations covered in <a rel="nofollow" href="http://amzn.to/2tZkmxG" target="_blank">our book</a>.</p>
<p>One of the most compelling reasons to adopt tidy data principles when doing topic modeling is that we can easily explore which words contribute the most to which topics, and which topics contribute the most to which documents (questions on Stack Overflow, in this case). This is how we find out what kind of content corresponds to the topics fit by the model. Let’s look at that for these specific questions. Which words are most important for each topic, in this model with 12 topics?</p>
<p><img alt="top-terms" src="https://i0.wp.com/juliasilge.com/figs/2017-07-06-text-mining-stack-overflow/top_terms-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>First look at topic 5. That topic is all English words, not terms from code; the topic model has fit one topic that is not specific to any tag, programming language, or technology used on Stack Overflow but instead aligns with the text people use to talk about their questions. Next, look at topic 3; most of those words look very general to me and applicable to almost all technologies (“file”, “error”, “server”, and so forth). Last, look through some of the other collections of terms. For some tech ecosystems that I am familiar with, these collections of terms make sense together.</p>
<p>What if there are words you are interested in, but that you don’t see in these plots? We can use tidy data principles to find which topic any word has the highest probability of being generated from. For example, “git” and “docker” are most likely to be generated from topic 3, “boost” is most likely to be generated from topic 10, and “ggplot2” (my own personal favorite data visualization tool!) is most likely to be generated from topic 4.</p>
<h2>Connecting to tags</h2>
<p>We can look at this from a different angle because each question on Stack Overflow has a tag, like “r” or “c#” or “sql”. The topic model estimates a probability that each document belongs to each topic; it’s the estimated proportion of words from that document that are generated from that topic. We know the tags for each document, so let’s examine which tags are associated with each topic.</p>
<p><img alt="top-tags" src="https://i2.wp.com/juliasilge.com/figs/2017-07-06-text-mining-stack-overflow/top_tags-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Remember that topic 5 was the one that corresponded to English words where users discuss and describe their problem, so that is a measure of something different than the other topics. Topic 1 looks like front-end web development, topic 4 is databases, topic 10 is C and low-level programming, and so forth. Remember, the tags did <em>not</em> go into the unsupervised modeling process; we are just looking at them after the fact. The topic modeling process has taken the raw text of Stack Overflow questions and discovered underlying patterns and structure. This is what topic modeling does, whether you are looking at <a rel="nofollow" href="http://tidytextmining.com/nasa.html#topic-modeling" target="_blank">NASA metadata</a> or <a rel="nofollow" href="http://tidytextmining.com/topicmodeling.html#library-heist" target="_blank">classic literature</a>.</p>
<p>Let’s look at a few real examples from this dataset so you can see how this worked out. Each of the following questions is part of the <a rel="nofollow" href="https://www.kaggle.com/stackoverflow/stacksample/" target="_blank">StackSample</a> dataset and this particular topic model.</p>
<p><img alt="ios" src="https://i2.wp.com/juliasilge.com/figs/2017-07-06-text-mining-stack-overflow/ios_question.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This <a rel="nofollow" href="https://stackoverflow.com/questions/24049020/nsnotificationcenter-addobserver-in-swift" target="_blank">first example question</a> is relatively short, and the topic model estimates that is 91% topic 12 and 6% topic 3. Looks good! I don’t see many of the top 10 terms from the first plot in this blog post for topic 12 here, but the topic model has classified it into the topic that is dominated by iOS, Objective-C, iPhone, and Swift.</p>
<p><img alt="python" src="https://i2.wp.com/juliasilge.com/figs/2017-07-06-text-mining-stack-overflow/python_question.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Our <a rel="nofollow" href="https://stackoverflow.com/questions/30216000/why-is-faster-than-list" target="_blank">second example question</a> is longer, and the topic model estimates that it is 82% topic 5 and 18% topic 7. This question has a lot of English text and not much code, and that is reflected by the modeling. The model has chosen topic 7, dominated by Python and Django, for this question.</p>
<p><img alt="haskell" src="https://i1.wp.com/juliasilge.com/figs/2017-07-06-text-mining-stack-overflow/haskell_question.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Last, let’s look at this <a rel="nofollow" href="https://stackoverflow.com/questions/17247880/getting-associated-type-synonyms-with-template-haskell" target="_blank">Haskell question</a>. Haskell is a sparsely used tag, and did not show up in the plot of top tags for topics at all. Where did this question land? The model estimates that this question is 63% topic 5 and 36% topic 10, with a tiny smidge of topic 7. I actually really like that the model has done this, putting Haskell in with low-level tags like C++/C, arrays, and pointers.</p>
<p>A model like this is not just for analysis; it can be used to make predictions or implement new ideas. For example, one idea for Stack Overflow would be to automatically suggest a list of possible tags for new questions based on the text of a question. It looks like such a feature would work best for questions with at least some code and would be less accurate suggesting tags for questions that are almost all English words, or for very unusual tags. If there are any particular questions or tags <em>you</em> would like to explore yourself, fork the <a rel="nofollow" href="https://www.kaggle.com/juliasilge/topic-modeling-of-questions/" target="_blank">kernel on Kaggle</a> and build a topic model yourself!</p>
<p>Approaches like topic modeling can provide a way to get from raw text to a deeper understanding of unstructured data. In <a rel="nofollow" href="http://amzn.to/2tZkmxG" target="_blank">our book</a>, we discuss these and other text mining tasks, from the nitty gritty of converting back and forth between common text data structures to <a rel="nofollow" href="https://www.kaggle.com/juliasilge/tf-idf-of-stack-overflow-questions/" target="_blank">measuring tf-idf</a> to sentiment analysis. One of the things I like best about working as a data scientist at Stack Overflow is applying tools exactly like these to real-world data from the developer community, to learn about developers worldwide and to help clients make decisions about hiring and engaging with developers. Check out <a rel="nofollow" href="https://insights.stackoverflow.com/" target="_blank">Developer Insights</a> to learn more.</p>
	<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://juliasilge.com/blog/text-mining-stack-overflow/"> Rstats on Julia Silge</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/SPgDmGYKW78" height="1"></a></div>