-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_BERTopic.Rmd
163 lines (138 loc) · 4.36 KB
/
example_BERTopic.Rmd
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
title: "R Notebook"
output: html_notebook
---
# ml_word_visualisations
## Structure of repository
Folder [lda](./lda) consists of the files to run lda experiments
1. [utils.R](./lda/utils.R) which includes functions for testing lda topics
2. [main.R](./lda/main.R) which includes methods for creating and testing lda models from different libraries
Folder [data](./data) is there to store your data. <br>
Folder [results](./results) stores the results of your experiment
## Example
### LDA
#### 0. Preparation
#### 0.1 Set (hyper)parameters
**Data**
```{r}
data <- read_csv("./data/depression_anxiety_final_2.csv")
data_col <- "dep_text"
group_var <- NULL # now necessary, but only used for t-test
cor_var <- "PHQtot"
```
**BERTopic**
```{r}
model_type <- "bert_topic" # or "mallet"
data_col <- "dep_text"
embedding_model <- "distilroberta"
umap_model <- "default"
hdbscan_model <- "default"
vectorizer_model <- "default"
representation_model <- "default"
num_top_words <- 10
stop_words = "english"
n_gram_window <- c(1,3)
min_df <- 5
reduce_frequent_words <- TRUE
bm25_weighting <- TRUE
seed=1234
```
**Analysis**
```{r}
cor_var <- "PHQtot" # grouping variable for t-test, to be predicted variable for other
control_vars <- c("PHQtot")#, "GADtot") # vector of variables to control analysis with if test_method is linear_regression
test_method <- "textTrain_regression" # linear_regression, logistic_regression, t-test
```
**Miscellaneous**
```{r}
seed <- 1234
```
##### 0.2 Create directory to save all computations
All objects created within the pipeline are created in the directory below. These include
- Document Term Matrix
- model
- predictions
- analysis results
```{r}
save_dir <- paste0("./results/",
model_type,"_",
data_col, "_",
"embed_", embedding_model,
"_min_df_", min_df,
"_bm25_weighting_", bm25_weighting)#,)
if (!dir.exists("./results")) {
dir.create("./results")
}
```
##### 0.3 Imports
```{r}
library(textmineR)
library(tidyverse)
library(dplyr)
library(textmineR)
library(mallet)
library(rJava)
library(tokenizers)
library(text2vec)
library(quanteda)
source("./topic_modeling/lda/main.R")
source("./topic_modeling/lda/wordclouds.R")
```
#### 1 Create bert topic model
```{r}
#data <- data.frame(read_csv("./data/suicide_test.csv"))
bertopic <- get_bertopic_model(data=data,
data_var="dep_text",
embedding_model=embedding_model,
umap_model=umap_model,
hdbscan_model=hdbscan_model,
vectorizer_model=vectorizer_model,
representation_model=representation_model,
num_top_words=num_top_words,
n_gram_range=n_gram_window,
min_df=min_df,
bm25_weighting=bm25_weighting,
reduce_frequent_words=reduce_frequent_words,
stop_words=stop_words,
save_dir=save_dir)
```
```{r}
data2 <- read_csv("./data/depression_anxiety_final_2.csv")
view(data2)
```
#### 2. Analysis
```{r}
test <- get_lda_test(model=bertopic$model,
preds=bertopic$preds,
data=bertopic$train_data,
group_var = "PHQtot",
control_vars = c("PHQtot"),
test_method = "textTrain_regression",
seed=seed,
save_dir=save_dir)
```
```{r}
test <- get_lda_test(model=bertopic$model,
preds=bertopic$preds,
data= bertopic$train_data,
group_var = "PHQtot",
control_vars = c("PHQtot"),
test_method = "linear_regression",
seed=seed,
save_dir=save_dir)
view(test)
```
```{r}
plot_wordclouds(model = model,
model_type = "bert_topic",
test = test,
test_type = "linear_regression",
cor_var = "PHQtot",
plot_topics_idx = NULL,
p_threshold = 0.05,
scale_size=FALSE,
color_negative_cor = scale_color_gradient(low = "darkgreen", high = "green"),
color_positive_cor = scale_color_gradient(low = "darkred", high = "red"),
save_dir=save_dir,
seed=seed)
```