RTextTools bundles a host of functions for performing supervised learning on your data, but what about other methods like latent Dirichlet allocation? With some help from the topicmodels package, we can get started with LDA in just five steps. Text in green can be executed within R.

Step 1: Install RTextTools + topicmodels
We begin by installing and loading RTextTools and the topicmodels package into our R workspace.


Step 2: Load the Data
In this example, we will be using the bundled NYTimes dataset compiled by Amber E. Boydstun. This dataset contains headlines from front-page NYTimes articles. We will take a random sample of 1000 articles for the purposes of this tutorial.

data <- NYTimes[sample(1:3100,size=1000,replace=FALSE),]

Step 3: Create a DocumentTermMatrix
Using the create_matrix() function in RTextTools, we'll create a DocumentTermMatrix for use in the LDA() function from package topicmodels. Our text data consists of the Title and Subject columns of the NYTimes data. We will be removing numbers, stemming words, and weighting the DocumentTermMatrix by term frequency.

matrix <- create_matrix(cbind(as.vector(data$Title),as.vector(data$Subject)), language="english", removeNumbers=TRUE, stemWords=TRUE, weighting=weightTf)

Step 4: Perform Latent Dirichlet Allocation
First we want to determine the number of topics in our data. In the case of the NYTimes dataset, the data have already been classified as a training set for supervised learning algorithms. Therefore, we can use the unique() function to determine the number of unique topic categories (k) in our data. Next, we use our matrix and this k value to generate the LDA model.

k <- length(unique(data$Topic.Code))
lda <- LDA(matrix, k)

Step 5: View the Results
Last, we can view the results by most likely term per topic, or most likely topic per document.

Topic 1  "campaign"  Topic 2  "kill"      Topic 3  "elect"     Topic 4  "china"     Topic 5  "govern"    Topic 6  "fight" Topic 7  "leader"    Topic 8  "york"      Topic 9  "isra"      Topic 10 "win"       Topic 11 "report"    Topic 12 "plan"
Topic 13 "republican"Topic 14 "aid"       Topic 15 "set"       Topic 16 "clinton"   Topic 17 "nation"    Topic 18 "hous"
Topic 19 "iraq"      Topic 20 "bush"      Topic 21 "citi"      Topic 22 "rais"      Topic 23 "overview"  Topic 24 "money"
Topic 25 "basebal"   Topic 26 "court"     Topic 27 "war"

Output too long to display here. Try it out for yourself to see what it looks like!
10/17/2011 10:13:11

Thanks for the helpful example. I've only used SVD and had been wanting to try LDA.

Bob Muenchen

Eric Brown
2/10/2012 10:46:43

I tried to run this code, but when I got to the LDA command:

lda <- LDA(matrix, k)

Error in LDA(matrix, k) :
Each row of the input matrix needs to contain at least one non-zero entry

3/27/2012 11:13:15

I'm having the same problem. Any thoughts?

3/27/2012 14:47:48

Hi Brian,

The topicmodels packages has changed since this demo was written. I will need to go back and see what's going on.


3/5/2013 19:32:31

here is a way to remove those empty rows (though this may not actually address the underlying issue):


Tim Jurka
2/10/2012 21:09:47

Hi Eric, thanks for pointing this out. It looks like package topicmodels has been updated since our script was originally written. I'll look into how to fix it.

4/21/2012 18:42:36

Works Perfect for me. Thanks load to Tim and all others in the development Team.

8/2/2012 14:02:06

Hi - I've started using this tool and have a metadata question. The DTM preserves the names of the articles I put in, but in the LDA process the articles simply become row numbers. Do you know how to reference back to find out which article is which row number? I need to sort the data within the topics and can't do it without their names...

9/28/2012 05:20:11

First of all, great work, guys, and a big thank you!

I'd just like to know if there are any news on the issue reported by Eric?

Error in LDA(matrix, k) :
Each row of the input matrix needs to contain at least one non-zero entry

Thanks a lot!

12/2/2012 04:40:21

Hi Dan,

The problem should be fixed now.


3/28/2015 23:24:06

Hi Tim,
I tried to run your code but there was an error when you try to create a matrix:

Error in create_matrix(cbind(as.vector(data$Title), as.vector(data$Subject)), :
object 'weightTf' not found

Any ideas?

Antony Stevens
10/8/2012 16:07:13

many thanks for your helpful example

Big Mike
12/14/2012 18:14:46

Are the topics as return by 'terms(lda)' represented by just the most frequent term? Is there a way to find all the terms in the topic?

Big Mike
12/14/2012 18:17:41

Sorry, that was an "RTFM" question. Never mind.

12/16/2012 21:31:39

I am now getting the "Each row of the input matrix needs to contain at least one non-zero entry" error. Did the fix work for the rest of you guys?

4/9/2013 03:37:25

This looks very cool, but it seems the RTextTools package has been taken of CRAN, so I can't load it. Do you know what the issue is?

7/19/2013 12:31:01

Great tutorial thanks. My one question is whether the weightings of words are case sensitive? For example, would 'Hello' have one weight and 'hello' another? Or does the weighting calculation just see these as the same word?

9/27/2013 21:58:59

The code and explanation were spot on. Thanks!

10/28/2013 01:21:12

Why do you still need to run LDA if the topics are already classified?

Tom R
3/5/2015 14:48:30

Thanks a lot for posting this. I was unaware of RTextTools, it makes for much easier creation of TDM matrices than qdap or tm (which is what I was using). This worked perfectly for me.


Your comment will be posted after it is approved.

Leave a Reply.