1. Topic modeling

Thomas W. Jones


Topic modeling

textmineR has extensive functionality for topic modeling. You can fit Latent Dirichlet Allocation (LDA), Correlated Topic Models (CTM), and Latent Semantic Analysis (LSA) from within textmineR. (Examples with LDA and LSA follow below.) As of this writing, textmineR’s LDA and CTM functions are wrappers for other packages to facilitate a consistent workflow. (And textmineR takes advantage of the RSpectra package for LSA’s single-value decomposition.) Plans exist to impelement LDA natively with Rcpp sometime in 2018.

textmineR’s consistent representation of topic models boils down to two matrices. The first, “theta” (\(\Theta\)), has rows representing a distribution of topics over documents. The second, phi (\(\Phi\)), has rows representing a distribution of words over topics. In the case of probabilistic models, these are categorical probability distributions. For non-probabilistic models (e.g. LSA) these distributions are, obviously, not probabilities. With LSA, for example, there is a third object representing the sigular values in the decomposition.

In addition, textmineR has utility functions for topic models. This includes some original research. Examples include an R-squared for probabilistic topic models (working paper here), probabilistic coherence (a measure of topic quality), and a topic labeling function based on most-probable bigrams. Other utilities are demonstrated below


# load movie_review dataset from text2vec
data(movie_review, package = "text2vec")

#> 'data.frame':    5000 obs. of  3 variables:
#>  $ id       : chr  "5814_8" "2381_9" "7759_3" "3630_4" ...
#>  $ sentiment: int  1 1 0 0 1 1 0 0 0 1 ...
#>  $ review   : chr  "With all this stuff going down at the moment with MJ i've started listening to his music, watching the odd docu"| __truncated__ "\\\"The Classic War of the Worlds\\\" by Timothy Hines is a very entertaining film that obviously goes to great"| __truncated__ "The film starts with a manager (Nicholas Bell) giving welcome investors (Robert Carradine) to Primal Park . A s"| __truncated__ "It must be assumed that those who praised this film (\\\"the greatest filmed opera ever,\\\" didn't I read some"| __truncated__ ...

# let's take a sample so the demo will run quickly
# note: textmineR is generally quite scaleable, depending on your system
s <- sample(1:nrow(movie_review), 500)

movie_review <- movie_review[ s , ]

# create a document term matrix 
dtm <- CreateDtm(doc_vec = movie_review$review, # character vector of documents
                 doc_names = movie_review$id, # document names
                 ngram_window = c(1, 2), # minimum and maximum n-gram length
                 stopword_vec = c(tm::stopwords("english"), # stopwords from tm
                                  tm::stopwords("SMART")), # this is the default value
                 lower = TRUE, # lowercase - this is the default value
                 remove_punctuation = TRUE, # punctuation - this is the default
                 remove_numbers = TRUE, # numbers - this is the default
                 verbose = FALSE, # Turn off status bar for this demo
                 cpus = 2) # default is all available cpus on the system

LDA Example

To fit an LDA model in textmineR, use the FitLdaModel function. Input is a document term matrix. textmineR implements 2 methods for LDA, Gibbs sampling, and variational expectation maximization (also known as variational Bayes). The default is Gibbs sampling.

# Fit a Latent Dirichlet Allocation model
# note the number of topics is arbitrary here
# see extensions for more info
model <- FitLdaModel(dtm = dtm, 
                     k = 100, 
                     iterations = 200, # i recommend a larger value, 500 or more
                     alpha = 0.1, # this is the default value
                     beta = 0.05, # this is the default value
                     cpus = 2) 

The output from the model is a list with the two matrices listed above.

# two matrices: 
# theta = P(topic | document)
# phi = P(word | topic)
#> List of 2
#>  $ theta: num [1:500, 1:100] 6.13e-07 8.55e-07 7.41e-07 6.90e-07 2.11e-02 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:500] "2595_9" "8892_2" "8620_8" "2892_10" ...
#>   .. ..$ : chr [1:100] "t_1" "t_2" "t_3" "t_4" ...
#>  $ phi  : num [1:100, 1:55459] 1.22e-07 1.51e-07 1.34e-07 1.40e-07 1.73e-07 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:100] "t_1" "t_2" "t_3" "t_4" ...
#>   .. ..$ : chr [1:55459] "making_debut" "bureau_lowly" "scenes_thing" "injections" ...

Once we have created a model, we need to evaluate it. For overall goodness of fit, textmineR has R-squared and log likelihood. R-squared is interpretable as the proportion of variability in the data explained by the model, as with linear regression. For a full derivation and explanation of properties. See the working paper, here.

The log likelihood has a more difficult interpretation. Though, as shown in the R-squared working paper, R-squared and log likelihood are highly correlated.

# R-squared 
# - only works for probabilistic models like LDA and CTM
model$r2 <- CalcTopicModelR2(dtm = dtm, 
                             phi = model$phi,
                             theta = model$theta,
                             cpus = 2)

#> [1] 0.199623

# log Likelihood (does not consider the prior) 
# - only works for probabilistic models like LDA and CTM
model$ll <- CalcLikelihood(dtm = dtm, 
                           phi = model$phi, 
                           theta = model$theta,
                           cpus = 2)

#> [1] -370869.1

Next, we turn our attention to topic quality. There are many “topic coherence” metrics available in the literature. For example, see this paper or this paper. textmineR implements a new topic coherence measure based on probability theory. (A formal write up of this metric will be included in my PhD dissertation, expected 2020.)

Probabilistic coherence measures how associated words are in a topic, controlling for statistical independence. For example, suppose you have a corpus of articles from the sports section of a newspaper. A topic with the words {sport, sports, ball, fan, athlete} would look great if you look at correlation, without correcting for independence. But we actually know that it’s a terrible topic because the words are so frequent in this corpus as to be meaningless. In other words, they are highly correlated with each other but they are statistically-independent of each other.

For each pair of words \(\{a, b\}\) in the top M words in a topic, probabilistic coherence calculates \(P(b|a) - P(b)\), where \(\{a\}\) is more probable than \(\{b\}\) in the topic.

Here’s the logic: if we restrict our search to only documents that contain the word \(\{a\}\), then the word \(\{b\}\) should be more more probable in those documents than if chosen at random from the corpus. \(P(b|a)\) measures how probable \(\{b\}\) is only in documents containing \(\{a\}\). \(P(b)\) measures how probable \(\{b\}\) is in the corpus as a whole. If \(\{b\}\) is not more probable in documents containing \(\{a\}\), then the difference \(P(b|a) - P(b)\) should be close to zero.

For example, suppose the top 4 words in a topic are \(\{a, b, c, d\}\). Then, we calculate

  1. \(P(a|b) - P(b)\), \(P(a|c) - P(c)\), \(P(a|d) - P(d)\)
  2. \(P(b|c) - P(c)\), \(P(b|d) - P(d)\)
  3. \(P(c|d) - P(d)\)

And all 6 differences are averaged together, giving the probabilistic coherence measure.

# probabilistic coherence, a measure of topic quality
# this measure can be used with any topic model, not just probabilistic ones
model$coherence <- CalcProbCoherence(phi = model$phi, dtm = dtm, M = 5)

#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#> 0.03439 0.14481 0.24439 0.28783 0.38961 0.98440

     col= "blue", 
     main = "Histogram of probabilistic coherence")

We’ll see the real value of coherence after calculating a few more objects. (Note: a future version of textmineR will calculate probabilistic coherence and the below objects automatically when you call FitLdaModel or any other topic model.)

In the chunk below, we will

  1. Pull out the top 5 terms for each topic
  2. Calculate the most frequent (prevalent) topics in the corpus
  3. Get some bi-gram topic labels from a naive labeling algorithm (These naive labels are based on \(P(\text{bi-gram}|\text{topic}) - P(\text{bi-gram})\). Noticing a theme?)

We’ll then pull these together, along with coherence, into a table that summarizes the topic model.

# Get the top terms of each topic
model$top_terms <- GetTopTerms(phi = model$phi, M = 5)
t_1 paperhouse debut rose charlotte door
t_2 science science_fiction fiction br_read japanese
t_3 scooby doo scooby_doo shaggy voice
t_4 jokes scary_movie amusing hardy execution
t_5 ned kelly dinocroc jerilderie ann
t_6 legendary myra west bourne breckinridge
# Get the prevalence of each topic
# You can make this discrete by applying a threshold, say 0.05, for
# topics in/out of docuemnts. 
model$prevalence <- colSums(model$theta) / sum(model$theta) * 100

# textmineR has a naive topic labeling tool based on probable bigrams
model$labels <- LabelTopics(assignments = model$theta > 0.05, 
                            dtm = dtm,
                            M = 1)

#>     label_1            
#> t_1 "br_br"            
#> t_2 "science_fiction"  
#> t_3 "scooby_doo"       
#> t_4 "scary_movie"      
#> t_5 "revive_wife"      
#> t_6 "myra_breckinridge"

# put them together, with coherence into a summary table
model$summary <- data.frame(topic = rownames(model$phi),
                            label = model$labels,
                            coherence = round(model$coherence, 3),
                            prevalence = round(model$prevalence,3),
                            top_terms = apply(model$top_terms, 2, function(x){
                              paste(x, collapse = ", ")
                            stringsAsFactors = FALSE)
model$summary[ order(model$summary$prevalence, decreasing = TRUE) , ][ 1:10 , ]
Summary of 10 most prevalent topics
topic label_1 coherence prevalence top_terms
t_98 t_98 br_br 0.053 33.618 br, br_br, movie, film, good
t_32 t_32 br_br 0.126 1.000 game, games, addictive, multi, sydney
t_91 t_91 star_power 0.167 0.950 david, tony, rosenlski, cd, peter
t_17 t_17 br_br 0.244 0.900 singing, cody, sci_fi, fi, rocket
t_69 t_69 rock_roll 0.377 0.896 rock, rock_roll, roll, ramones, punk
t_27 t_27 br_br 0.227 0.864 hanks, political, sullivan, son, douglas
t_24 t_24 br_br 0.230 0.829 witches, unfunny, powers, dull, teenage_witches
t_81 t_81 dark_heart 0.105 0.826 heart, care, dark, dark_heart, christy
t_25 t_25 watch_rhine 0.079 0.805 scarecrows, chandler, francine, marriage, friends
t_28 t_28 br_br 0.220 0.802 summary, elisha, farmer, wang, lan

Ok, you’ve built a topic model. You’ve decided how well it fits your data. You’ve examined coherence, top words, and so on. Now you want to get topic distributions for new documents. (Remember, we only used 500 of our 5,000 documents to train the model.) To do this, we need Bayes’ Rule.

The rows of \(\Phi\) are \(P(\text{word}|\text{topic})\). However, to get predictions for new documents, we need \(P(\text{topic}|\text{word})\). Remembering Bayes’ Rule, we get

\[\begin{align} P(\text{topic}|\text{word}) &= \frac{P(\text{word}|\text{topic})P(\text{topic})}{P(\text{word})} \end{align}\]

Detail-oriented readers may wonder how you can get \(P(\text{topic})\). We can get this through \(\sum_j P(\text{topic}|\text{document}_j)P(\text{document}_j)\).

For now, textmineR refers to the resulting matrix as \(\Phi'\) or “phi prime”. (Note: this will be called \(\Gamma\) or “gamma” in textmineR version 3.0+.)

textmineR’s CalcPhiPrime function does the above calculations for you.

Once you have \(\Phi'\), a simple dot product with the DTM of your new documents (\(A\)) will get new topic predictions.

\[\begin{align} \Theta_{new} &= A \cdot \Phi'^T \end{align}\]

As of this writing, you will have to take care to make sure your vocabulary aligns. (I’d suggest using something like intersect(colnames(dtm), colnames(theta)).) (textmineR version 3.0 will enable a predict method for topic models that will handle all of this for you.)

# first get a prediction matrix, phi is P(word | topic)
# we need P(topic | word), or "phi_prime"
model$phi_prime <- CalcPhiPrime(phi = model$phi,
                                theta = model$theta)

# set up the assignments matrix and a simple dot product gives us predictions
assignments <- dtm / rowSums(dtm)

assignments <- assignments %*% t(model$phi_prime)

assignments <- as.matrix(assignments) # convert to regular R dense matrix

For the pedantic, the above method is a “frequentist” prediction even though LDA is a Bayesian model. textmineR 3.0 will implement a fully Bayesian predict method for LDA. In the meantime, this frequentist method works well, but is a little noisier. We can compare the difference between the frequentist predictions to the Bayesian ones, as the two barplots below show. By and large they are the same.

# compare the "fit" assignments to the predicted ones
barplot(rbind(model$theta[ rownames(dtm)[ 1 ] , ],
              assignments[ rownames(dtm)[ 1 ] , ]), 
        las = 2,
        main = "Comparing topic assignments",
        beside = TRUE,
        col = c("red", "blue"))

       legend = c("Bayesian (during fitting)", "Frequentist (predicted)"),
       fill = c("red", "blue"))

Depending on your application, you can reformat the outputs of phi, theta, assignments, the summary table etc. to suite your needs. For example, you can build a “semantic” search of your documents by vectorizing the query with CreateDtm, then predicting under the model with phi_prime.

LSA Example

Latent semantic analysis was arguably the first topic model. LSA was patented in 1988. It uses a single value decomposition on a document term matrix, TF-IDF matrix, or similar.

In textmineR’s notation:

\[\begin{align} A &= \Theta \cdot S \cdot \Phi \end{align}\]

\(\Theta\) and \(\Phi\) have the same (though non-probabilistic) interpretation as in LDA. \(S\) is the matrix of single values.

The workflow for LSA is largely the same for LDA. Two key differences: we will use the IDF vector mentioned above to create a TF-IDF matrix and we cannot get an R-squared for LSA as it is non-probabilistic.

# get a tf-idf matrix
tf_sample <- TermDocFreq(dtm)

tf_sample$idf[ is.infinite(tf_sample$idf) ] <- 0 # fix idf for missing words

tf_idf <- t(dtm / rowSums(dtm)) * tf_sample$idf

tf_idf <- t(tf_idf)

# Fit a Latent Semantic Analysis model
# note the number of topics is arbitrary here
# see extensions for more info
lsa_model <- FitLsaModel(dtm = tf_idf, 
                     k = 100)

# three objects: 
# theta = distribution of topics over documents
# phi = distribution of words over topics
# sv = a vector of singular values created with SVD
#> List of 3
#>  $ sv   : num [1:100] 1.123 1.034 0.93 0.911 0.901 ...
#>  $ theta: num [1:500, 1:100] 0.000855 0.001463 0.000916 0.001615 0.00305 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:500] "2595_9" "8892_2" "8620_8" "2892_10" ...
#>   .. ..$ : chr [1:100] "t_1" "t_2" "t_3" "t_4" ...
#>  $ phi  : num [1:100, 1:55459] 2.06e-05 7.00e-05 -4.88e-04 -4.52e-05 2.99e-04 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:100] "t_1" "t_2" "t_3" "t_4" ...
#>   .. ..$ : chr [1:55459] "making_debut" "bureau_lowly" "scenes_thing" "injections" ...

We cannot get a proper R-squared for an LSA model. (Actually, multiplying \(\Phi \cdot S \cdot \Theta\) would give us exactly our document term matrix and an R-squared of \(1\). There isn’t really a proper interpretation of \(\Phi \cdot \Theta\) with LSA.)

However, we can still use probabilistic coherence to evaluate individual topics. We’ll also get our top terms and make a summary table as we did with LDA, above.

# probabilistic coherence, a measure of topic quality
# - can be used with any topic lsa_model, e.g. LSA
lsa_model$coherence <- CalcProbCoherence(phi = lsa_model$phi, dtm = dtm, M = 5)

#>     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
#> -0.02771  0.18238  0.39689  0.47248  0.73780  0.99800

hist(lsa_model$coherence, col= "blue")

# Get the top terms of each topic
lsa_model$top_terms <- GetTopTerms(phi = lsa_model$phi, M = 5)
t_1 colombo listed playing_caracter colombo_made listed_movie
t_2 read_years allende isabel author_movie isabel_allende
t_3 read_years allende isabel author_movie isabel_allende
t_4 weird_scare jim_carrey freaking_fingers carrey_works carrey
t_5 mary_kate kate_ashley kate ashley mary
t_6 cable_pleasantly sopranos_typecast iman_appearance sopranos appearance_deprecating

# Get the prevalence of each topic
# You can make this discrete by applying a threshold, say 0.05, for
# topics in/out of docuemnts. 
lsa_model$prevalence <- colSums(lsa_model$theta) / sum(lsa_model$theta) * 100

# textmineR has a naive topic labeling tool based on probable bigrams
lsa_model$labels <- LabelTopics(assignments = lsa_model$theta > 0.05, 
                            dtm = dtm,
                            M = 1)
t_1 mary_kate
t_2 lot_movie
t_3 read_years
t_4 weird_scare
t_5 mary_kate
t_6 mind_control

# put them together, with coherence into a summary table
lsa_model$summary <- data.frame(topic = rownames(lsa_model$phi),
                            label = lsa_model$labels,
                            coherence = round(lsa_model$coherence, 3),
                            prevalence = round(lsa_model$prevalence,3),
                            top_terms = apply(lsa_model$top_terms, 2, function(x){
                              paste(x, collapse = ", ")
                            stringsAsFactors = FALSE)
lsa_model$summary[ order(lsa_model$summary$prevalence, decreasing = TRUE) , ][ 1:10 , ]
Summary of 10 most prevalent LSA topics
topic label_1 coherence prevalence top_terms
t_5 t_5 mary_kate 0.792 88.732 mary_kate, kate_ashley, kate, ashley, mary
t_6 t_6 mind_control 0.998 74.258 cable_pleasantly, sopranos_typecast, iman_appearance, sopranos, appearance_deprecating
t_7 t_7 mind_control 0.145 71.431 br, movie_neat, mind_control, movie, movies
t_10 t_10 special_effects 0.107 52.044 br, series, effects, rider_days, story_confused
t_2 t_2 lot_movie 0.998 49.018 read_years, allende, isabel, author_movie, isabel_allende
t_16 t_16 years_ago 0.352 32.829 sequels, br, popular_watch, excellent_special, sequels_excellent
t_9 t_9 michael_jackson 0.757 24.992 movie_neat, neat, criminal_dancing, song_play, imagination_mention
t_1 t_1 mary_kate 0.757 24.449 colombo, listed, playing_caracter, colombo_made, listed_movie
t_31 t_31 good_movie 0.998 21.220 lightweight, script_bit, camp_late, lightweight_movie, existed_appeal
t_11 t_11 warning_handle 0.998 20.088 warning_handle, recommend_warning, stay_telling, film_tape, bar_laughed

One key mathematical difference is how you calculate \(\Phi'\). For LSA the operation is

\[\begin{align} \Phi' &= (S\cdot\Phi)^{-1} \end{align}\]
# Get topic predictions for all 5,000 documents

# first get a prediction matrix,
lsa_model$phi_prime <- diag(lsa_model$sv) %*% lsa_model$phi

lsa_model$phi_prime <- t(MASS::ginv(lsa_model$phi_prime))

# set up the assignments matrix and a simple dot product gives us predictions
lsa_assignments <- t(dtm) * tf_sample$idf

lsa_assignments <- t(lsa_assignments)

lsa_assignments <- lsa_assignments %*% t(lsa_model$phi_prime)

lsa_assignments <- as.matrix(lsa_assignments) # convert to regular R dense matrix

In this case, there is no Bayesian/frequentist difference. So predictions are identical with both methods.

# compare the "fit" assignments to the predicted ones
barplot(rbind(lsa_model$theta[ rownames(dtm)[ 1 ] , ],
              lsa_assignments[ rownames(dtm)[ 1 ] , ]), 
        las = 2,
        main = "Comparing topic assignments in LSA",
        beside = TRUE,
        col = c("red", "blue"))

       legend = c("During fitting", "Predicted"),
       fill = c("red", "blue"))

Other topic models

As of this writing, textmineR has implementations of

A future version of textmineR will have an implementation of a strucural topic model from the stm package.

All of the above have nearly identical syntax and workflows as detailed above.


Document clustering is just a special topic model

Document clustering can be thought of as a topic model where each document contains exactly one topic. textmineR’s Cluster2TopicModel function allows you to take a clustering solution and a document term matrix and turn it into a probabilistic topic model representation. You can use many of textmineR’s topic model utilities to evaluate your clusters (e.g. R-squared, coherence, labels, etc.)

Choosing the number of topics

There is no commonly accepted way to choose the number of topics in a topic model. Fear not! Probabilistic coherence can help you. In forthcoming research, I show that probabilistic coherence can find the correct number of topics on a simulated corpus where the number of topics is known beforehand. (This will be part of a PhD dissertation, sometime around 2021. Stand by!)

Users can implement this procedure. Simply fit several topic models across a range of topics. Then calculate the probabilistic coherence for each topic in each model. Finally, average the probabilistic coherence across all topics in a model. This is similar to using the silhouette coefficient to select the number of clusters when clustering.

Some example code (on a trivially small dataset packaged with textmineR) is below.

# load a sample DTM

# choose a range of k 
# - here, the range runs into the corpus size. Not recommended for large corpora!
k_list <- seq(5, 95, by = 5)

# you may want toset up a temporary directory to store fit models so you get 
# partial results if the process fails or times out. This is a trivial example, 
# but with a decent sized corpus, the procedure can take hours or days, 
# depending on the size of the data and complexity of the model.
# I suggest using the digest package to create a hash so that it's obvious this 
# is a temporary directory
model_dir <- paste0("models_", digest::digest(colnames(nih_sample_dtm), algo = "sha1"))

# Fit a bunch of LDA models
# even on this trivial corpus, it will a bit of time to fit all of these models
model_list <- TmParallelApply(X = k_list, FUN = function(k){

  m <- FitLdaModel(dtm = nih_sample_dtm, 
                   k = k, 
                   iterations = 200, 
                   cpus = 1)
  m$k <- k
  m$coherence <- CalcProbCoherence(phi = m$phi, 
                                   dtm = nih_sample_dtm, 
                                   M = 5)
}, export=c("nih_sample_dtm"), # export only needed for Windows machines
cpus = 2) 

# Get average coherence for each model
coherence_mat <- data.frame(k = sapply(model_list, function(x) nrow(x$phi)), 
                            coherence = sapply(model_list, function(x) mean(x$coherence)), 
                            stringsAsFactors = FALSE)

# Plot the result
# On larger (~1,000 or greater documents) corpora, you will usually get a clear peak
plot(coherence_mat, type = "o")

Using topic models from other packages

Topic models from other packages can be used with textmineR. The workflow would look something like this:

  1. Use CreateDtm to create a curated DTM
  2. Use Dtm2Docs to re-create a text vector of curated tokens from your DTM
  3. Fit a topic model using your desired package (for example, mallet)
  4. Format the raw output to have two matrices, phi and theta as above
  5. Use textmineR’s suite of utility functions with your model