title: LDA - How do I normalise and "add the smoothing constant" to raw document-topic allocation counts?
body: <h1>Context</h1>
  
  <p>thanks in advance for your help. Right now, I have run a dataset through the LDA function in Jonathan Chang's 'lda' package (N.B. this is different from the 'topicmodels' package). Below is a replicable example, which uses the cora dataset that comes automatically when you install and load the 'lda' package.</p>
  
  <pre><code>library(lda)
  data(cora.documents) #list of words contained in each of the 2,410 documents
  data(cora.vocab) #vocabulary list of words that occur at least once across all documents
  </code></pre>
  
  <p>Thereafter, I conduct the actual LDA by setting the different parameters and running the code.</p>
  
  <pre><code>#parameters for LDA algorithm
  K &lt;- 20 #number of topics to be modelled from the corpus, "K"
  G &lt;- 1000 #number of iterations to cover - the higher this number, the more likely the data converges, "G"
  alpha &lt;- 0.1 #document-topic distributions, "alpha"
  beta &lt;- 0.1 #topic-term distributions, "beta/eta"
  
  #creates an LDA fit based on above parameters
  lda_fit &lt;- lda.collapsed.gibbs.sampler(cora.documents, cora.vocab, K = 20, 
                                         num.iterations = G, alpha, beta) 
  </code></pre>
  
  <p>Following which, we examine one component of the output of the LDA model, which is called <em>document_sums</em>. This component displays the number of words that each individual document contains that is allocated to each of the 20 topics (based on the K-value I chose). For instance, one document may have 4 words allocated to Topic 3, and 12 words allocated to Topic 19, in which case <strong>the document is assigned to Topic 19</strong>.</p>
  
  <pre><code>#gives raw figures for no. of times each document (column) had words allocated to each of 20 topics (rows)
  document_sums &lt;- as.data.frame(lda_fit$document_sums)
  document_sums[1:20, 1:20] 
  </code></pre>
  
  <h1>Question</h1>
  
  <p>However, what I want to do is essentially use the principle of fuzzy membership. Instead of allocating each document to the topic it contains the most words in, I want to extract the probabilities that each document gets allocated to each topic. <em>document_sums</em> quite close to this, but I still have to do some processing on the raw data.</p>
  
  <p>Jonathan Chang, the creator of the 'lda' package, himself says this in <a href="https://stackoverflow.com/questions/21169266/output-of-lda-collapsed-gibbs-sampler-command-from-r-lda-package?newreg=f86e859c01544da497eab61cab79c1c1">this thread</a>:</p>
  
  <blockquote>
    <p>n.b. If you want to convert the matrix to probabilities just row normalize and add the smoothing constant from your prior. The function here just returns the raw number of assignments in the last Gibbs sampling sweep. ()</p>
  </blockquote>
  
  <p>Separately, <a href="https://stats.stackexchange.com/questions/221982/how-to-use-fitted-rtm-model-to-predict-new-topics">another reply</a> on another forum reaffirms this:</p>
  
  <blockquote>
    <p>The resulting document_sums will give you the (unnormalized) distribution over topics for the test documents. Normalize them, and compute the inner product, weighted by the RTM coefficients to get the predicted link probability (or use predictive.link.probability)</p>
  </blockquote>
  
  <p>And thus, my question is, how do I normalise my <em>document_sums</em> and <strong>'add the smoothing constant'</strong>? These I am unsure of.</p>