title: Getting term weights out of an LDA model in R
body: <p>I was wondering if anyone knows of a way to extract term weights / probabilities out of a topic model constructed in R, using the <code>topicmodels</code> package.</p>
  
  <p>Following the example in the following <a href="https://cran.r-project.org/web/packages/topicmodels/vignettes/topicmodels.pdf" rel="nofollow">link</a> I created a topic model like so:</p>
  
  <pre><code>Gibbs = LDA(JSS_dtm, k = 4, 
              method = "Gibbs",
              control = list(seed = 1, burnin = 1000, thin = 100, iter = 1000))
  </code></pre>
  
  <p>we can then get the topics using <code>topics(Gibbs,1)</code>, terms using <code>terms(Gibbs,10)</code> and even the topic probabilities using <code>Gibbs@gamma</code>, but after looking at <code>str(Gibbs)</code> it appears that there is no way to get term probabilities within each topic. This would be useful because topic 1 could be 50% term A and 50% term B, while topic 2 can be 90% Term C and 10% term D. I'm aware that tools like MALLET and Python's NLTK module offer this capability, but I was also hoping that a similar solution may exist in R.</p>
  
  <p>If anyone know how this can be achieved, please let us know. </p>
  
  <p>Many thanks!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>For the benefit of the others, I thought I'd share my current workaround. If I knew term probabilities, I'd be able to visualise them and give the viewer a better understanding of what each topic means, but without the probabilities, I'm simply breaking down my data by each topic and creating a word cloud for each topic using binary weights. While these values are not probabilities, they give an indication of what each topic focuses on.</p>
  
  <p>See the below code:</p>
  
  <pre><code>JSS_text   &lt;- sapply(1:length(JSS_papers[,"description"]), function(x) unlist(JSS_papers[x,"description"]))
  jss_df     &lt;- data.frame(text=JSS_text,topic=topics(Gibbs, 1))
  jss_dec_df &lt;- data.frame()
  
  for(i in unique(topics(Gibbs, 1))){
    jss_dec_df &lt;- rbind(jss_dec_df,data.frame(topic = i, 
                                              text = paste(jss_df[jss_df$topic==i,"text"],collapse=" ")))
  }
  
  corpus &lt;- Corpus(VectorSource(jss_dec_df$text))
  JSS_dtm &lt;- TermDocumentMatrix(corpus,control = list(stemming = TRUE, 
                                                      stopwords = TRUE, 
                                                      minWordLength = 3,
                                                      removeNumbers = TRUE, 
                                                      removePunctuation = TRUE,
                                                      function(x)weightSMART(x,spec="bnc")))
  
  (JSS_dtm  = removeSparseTerms(JSS_dtm,0.1)) # not the sparsity parameter 
  
  library(wordcloud)
  comparison.cloud(as.matrix(JSS_dtm),random.order=F,max.words=100, 
                   scale=c(6,0.6),colours=4,title.size=2)
  </code></pre>