ntopics {discursive}R Documentation

Compute number of topics based on stm results

Description

This function takes a structural topic model output estimated via stm::stm() as well as the underlying set of documents created via stm::prepDocuments() to compute the relative number of topics raised in each open-ended response. The function returns a numeric vector of topic counts re-scaled to range from 0 to 1. See Kraft (2023) for details.

Usage

ntopics(x, docs, progress = TRUE)

Arguments

x

A structural topic model estimated via stm::stm().

docs

A set of documents used for the structural topic model; created via stm::prepDocuments().

progress

Logical. Shows progress bar if TRUE.

Value

A numeric vector with the same length as the number of documents in x and docs.

Examples

meta <- c("age", "educ_cont", "pid_cont", "educ_pid", "female")
openends <- c(paste0("oe0", 1:9), "oe10")
cces$resp <- apply(cces[, openends], 1, paste, collapse = " ")
cces <- cces[!apply(cces[, meta], 1, anyNA), ]
processed <- stm::textProcessor(cces$resp, metadata = cces[, meta])
out <- stm::prepDocuments(processed$documents, processed$vocab, processed$meta, lower.thresh = 10)
stm_fit <- stm::stm(out$documents, out$vocab, prevalence = as.matrix(out$meta), K=25, seed=12345)
ntopics(stm_fit, out)

[Package discursive version 0.1.1 Index]