sts: An R Package for the Structural Topic and Sentiment-Discourse Model

Authors: Shawn Mankad and Li Chen

Please email all comments/questions to smankad [AT] ncsu.edu

CRAN Version

Summary

This repository will host the development version of the package. It is also available on CRAN. It estimates the Structural Topic and Sentiment-Discourse Model that was developed in Chen and Mankad (2025).

The package currently includes functionality to:

As of version 1.6, sts no longer requires the stm package: the initialization routines and text-preparation helpers that sts previously borrowed from stm are now included in sts itself.

Quick Start

library(sts)
temp <- textProcessor(documents = gadarian$open.ended.response, metadata = gadarian)
out <- prepDocuments(temp$documents, temp$vocab, temp$meta)
out$meta$noTreatment <- ifelse(out$meta$treatment == 1, -1, 1)
fit <- sts(~ treatment*pid_rep, ~ noTreatment, out, K = 3)
printTopWords(fit)

Installation Instructions

Assuming you already have R installed (if not see http://www.r-project.org/), to install the CRAN version, simply use:

install.packages("sts")

You can install the most recent development version using the devtools package. First you have to install devtools using the following code. Note that you only have to do this once

if(!require(devtools)) install.packages("devtools")

Then you can load the package and use the function install_github

library(devtools)
install_github("mankad-research/sts_r",dependencies=TRUE)

Note that this will install all the packages suggested and required to run the sts package. It may take a few minutes the first time, but this only needs to be done on the first use. In the future you can update to the most recent development version using the same code.

Acknowledgements

We want to thank our research assistant Nala Peng for her help in translating our original R code to C++.

Our work, both the implementation and the theoretical development, builds on the foundational stm package by Margaret Roberts, Brandon Stewart and Dustin Tingley. Portions of their MIT-licensed code are adapted in sts (see inst/COPYRIGHTS).