-
Notifications
You must be signed in to change notification settings - Fork 0
/
FP_topicmodel.R
52 lines (41 loc) · 1.09 KB
/
FP_topicmodel.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
library(tidyverse)
library(lubridate)
library(stringr)
library(tidytext)
library(broom)
library(scales)
library(dplyr)
library(ggplot2)
theme_set(theme_bw())
# get text
Livy <- read_csv("All_Livy.csv")%>%
mutate(Book = parse_number(Book))
# convert to tokens
Livy_tokens <- Livy %>%
group_by(Book) %>%
unnest_tokens(word, Text) %>%
anti_join(stop_words)
# convert tokens to count of words in each book
Livy_count <- Livy_tokens %>%
count(Book, word) %>%
na.omit()
# convert to document term matrix
Livy_dtm <- cast_dtm(Livy_count, Book, word, n)
# estimate topic model
library(topicmodels)
livy_tm <- LDA(Livy_dtm, k = 5, control = list(seed = 1234))
# top terms for each topic
livy_tm_td <- tidy(livy_tm)
top_terms <- livy_tm_td %>%
group_by(topic) %>%
top_n(5, beta) %>%
ungroup() %>%
arrange(topic, -beta)
top_terms
top_terms %>%
mutate(term = reorder(term, beta)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
geom_bar(alpha = 0.8, stat = "identity", show.legend = FALSE) +
facet_wrap(~ topic, scales = "free", ncol = 3) +
coord_flip()+
ggsave("LivyTerms.png")