# A tibble: 6 × 2
line_id word
<int> <chr>
1 1 life's
2 1 but
3 1 a
4 1 walking
5 1 shadow
6 1 a
From Documents to Discoveries
You work at an investment firm. Your task:
Review 500 quarterly reports and analyst reactions from companies in your portfolio. Identify emerging themes. Flag concerns. Recommend adjustments.
You cannot read 500 reports carefully. What do you do?
Given a collection of documents (a corpus), we might ask:
Statistical text analysis:
This chapter focuses on these foundational methods.
Large Language Models:
The Goal of This Chapter
Build intuition about text-as-data so you can ask better questions and evaluate answers critically—whether from classical methods or LLMs.
To analyze text computationally, we must convert it to numbers.
Step 1: Tokenization — Break text into units (usually words)
Step 2: Vocabulary — Identify the set of unique terms
Step 3: Counting — Record which terms appear in which documents
The result: a term-document matrix (or document-term matrix).
# A tibble: 6 × 2
line_id word
<int> <chr>
1 1 life's
2 1 but
3 1 a
4 1 walking
5 1 shadow
6 1 a
The soliloquy yields 38 word tokens in total.
Most frequent words:
| word | n |
|---|---|
| a | 3 |
| and | 3 |
| is | 2 |
| an | 1 |
| but | 1 |
| by | 1 |
| frets | 1 |
| full | 1 |
“A” and “is” tell us nothing about Macbeth. These are stop words—common words that carry little information.
| word | n |
|---|---|
| frets | 1 |
| fury | 1 |
| heard | 1 |
| hour | 1 |
| idiot | 1 |
| life’s | 1 |
Now we see content words: shadow, player, stage, tale, sound, fury…
For a corpus of \(D\) documents with vocabulary of \(V\) terms:
\[ \underset{V \times D}{\mathbf{M}} = \begin{pmatrix} m_{1,1} & m_{1,2} & \cdots & m_{1,D} \\ m_{2,1} & m_{2,2} & \cdots & m_{2,D} \\ \vdots & \vdots & \ddots & \vdots \\ m_{V,1} & m_{V,2} & \cdots & m_{V,D} \end{pmatrix} \]
where \(m_{v,d}\) = count of term \(v\) in document \(d\).
Key property: This matrix is extremely sparse. Most entries are zero.
Text is Just Another Feature Matrix
The term-document matrix is an \(n \times d\) matrix where:
Everything we learned about high-dimensional data applies:
Term-document matrices are extremely sparse
Typical corpora: 99% zeros. Most words don’t appear in most documents.
If we rank words by frequency across a corpus, we get:
the, and, to, of, a, in, that, is, was, for…
These words appear in every document. They don’t distinguish anything.
We need a measure that asks:
Which words are distinctive to this document?
Term frequency measures how often a term appears in a document:
\[ \text{tf}(t, d) = \frac{\text{count of term } t \text{ in document } d}{\text{total terms in document } d} \]
High tf means the term is prominent in this document.
But “the” has high tf in every document—not distinctive.
Inverse document frequency measures how rare a term is across the corpus:
\[ \text{idf}(t) = \log \frac{\text{total documents}}{\text{documents containing term } t} \]
IDF downweights common terms, upweights rare ones.
\[ \text{tf-idf}(t, d) = \text{tf}(t, d) \times \text{idf}(t) \]
TF-IDF Intuition
A term has high tf-idf if it is:
This surfaces distinctive words—the words that make this document different.
Figure 1: Top tf-idf words for each Jane Austen novel
Each novel’s distinctive words are character names:
TF-IDF automatically surfaces what makes each document unique.
For Your Investment Analyst
TF-IDF on quarterly reports would surface: company-specific terms, unusual product names, emerging risk language…
TF-IDF tells us which words are distinctive.
But words cluster into themes:
Topic models discover these latent themes automatically.
Two Key Assumptions
Given a corpus, the algorithm discovers both: - What topics exist - Which topics each document expresses
The most common topic model is Latent Dirichlet Allocation.
Yes—the other LDA! (Recall Chapter 9’s Linear Discriminant Analysis.)
The name:
Imagine each document was generated as follows:
LDA inverts this process: given documents, infer the topics.
2,246 Associated Press articles, fit with \(K = 2\) topics:
Figure 2: Top words in each topic (AP articles, K=2)
Topic 1: percent, million, billion, company, market…
→ Business/Finance
Topic 2: president, government, party, members, congress…
→ Politics/Government
The algorithm knew nothing about these categories—it discovered them from word co-occurrence patterns.
Like \(k\)-means clustering, you must choose \(K\).
Too few topics: Themes are too broad, mix unrelated content
Too many topics: Themes are too narrow, hard to interpret
Practical approaches:
LDA also tells you each document’s topic composition:
| document | Topic 1 (Business) | Topic 2 (Politics) |
|---|---|---|
| Article 127 | 0.89 | 0.11 |
| Article 203 | 0.23 | 0.77 |
| Article 891 | 0.52 | 0.48 |
Large Language Models excel at tasks that once required topic models:
Why learn classical methods at all?
Use Classical Methods When:
Classical methods are often a first pass—filter, cluster, and then use LLMs on selected subsets.
Even if you use LLMs for analysis, understanding text-as-data helps you:
Text EDA workflow combining classical and LLM methods
Term frequency: \[\text{tf}(t, d) = \frac{n_{t,d}}{\sum_t n_{t,d}}\]
Inverse document frequency: \[\text{idf}(t) = \log \frac{|D|}{|\{d : t \in d\}|}\]
TF-IDF: \[\text{tf-idf}(t, d) = \text{tf}(t, d) \times \text{idf}(t)\]
| Part 1–2 Concept | Text Application |
|---|---|
| Feature matrix | Term-document matrix |
| Curse of dimensionality | Vocabulary size (10,000+ terms) |
| PCA | Topic models reduce dimensions |
| Clustering | Documents cluster by theme |
| Information theory | IDF relates to entropy/surprisal |
A Name Collision Resolved
Chapter 9: Linear Discriminant Analysis — supervised, finds directions that separate classes
Chapter 10: Latent Dirichlet Allocation — unsupervised, finds topics in text
Both involve “allocation” to categories. Context makes the meaning clear.
Using the Jane Austen tf-idf results:
For the AP news articles:
Your firm has 10,000 customer reviews of a new product.
Return to the quarterly report scenario:
Text Mining with R: A Tidy Approach — Silge & Robinson
Latent Dirichlet Allocation — Blei, Ng, & Jordan (2003)
tidytext package — R tools for tidy text analysis
| Function | Package | Purpose |
|---|---|---|
unnest_tokens() |
tidytext | Tokenize text |
bind_tf_idf() |
tidytext | Compute tf-idf |
LDA() |
topicmodels | Fit topic model |
tidy() |
tidytext | Convert to tidy format |
dfm() |
quanteda | Document-feature matrix |