Logo Topic Modeling

Topic Modeling

Cộng đồng
scdenney
topic-modeling

Advises on setting up and validating topic models for survey and experimental text: discovery vs. measurement, STM/LDA/BERTopic choice, preprocessing, covariate formulas, choosing K across coherence, FREX, held-out likelihood, and residuals, validating topics against human coding, robustness, and reporting. Coding against a fixed codebook goes to text-classification.

Tổng quan

Nhà phát hànhscdenney
Kho mãopen-science-skills
Tên kỹ năngtopic-modeling
Sao
69
Bản fork
3
Tệp đi kèm
1
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 1 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được scdenney công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Topic Modeling trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/scdenney/open-science-skills/tree/main/codex/topic-modeling
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/scdenney/open-science-skills.git /tmp/open-science-skills
mkdir -p .claude/skills
cp -r /tmp/open-science-skills/codex/topic-modeling .claude/skills/topic-modeling
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Topic Modeling trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Topic Modeling ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Topic Modeling với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Topic Modeling for Survey and Experimental Text Data

Instructions

Help the user design the analysis before fitting anything. Work through the decisions below in order, explain what each choice changes and which standard governs it, and record the choice with its rationale. Every one of them — model, preprocessing, covariates, K, labels — shapes which topics appear, so each belongs in the methods section.

1. Discovery or Measurement?

Settle this first; it decides the rest.

  • Topic models are discovery tools: they show what respondents discussed. If the user already has categories and wants each document coded against them, that is classification — use $text-classification.
  • If the goal is to infer latent attitudes rather than categorize surface content, topics alone will not do it; complement them with an embeddings-based scaling of contextually common words or a supervised classifier (Hobbs & Green 2025).
  • A common sound design is two-stage: discover categories with a topic model, then write those categories into a codebook and measure them with validated supervised classification for confirmatory tests (Grimmer & Stewart 2013).
  • If topic-prevalence effects will be tested as hypotheses, pre-register them ($pre-registration-writing) so estimateEffect() output is confirmatory rather than exploratory by default (Nosek et al. 2018).

2. Model Choice

  • STM is the default for survey and experimental text. It brings document metadata — treatment arm, demographics, country — into estimation so prevalence and content can vary with covariates (Roberts et al. 2014).
  • LDA fits when no document-level covariates are needed and the corpus is large enough for unsupervised discovery (Blei, Ng & Jordan 2003).
  • BERTopic fits short texts where word co-occurrence is sparse, or multilingual corpora where embedding similarity is needed (Grootendorst 2022). It clusters embeddings with HDBSCAN, so each document gets one hard topic (no mixed membership), c-TF-IDF topic words can be unstable on small corpora, and there is no native covariate framework.

3. Preprocessing

Preprocessing is not neutral: stemming, stopword removal, and frequency thresholds all change which topics emerge (Denny & Spirling 2018). Justify each choice.

  • Lowercase; drop punctuation and numbers unless they carry meaning in the domain.
  • Use a standard stopword list, but inspect it for domain terms that should stay (e.g., "foreign" in immigration research).
  • Stem only after checking that it does not merge substantively distinct terms, and compare results with and without stemming (Denny & Spirling 2018).
  • Prune rare terms with a threshold that scales with the corpus: a fixed 2-5 documents for small open-ended corpora; roughly 0.5-1% of documents for larger ones (Grimmer & Stewart 2013). In stm, plotRemoved() shows terms and documents dropped across candidate thresholds before you pass one to prepDocuments(). Report the threshold and the retained counts.
  • For translated text, preprocess after translation and document the translation pipeline; inconsistent translation can degrade coherence.

4. Model Specification

  • Put theoretically relevant covariates in the prevalence formula — treatment arm for experiments, country for cross-national data: prevalence = ~ treatment + country (Roberts et al. 2014).
  • Add a content formula only when you expect the words of a topic, not just its prevalence, to differ by covariate (different framings across countries or arms). Content covariates parameterize SAGE-style deviations from a baseline, which complicates comparing β across groups; inspect group vocabulary with sageLabels() (Roberts, Stewart & Tingley 2014; 2019).
  • Use spectral initialization (init.type = "Spectral"). It is deterministic on a given machine and BLAS configuration; cross-machine precision can still differ slightly, so record hardware and OS (Roberts, Stewart & Tingley 2019).
  • Set and record a seed regardless of initialization, plus the stm version and R session info.

5. Selecting the Number of Topics

  • Use several diagnostics across a range of K (e.g., 5-30): semantic coherence, exclusivity, held-out likelihood, and residuals (Roberts, Stewart & Tingley 2019). searchK() returns all four; selectModel() fits several initializations at a fixed K and returns the coherence-exclusivity frontier (useful only with non-spectral initialization).
  • Coherence rewards topics whose top words co-occur but favors very common topics (Mimno et al. 2011). For a poor topic, diagnose the failure — chained (two concepts joined by a shared word), intruded (unrelated words mixed in), random, or unbalanced (general and specific terms mixed) — because the diagnosis says whether to change K, preprocessing, or covariates.
  • Exclusivity: stm reports FREX, a weighted harmonic mean of frequency and exclusivity ranks (default ω = 0.7). The coherence-FREX frontier marks models that balance both (Roberts, Stewart & Tingley 2019).
  • Held-out likelihood is one input, not the decider: it can correlate negatively with human interpretability (Chang et al. 2009). When the metrics disagree, prefer interpretability.
  • After narrowing statistically, read top words and representative documents for each candidate. The final K must give substantively meaningful, distinguishable topics.
  • Report the K range, the diagnostics, and the rationale for the final choice.

6. Interpretation and Validation Against Human Judgment

Statistical fit does not show that a topic measures what its label claims. Validate the topics as measures (Grimmer & Stewart 2013).

  • For each topic, report the top 10-15 words by probability and by FREX (Roberts, Stewart & Tingley 2019).
  • Read 3-5 representative documents per topic (findThoughts()). Word lists alone are not enough to interpret a topic.
  • Label topics descriptively ("economic contribution concerns"), from both word lists and documents. Write each label with a one-sentence definition so the labels work as a codebook others can apply.
  • Test the labels against human coders. Have coders blind to the model assign a random sample of documents to the labeled topics (or to none) and report agreement with the model's dominant topic, using Krippendorff's α or κ (Krippendorff 2019; Landis & Koch 1977). Human word-intrusion and topic-intrusion tasks test whether topics are interpretable at all (Chang et al. 2009). Low agreement means the labels or K need revision before any substantive claim.
  • Estimate covariate effects on prevalence with estimateEffect() and plot them with confidence intervals (Roberts et al. 2014). When testing many topics against a treatment, report FDR or similar corrections alongside uncorrected estimates.
  • Guard against spurious treatment effects with permutationTest(), which permutes the treatment label and refits to give a randomization-inference null for each topic's effect (Roberts, Stewart & Tingley 2014; 2019).
  • For construct validation in survey experiments, check whether topics tied to the intended construct rise among respondents exposed to the relevant treatment. A null is suggestive of a construct-validity problem, not proof: first rule out low power, a K that splits or merges the construct topic, a content-covariate effect (the treatment changed how the topic was framed, not whether it was discussed), and preprocessing that dropped construct vocabulary.

7. Robustness

  • Refit at neighboring K (K−2, K+2) and check whether substantive conclusions hold.
  • Compare with and without stemming (Denny & Spirling 2018).
  • For STM, compare with and without covariates to see how much they shape the solution.
  • Check that topics are not driven by one high-frequency term: remove a topic's most common term and see whether its interpretation changes.
  • Inspect topic correlations with topicCorr().

8. Reporting

  • Preprocessing pipeline (stopwords, stemming, thresholds, documents and terms retained after prepDocuments), K and its rationale, initialization, convergence (max EM iterations, final variational bound), seed, stm version, R session info. For the broader checklist see $methods-reporting.
  • A topic summary table: number, label and definition, top FREX words, prevalence overall and by covariate group, and 1-2 example documents.
  • Human validation results: coder procedure, sample size, and agreement with the model's topic assignments.
  • Covariate effects as estimates with confidence intervals, with side-by-side prevalence plots by group.
  • Discovery versus confirmation: say which findings were anticipated and which emerged. Treating every topic comparison as a hypothesis test recreates the researcher-degrees-of-freedom problem (Simmons, Nelson & Simonsohn 2011; Nosek et al. 2018). When topics validate constructs, report whether the intended constructs appeared, not that the model "confirmed" a hypothesis (Grimmer & Stewart 2013).
  • Replication materials: code, preprocessing decisions, seed. If the corpus cannot be shared, provide the topic-term matrix and document-topic proportions (Grimmer & Stewart 2013).

Quality Checks

  • Discovery vs. measurement settled; categories already known routed to $text-classification
  • Model type (STM, LDA, BERTopic) justified by data structure and question
  • Preprocessing choices documented and justified; retained counts reported
  • K chosen from multiple diagnostics (searchK/selectModel) plus interpretability; held-out vs. coherence conflicts resolved toward interpretability (Chang et al. 2009)
  • Prevalence (and, if justified, content) covariates match the design; sageLabels() inspected for content covariates
  • Topics interpreted from probability and FREX words plus findThoughts() documents, and labeled with definitions
  • Topic labels validated against blind human coding with agreement reported (Grimmer & Stewart 2013)
  • Prevalence effects from estimateEffect() with confidence intervals and multiple-comparison adjustment
  • permutationTest() run for experimental treatment effects
  • Robustness: neighboring K, preprocessing variants, covariate structure, topicCorr()
  • Discovery vs. confirmation stated; confirmatory prevalence hypotheses pre-registered where feasible
  • Seed, initialization, convergence, stm version, session info, and replication materials archived

Example

A minimal STM pattern for a survey experiment where df has one row per respondent with open_ended_responses (text) and treatment_arm (factor). Adapt it; stages (a)-(d) map onto Sections 3-6.

r
library(stm)
set.seed(20260418)

# (a) Preprocess and choose the rare-term threshold (Section 3).
processed <- textProcessor(df$open_ended_responses, metadata = df)
plotRemoved(processed$documents, lower.thresh = seq(1, 20, by = 2))
prepped <- prepDocuments(processed$documents, processed$vocab,
                         processed$meta, lower.thresh = 5)

# (b) Sweep K: coherence, exclusivity, held-out likelihood, residuals (Section 5).
k_search <- searchK(prepped$documents, prepped$vocab, K = c(5, 10, 15, 20),
                    prevalence = ~ treatment_arm, data = prepped$meta,
                    init.type = "Spectral")
plot(k_search)

# (c) At the chosen K, compare random initializations on the frontier.
# Spectral init is deterministic, so selectModel needs a random init here.
k_chosen   <- 10
candidates <- selectModel(prepped$documents, prepped$vocab, K = k_chosen,
                          prevalence = ~ treatment_arm, data = prepped$meta,
                          runs = 20, init.type = "LDA")
plotModels(candidates)
fit <- candidates$runout[[1]]

# (d) Treatment effects on prevalence (Section 6); follow with permutationTest and topicCorr.
effects <- estimateEffect(1:k_chosen ~ treatment_arm, fit,
                          metadata = prepped$meta, uncertainty = "Global")
summary(effects)

Tệp đi kèm

Khi kỹ năng đã được tải, mô hình có thể đọc các tệp này nếu cần. Các tệp này chỉ dùng để đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Topic Modeling làm được gì?

Advises on setting up and validating topic models for survey and experimental text: discovery vs. measurement, STM/LDA/BERTopic choice, preprocessing, covariate formulas, choosing K across coherence, FREX, held-out likelihood, and residuals, validating topics against human coding, robustness, and reporting. Coding against a fixed codebook goes to text-classification.

Vì sao nên dùng Topic Modeling trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Topic Modeling là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Topic Modeling trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/scdenney/open-science-skills/tree/main/codex/topic-modeling. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm rồi cài đặt cả gói dưới dạng một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Topic Modeling?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Topic Modeling với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Topic Modeling với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Topic Modeling có miễn phí không?

Kỹ năng này được scdenney phát hành trên GitHub. Hãy xem điều khoản giấy phép trong kho mã. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇