<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jovan Trajceski on Medium]]></title>
        <description><![CDATA[Stories by Jovan Trajceski on Medium]]></description>
        <link>https://medium.com/@jovantrajceski?source=rss-de0c65de82f3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aSzs9ZUcrpJNsFObhh2NSg.png</url>
            <title>Stories by Jovan Trajceski on Medium</title>
            <link>https://medium.com/@jovantrajceski?source=rss-de0c65de82f3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 12:30:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jovantrajceski/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Structural Topic Modeling with R — Part II]]></title>
            <link>https://jovantrajceski.medium.com/structural-topic-modeling-with-r-part-ii-462e6e07328?source=rss-de0c65de82f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/462e6e07328</guid>
            <category><![CDATA[lda]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[topic-modeling]]></category>
            <category><![CDATA[r-markdown]]></category>
            <category><![CDATA[nlp]]></category>
            <dc:creator><![CDATA[Jovan Trajceski]]></dc:creator>
            <pubDate>Sat, 22 May 2021 04:04:10 GMT</pubDate>
            <atom:updated>2021-05-22T04:04:10.806Z</atom:updated>
            <content:encoded><![CDATA[<h3>Structural Topic Modeling with R — Part II</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EVlP7n_96xfuroc9_pZBRA.png" /></figure><p>In <a href="https://jovantrajceski.medium.com/structural-topic-modeling-with-r-part-i-2da2b353d362">Structural Topic Modeling with R — Part I</a>, I covered STM basics, including libraries, modeling, and finding an optimal number of topics, all done in RStudio.</p><p>We set up our final STM (Structural Topic Model) with 20 topics, 75 iterations, and set prevalence on publisher and date. This is what we have in the data panel in RStudio so far:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*VkQobW7ci7vGZT7wv_45Lg.png" /><figcaption>Screenshot of RStudio</figcaption></figure><p>Let’s see what our model came up with! The following tools can be used to evaluate the model:<br>1.<strong> labelTopics</strong> gives the top words for each topic,</p><p>2. <strong>findThoughts</strong> gives the top documents for each topic (the documents with the highest proportion of each topic).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2ee19b1e7328cef12fe72367ce2a629/href">https://medium.com/media/a2ee19b1e7328cef12fe72367ce2a629/href</a></iframe><p>A sample of the console output is provided below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*-H6fR6Fs9SCQLKKjo25krw.png" /><figcaption>Top words per topic.</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/973397840e85e101e8039a004a3c4278/href">https://medium.com/media/973397840e85e101e8039a004a3c4278/href</a></iframe><p>Console output for top documents by topic:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TPuTKGAYvAwiYrMEq4XhjA.png" /><figcaption>Top documents per topic.</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55b95c513de0404a601354edf61b50fb/href">https://medium.com/media/55b95c513de0404a601354edf61b50fb/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qOupom_SjHqFutJCKxJU9A.png" /><figcaption>Top 3 paragraphs for Topic 1 to 15</figcaption></figure><p>Sometimes, I find it useful to see the topic correlation. For this purpose, you can leverage the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcc20c0ac1d6f2b0c8944dc65cfb0b09/href">https://medium.com/media/bcc20c0ac1d6f2b0c8944dc65cfb0b09/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tst4w2VoZKrELro_HIkrdQ.png" /><figcaption>Topic correlation.</figcaption></figure><p>At this point, it would be a good idea to look at the convergence plot.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5da29c63ad87472c7dd966f0ff0150a/href">https://medium.com/media/e5da29c63ad87472c7dd966f0ff0150a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qiBWgpcAmNumZoSlsoobow.png" /><figcaption>Convergence plot.</figcaption></figure><p>We can also deploy the simple wordcloud library to review word distribution per specific topic.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11215a486d4d238aab9bb7c95735163c/href">https://medium.com/media/11215a486d4d238aab9bb7c95735163c/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VKqmi_qwtbl6-7F8tGvihQ.png" /><figcaption>Wordcloud for topic 17.</figcaption></figure><p>Now, we will jump into working with meta-data.</p><p>First, we will set up the estimated effect and take a look at the publishers&#39; effect. Please note, we need to add a small prior for numerical stability.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37f42c6be929d5e36a05c0ff80fcef5b/href">https://medium.com/media/37f42c6be929d5e36a05c0ff80fcef5b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vfmxpA9JcoqJhdhJ2T-uKg.png" /><figcaption>Effect of Zacks vs. Seeking Alpha publishers.</figcaption></figure><p>We can take a look at another set of publishers and compare the estimated effect.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56f173ebccd1aa681d39ab3bc9ea72ce/href">https://medium.com/media/56f173ebccd1aa681d39ab3bc9ea72ce/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KntSakGI507e0SWPTjs5kA.png" /><figcaption>Effect of ‘TalkMarkets’ vs. ‘Investopedia’ publishers.</figcaption></figure><p>We can also compare two topics or a single topic across two covariate levels to see how the terms differ.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0240e82d0df7a2899ff4f0d0c3cb09a0/href">https://medium.com/media/0240e82d0df7a2899ff4f0d0c3cb09a0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aICQnbPBwUmOQZp2oTItWA.png" /><figcaption>Comparing content in topic 17 and topic 12.</figcaption></figure><p>Next, we will review the topic proportions within the documents for 20 topics.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c99eb1f930548878a8ffee0e54891078/href">https://medium.com/media/c99eb1f930548878a8ffee0e54891078/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b6ywRjshvJwqkYbc8lg2SA.png" /><figcaption>Topic proportions.</figcaption></figure><p>Lastly, in this series of structural topic modeling in R, we will cover the topic quality and plot them with a topic number.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f285cec7fad0c1b5a518b43275074e04/href">https://medium.com/media/f285cec7fad0c1b5a518b43275074e04/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f7XMUYGJNeKMCUMYIK8FSQ.png" /><figcaption>Topic quality — Topic 16 and topic 20.</figcaption></figure><p>I hope you enjoyed setting up and reviewing Structural Topic Modeling in R.</p><p>Until next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=462e6e07328" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Structural Topic Modeling with R — Part I]]></title>
            <link>https://jovantrajceski.medium.com/structural-topic-modeling-with-r-part-i-2da2b353d362?source=rss-de0c65de82f3------2</link>
            <guid isPermaLink="false">https://medium.com/p/2da2b353d362</guid>
            <category><![CDATA[lda]]></category>
            <category><![CDATA[topic-modeling]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[stm]]></category>
            <category><![CDATA[r-markdown]]></category>
            <dc:creator><![CDATA[Jovan Trajceski]]></dc:creator>
            <pubDate>Tue, 18 May 2021 07:58:17 GMT</pubDate>
            <atom:updated>2021-05-22T04:05:08.791Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Structural Topic Modeling with R — Part I</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GGdIiy5197ZYgJOOk7AzCQ.png" /></figure><p><strong>Topic models</strong> allow us to summarize unstructured text, find clusters (hidden topics) where each observation or document (in our case, news article) is assigned a (Bayesian) probability of belonging to a specific topic.</p><p>The most common form of topic modeling is <em>LDA</em> (Latent Dirichlet Allocation). Other techniques exist, such as <em>Dynamic Topic Models</em>, <em>Correlated Topic Models</em>, <em>Hierarchical Topic Models</em>.</p><p>Structural topic modeling (STM) has been increasing in popularity over recent years. STM is essentially LDA that employs metadata to improve word assignment to topics within a corpus (collection of news articles).</p><p><strong><em>So we can easily say, STM = LDA + metadata.</em></strong></p><p>We will be using the <a href="https://www.kaggle.com/miguelaenlle/massive-stock-news-analysis-db-for-nlpbacktests?select=raw_partner_headlines.csv">Daily Financial News dataset published on Kaggle</a>. The dataset includes approximately 2 million articles for 6000 stocks from 2009–2020.</p><p>There is an R package called <a href="https://www.structuraltopicmodel.com/"><em>stm</em></a>, which provides almost everything we need for our first Structural Topic Model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E05IwIOZ0I1cuSLip1LfIw.png" /><figcaption>stm package overview</figcaption></figure><p>Let&#39;s load the required libraries (these will be used in Part II of this article as well):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/759f5d5740947a8bde0566615fd78c25/href">https://medium.com/media/759f5d5740947a8bde0566615fd78c25/href</a></iframe><p>Load original dataset and check for NAs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75e710c6f042292aab0e66501d1b9328/href">https://medium.com/media/75e710c6f042292aab0e66501d1b9328/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*mLkRZtrgLdyi2tx_siCoEg.png" /><figcaption>Original dataset overview</figcaption></figure><p>Let&#39;s randomly sample 1,000 rows out of the loaded dataset (feel free to reduce the sample size if your laptop can&#39;t handle it).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2bb9e12eaaed8c4401dd477c4b16dc2d/href">https://medium.com/media/2bb9e12eaaed8c4401dd477c4b16dc2d/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*yLVgCaMvT_blMRvrVgcPsA.png" /><figcaption>Our sample news</figcaption></figure><p>We will apply few preprocessing steps to our sample, including: <br>• Convert to lower case<br>• Remove Stop-Words<br>• Remove numbers<br>• Remove punctuation<br>• Words shorter than the minimum word length are discarded<br>• Stemming<br>• Replace all non-alphanumeric characters<br>• Create output</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1aeaf0cf06a68c355d40fa9741e7b4d0/href">https://medium.com/media/1aeaf0cf06a68c355d40fa9741e7b4d0/href</a></iframe><p>The processed object is a list of four objects: documents, vocab, meta, and docs removed. The document object is a list, one per document, of 2-row matrices; the first row indicates the index of a word found in the document, and the second row indicates the (nonzero) counts. If preprocessing causes any documents to be empty, they are removed, as are the corresponding rows of the meta-object.</p><p>Let&#39;s filter out terms that do not appear in more than ten documents (our threshold — you can change this as desired).</p><p>Also, we will level check for “publisher” and “stock” columns before proceeding with the structural topic model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6770be6d2b1c12af85ff1a43cab52d79/href">https://medium.com/media/6770be6d2b1c12af85ff1a43cab52d79/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*BLlVuwDpa64y_gHl4hGrIg.png" /><figcaption>Our corpus now has 882 documents, 103 terms, and 2334 tokens.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*fFuXhaFLOQnWCU5kxKT4xw.png" /><figcaption>This is what we have currently under our data panel in RStudio.</figcaption></figure><p>Let`s run our initial topic model [prevalence =~ publisher + date] at 15 topics (without finding optimal k), see how long it takes to run, and what the output looks like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcb593d3dc047c80a22d153552631f99/href">https://medium.com/media/bcb593d3dc047c80a22d153552631f99/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*3b9e_TrWKdqoLDCJEP5tdw.png" /><figcaption>It took 2 seconds for our first model to run. The model was terminated before convergence was reached.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1SXxZOWaH3i4b2OYTBtU2Q.png" /><figcaption>In our first STM plot, we can see topic 8 took the lead.</figcaption></figure><p>We can run a second preliminary topic model [prevalence =~ publisher + date] at 18 topics (without finding optimal k) but add 75 iterations, see how long it takes to run, and review its respective output.</p><p>Both STM models produce meaningful topics and keywords.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcbba240b6e4cdc95df344b380017356/href">https://medium.com/media/dcbba240b6e4cdc95df344b380017356/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xy03r1mvciSeOegfxWVOYA.png" /><figcaption>The plot of our second STM — we can see topic 17 is in the lead.</figcaption></figure><p>Next, we need to identify the<strong> number of optimal topics</strong>.</p><p>I will showcase three approaches in finding an optimal number of topics (k) that we can apply to the final version of our STM:</p><p>• <strong>Approach 1</strong>: use “searchK” function and look through 10 to 30 topics; no other parameters set</p><p>• <strong>Approach 2</strong>: use “searchK” function, search through 10, 20, 30, 40, 50, 60, 70 k tries, N= 500 and 75 iterations</p><p>• <strong>Approach 3</strong>: “Lee Mimno” via stm where k=0 and iterations = 75</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87b561c836949648da87c3314c17f061/href">https://medium.com/media/87b561c836949648da87c3314c17f061/href</a></iframe><p><strong>Approach 1</strong> takes approximately <em>23 minutes</em> on i7 CPU. The guideline is usually where the held-out likelihood is highest, and the residuals are lowest. It looks like 17 optimal topics under this approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6buDzvg4IzrHVcoGS6qhBA.png" /><figcaption>Approach 1: finding an optimal topic number.</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6dc369d00e37622ebba484eb68e3bcca/href">https://medium.com/media/6dc369d00e37622ebba484eb68e3bcca/href</a></iframe><p><strong>Approach 2 </strong>takes approximately 6 <em>minutes</em>. It looks like 20 optimal topics under this approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*hNkPl_TLTwvWzj0fJi2NLg.png" /><figcaption>Console screenshot: Approach 2 in finding optimal k.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kOZWWItLiELHTHUeZncN0g.png" /><figcaption>Approach 2: finding an optimal topic number.</figcaption></figure><p><strong>Approach 3 </strong>takes approximately 29 second<em>s</em>. It looks like 27 optimal topics under this approach.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4f46156754612da9f86197ef651fce5/href">https://medium.com/media/e4f46156754612da9f86197ef651fce5/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FTeDMUg-sAGwXL8zohUF-A.png" /><figcaption>Approach 3: finding an optimal topic number.</figcaption></figure><p>Based on the above, I have decided to run the final STM on 20 topics. The final STM runs in about 20 seconds.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b7fa8835a7435995af1c1fbb9f8ae84/href">https://medium.com/media/7b7fa8835a7435995af1c1fbb9f8ae84/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5VDiDlZPP-dp0eoDtQ56lg.png" /><figcaption>Final Structural Topic Model.</figcaption></figure><p>In <a href="https://jovantrajceski.medium.com/structural-topic-modeling-with-r-part-ii-462e6e07328?sk=008a013921288fe6053abf199f1104ab">Part II of this article</a>, I will evaluate the model and explore other aspects of structural topic modeling.</p><p>If you find this article of interest, please like and comment. Until next time!</p><p>Reference:</p><ol><li>The Structural Topic Model and Applied Social Science. <a href="https://scholar.princeton.edu/files/bstewart/files/stmnips2013.pdf">https://scholar.princeton.edu/files/bstewart/files/stmnips2013.pdf</a></li><li>stm package for R. <a href="https://www.structuraltopicmodel.com/">https://www.structuraltopicmodel.com/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2da2b353d362" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>