0
$\begingroup$

Using the data_corpus-inaugural from Quanteda I want to show how the usage of certain topics by Democrats VS Republicans has changed over time (since 1900). I did the stm topic modeling but have no idea how to do and visualize it in comparison for the two parties over time. Please help me if you are so kind.

##
corp = corpus_reshape(data_corpus_inaugural, to = "paragraphs")

dfm_speeches <- corp %>%

corpus_subset(Year > 1900) %>%

tokens(remove_punct = TRUE) %>%

tokens_remove(stopwords("english")) %>%

tokens_remove(phrase(c("can", "say","one", "way", "use", "also", "let",
                       "however", "tell", "will", "much", "need", "take",
                       "tend", "even", "like", "last", "never", "brief", "bit", "entire",
                       "great", "lot", "still", "must", "new", "every", "enough", "want",
                       "attempt", "large", "yes", "no", "may", "like", "particular", "rather",
                       "said", "get", "well", "make", "ask", "come", "end", "first", "two", "help", "often",
                       "might", "see", "something", "point", "look", "right", "sure", "kind", "lack", "aware",
                       "found", "ever", "shall", "thing", "upon", "know", "day"))) %>%

tokens_replace(phrase(c("United Staates of America", "United States", "America",
                        "U.S.A", "USA")), phrase(c("USA", "USA", "USA", "USA", "USA")), case_insensitive = TRUE) %>%

tokens_replace(phrase(c("U.S.", "US")), phrase(c("USA", "USA")), case_insensitive = FALSE) %>% 

dfm() 


dfm_wordstem(dfm_speeches) 

#stm + visualisierung
if (require("stm")) {
my_lda_speeches <- stm(dfm_speeches, K = 10, verbose = FALSE)
plot(my_lda_speeches)    
}

#try to visualize stm
view(dfm_speeches)
dfm_speeches$meta <- as.factor(dfm_speeches$meta)
dfm_speeches$Party <- as.factor(dfm_speeches$Party)
dfm_speeches$Year <- as.factor(dfm_speeches$Year)

prep <- estimateEffect(formula = 1:20 ~ dfm_speeches$Party + dfm_speeches$Year, 
                     stmobj = my_lda_speeches, meta = dfm_speeches$meta, uncertainty = "Global")

plot(prep, covariate ="dfm_speeches$Party", topics = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
   model = my_lda_speeches, method = "difference", cov.value1 = "Democrats",
cov.value2 = "Republican",
xlab = "More Republican ... More Democratic",
main = "Usage of Topics: Democrats vs. Republicans", xlim = c(-0.1, 0.1),
labeltype = "custom", custom.labels = c("Topic 1", "Topic 2", "Topic 3",
                                      "Topic 4", "Topic 5", "Topic 6",
                                      "Topic 7", "Topic 8", "Topic 9",
                                      "Topic 10")) ```
New contributor
Mara is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
$\endgroup$