-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpubmed_search_3.Rmd
330 lines (222 loc) · 8.01 KB
/
pubmed_search_3.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
---
title: 'Pubmed2: a workflow for extracting and analysing Pubmed abstracts'
output:
slidy_presentation:
html_notebook:
number_sections: yes
toc: yes
toc_float: yes
word_document:
toc: yes
html_document:
toc: yes
params:
search_text: "data science + public health[mh]"
---
```{r setup, include=FALSE, echo=FALSE}
knitr::opts_chunk$set(cache = TRUE, echo = FALSE, warning = FALSE, message = FALSE)
```
# Motivation
This presentation was inspired by a recent meeting to discuss public health data science. It outlines a method for rapidly searching the Pubmed database and analysing retrieved abstracts. We are interested in rapidly assessing the literature on the extent of discussion or application of data science in public health research and practice.
# R for literature searching
* Lots of packages
* `RISmed` used here <https://cran.r-project.org/web/packages/RISmed/RISmed.pdf>
* Others include `rentrez`, `pubmed.mineR`,
* [R Open Science](https://ropensci.org/packages/) - lots of packages e.g.
+ `aRxiv`
+ `fulltext`
+ `rcrossref`
+ `rplos`
+ `rspringer`
# Method
We used the `RISmed` package which provides an `R` interface to the Pubmed API to extract and analyse the most recent abstracts retrieved via a very non-specific search strategy. We used cluster analysis (topic modelling) to group and classify abstracts. We also searched the abstracts for the terms *data science* and *big data* and conclude there is currently a very small literature on data science in public/ population health and there is a potential large research agenda to help us understand how we can apply emerging data management and analytic techniques in modern public health practice.
# Load libraries
Libraries used
* `tidyverse`
* `RISmed`
* `wordcloud`
* `tidytext`
* `tm`
* `topicmodels`
```{r load libraries, include=FALSE}
library(tidyverse)
library(stringr)
library(RISmed)
library(wordcloud)
library(tidytext)
library(tm)
library(topicmodels)
suppressPackageStartupMessages(library(viridis))
library(glue)
if(!require(text2vec))install.packages("text2vec")
library(text2vec)
library(LDAvis)
```
# Send search
Next, we'll query the Pubmed API via the RISmed package. We'll use a broad search strategy.
```{r run search}
search_term <- params$search_text
## search term = data science + public health[mh]
res1 <- EUtilsSummary(search_term,
type = "esearch",
db = "pubmed",
datetype = "pdat",
maxdate = 2017,
retmax = 10000
)
```
The query sent to Pubmed is `r res1@querytranslation` which retrieves `r formatC(res1@count, format = "d")` Pubmed entries. The query string shows that data science is not a current MESH heading.
I have restricted the downloads in the interests of time and limitations on the Pubmed API.
# Retrieve results
```{r retrieve pubmed entries, cache= TRUE}
res1@count
res1@querytranslation %>%
knitr::asis_output()
fetch <- EUtilsGet(res1, type = "efetch", db = "pubmed")
# auth <-Author(EUtilsGet(res1))
#
# typeof(auth)
#
# Last <-sapply(auth, function(x) paste(x$LastName))
```
# Create data frame
```{r extract fields and convert to data frame}
abstracts <- data.frame(title = fetch@ArticleTitle,
abstract = fetch@AbstractText,
journal = fetch@Title,
DOI = fetch@PMID,
year = fetch@YearPubmed)
## ensure abstracts are character fields (not factors)
abstracts <- abstracts %>% mutate(abstract = as.character(abstract))
head(abstracts) %>%
knitr::kable()
```
# Plot results over time
```{r plot article count per year}
abstracts %>%
group_by(year) %>%
tally() %>%
ggplot(aes(year, n)) +
geom_col() +
labs(title = paste("Pubmed articles with search terms", search_term), hjust = 0.5,
y = "Articles")
```
The total number of abstracts was `r nrow(abstracts)`.
# Word cloud
Using the `tidytext` framework makes it easy to create wordclouds.
```{r wordcloud, fig.height=6, fig.width=6}
library(stringr)
abs <- tolower(abstracts$abstract)
abs <- str_replace_all(abs, "[^[:alnum:]]", " ")
abs <- str_replace_all(abs, "\\s+", " ")
abs1 <- data.frame(cbind(id = abstracts$DOI, abs = as.character(abs))) %>%
mutate(abs = as.character(abs))
```
```{r}
cloud <- abstracts %>%
unnest_tokens(word, abstract) %>%
anti_join(stop_words) %>%
dplyr::count(word, sort = TRUE)
cloud %>%
with(wordcloud(word, n, min.freq = 10, max.words = "Inf", colors = brewer.pal(8, "Dark2")), scale = c(8,.3), per.rot = 0.4)
```
# Create bigrams
Bigrams - two word tokens - may provide more insight into the content of abstracts
```{r extract and plot bigrams, message=FALSE, warning=FALSE}
ds_bigrams <- abstracts %>%
unnest_tokens(ngram, abstract, token = "ngrams", n=2) %>%
dplyr::count(ngram, sort = TRUE)
bigrams_separated <- ds_bigrams %>%
separate(ngram, c("word1", "word2"), sep = " ")
bigrams_filtered <- bigrams_separated %>%
filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word)
bigrams_filtered
bigrams_united <- bigrams_filtered %>%
mutate(bigram = paste(word1, word2, sep = " ") )
bigrams_united %>%
filter(n >500) %>%
ggplot(aes(reorder(bigram, n), n)) +
geom_point() +
coord_flip() +
theme(axis.text.x = element_text(size = 10)) +
labs(title = "Bigrams with frequency count > 25", x = "")
```
# Bigram wordcloud
```{r bigram wordcloud, fig.height=6, fig.width=6}
bigrams_united %>%
with(wordcloud(bigram, n, max.words = 1000, random.order = FALSE, colors = brewer.pal(9, "Set1"), scale = c(8, 0.3)), per.rot = 0.4)
```
# Journal wordlcoud
```{r, fig.height=6, fig.width=6}
cloud3 <- abstracts %>%
select(journal) %>%
group_by(journal) %>%
tally(sort = TRUE)
cloud3 %>%
with(wordcloud(journal, n, min.freq = 10, random.order = FALSE, max.words = 80, colors = brewer.pal(9, "Set1")), rot.per = .6)
```
# Extract abstracts containing the phrase 'data science or big data'
```{r abstract which mention data science}
g <- abstracts[grepl("data science| big data| social media", abstracts$abstract),]
g %>%
knitr::kable()
#
g1 <- g$DOI %>% list
abstracts <- abstracts %>%
mutate(DOI = as.character(DOI))
g2 <- abstracts[abstracts$DOI %in% g1[[1]],] %>%
select(title, abstract, journal, DOI)
```
# Topic models
```{r}
g2$abstract <- tolower(g2$abstract)
g2$abstract <- stripWhitespace(g2$abstract)
g2$abstract <- removePunctuation(g2$abstract)
g2$abstract <- removeNumbers(g2$abstract)
## bigrams
textPubs <- g2 %>%
group_by(DOI) %>%
unnest_tokens(bigram, title, token = "ngrams", n = 2) %>%
dplyr::count(DOI, bigram)
pubs_sep1 <- textPubs %>%
separate(bigram, c("word1", "word2"), sep = " ")
## and filter out the 'stop words'
pubs_filt1 <- pubs_sep1 %>%
filter(!word1 %in% stop_words$word) %>%
filter(!word2 %in% stop_words$word)
## and recombine
pubs_new1 <- pubs_filt1 %>%
mutate(bigram = paste(word1, word2, sep = " "))
## dtm
corp_dtm <- pubs_new1 %>%
cast_dtm(DOI, bigram, n)
corp_lda <- LDA(corp_dtm, k = 5, control = list(seed = 1234))
corp_tidy_lda <- tidy(corp_lda)
corp_tidy_lda %>%
group_by(topic) %>%
top_n(1000, beta) %>%
arrange(topic, -beta) %>%
ggplot(aes(term, beta, fill = factor(topic), label = term)) +
geom_bar(stat = "identity") +
geom_text(hjust = -0.2, size = 2, check_overlap = TRUE) +
facet_wrap(~topic, ncol =3) +
coord_polar() +
theme_bw()+
labs(fill = "Topic", y = "" , x = "") +
expand_limits(y = c(0, .1)) +
theme(legend.position = "", axis.text.y = element_blank(), axis.ticks = element_blank(), axis.text.x = element_blank())
```
# Classifying documents
```{r document classification}
lda_gamma <- tidytext:::tidy.LDA(corp_lda, matrix = "gamma")
abs_class <- lda_gamma %>%
group_by(document) %>%
top_n(1, gamma) %>%
ungroup() %>%
arrange(gamma)
abs_class %>%
left_join(g2, by = c("document" = "DOI")) %>%
select(-gamma, -abstract, -journal) %>%
DT::datatable(filter = "top" )
```