-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphe_blogs.Rmd
401 lines (246 loc) · 9.51 KB
/
phe_blogs.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
---
title: "Text mining PHE's blogs"
output:
html_notebook:
number_sections: yes
toc: yes
toc_float: yes
html_document:
toc: yes
toc_float: yes
editor_options:
chunk_output_type: inline
---
# Introduction
## Text is the new data
PHE produces numerous reports, bulletins, and communications, and receives large amounts of feedback. In recent years the ability to analyse text as data has developed rapidly, and there are now tools which can help us gain insight from documents and bodies of texts. These tools allow us to rapidly analyse large numbers of documents. This note applies some of these techniques to analysing Duncan Selbie's Friday Messages.
There are a number of steps:
* Automated downloading all the bulletins (web-scraping)
* Extracting and cleaning the bulletin text
* Tidying the data to make it analysable
* Applying visualisation techniques like word clouds
* Applying text mining and clustering techniques like topic modelling which can help discover the main themes of the bulletins, and automatically classify them based on these themes.
This analysis is conducted using the statistical package `R` which is rapidly becoming the main tool for undertaking this kind of work.
```{r setup, cache=TRUE}
knitr::opts_chunk$set(echo = FALSE, cache = TRUE, warning = FALSE, message = FALSE)
```
First we need to load the libraries for the analysis.
```{r load libraries}
library(knitr)
suppressPackageStartupMessages(library(rvest))
suppressPackageStartupMessages(library(stringr))
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(httr))
suppressPackageStartupMessages(library(tm))
suppressPackageStartupMessages(library(pdftools))
suppressPackageStartupMessages(library(tidytext))
suppressPackageStartupMessages(library(ggplot2))
library(tidyverse)
library(govstyle)
source("~/themejf.R") ## A standard theme for plots - this should be changed
```
## Downloading messages - web scraping
Then we need to get the data. This process identifies the URLs of the Public Health Matters blog. To identify the parts of the html pages the handy [selectorgadget](http://selectorgadget.com) tool is very useful.
The first step is to extract all the relevant URLs.
```{r Identify URLs, message=FALSE, warning=FALSE, cache=TRUE}
## Scraping bulletins
### This is the main stem of the URLs for Public Health Matters Blogs
url <- "https://publichealthmatters.blog.gov.uk/category/duncan-selbie-friday-message/"
page <- read_html(url)
urls <- page %>%
html_nodes("a") %>% # find all links
html_attr("href") # get the url
urls <- unique(urls[grepl("friday",urls)]) ## Select those which are friday messages
url_comment <- urls[stringr::str_detect(urls, "comments$")]
url_category <- urls[stringr::str_detect(urls, "category")]
urls <- urls[!urls %in% url_comment]
urls <- urls[!urls %in% url_category]
url1 <- "https://publichealthmatters.blog.gov.uk/category/duncan-selbie-friday-message/page/2/"
page1 <- read_html(url1)
urls1 <- page1 %>%
html_nodes("a") %>% # find all links
html_attr("href") # get the url
urls1 <- unique(urls1[grepl("friday",urls1)]) ## Select those which are friday messages
url_comment1 <- urls1[stringr::str_detect(urls1, "comments$")]
url_category1 <- urls1[stringr::str_detect(urls1, "category")]
urls1 <- urls1[!urls1 %in% url_comment1]
urls1 <- urls1[!urls1 %in% url_category1]
url2 <- "https://publichealthmatters.blog.gov.uk/category/duncan-selbie-friday-message/page/3/"
page2 <- read_html(url2)
urls2 <- page2 %>%
html_nodes("a") %>% # find all links
html_attr("href") # get the url
urls2 <- unique(urls2[grepl("friday",urls2)]) ## Select those which are friday messages
url_comment2 <- urls2[stringr::str_detect(urls2, "comments$")]
url_category2 <- urls2[stringr::str_detect(urls2, "category")]
urls2 <- urls2[!urls2 %in% url_comment2]
urls2 <- urls2[!urls2 %in% url_category2]
urls_all <- c(urls, urls1, urls2)
```
Then read the text and clean the data.
```{r Extract text and create data frame, echo=FALSE, message=FALSE, warning=FALSE}
df <- data.frame()
for(url in urls_all){
test1 <- read_html(url) %>%
html_nodes("p")
test2 <- html_text(test1) %>%
str_replace_all("\n", "")
## Remove 1st row
test2 <- test2[2:length(test2)]
## Extract title
title <- test2[1]
## Extract body text
text <- test2[5:length(test2)]
text <- str_c(text, collapse = "")
## Remove repeated text at the end of each blog
test3 <- gsub("With best wishes.*", "", text )
test4 <- data.frame(test3, title)
df <- bind_rows(df, test4)
}
```
We'll extract the date from the title
```{r}
date <- stringr::str_split(df$title, pattern = "/")
year <- map(date, 4)
month <- map(date, 5)
day <- map(date, 6)
date1 <- paste(year, month, day, sep = "-")
df <- cbind(df, date1)
df$date1 <- as.Date(as.character(df$date1))
df <- rename(df,
text = test3)
```
### Text processing
##### Removing 'stop words'
Stop words are common English words which occur frequently in all documents and are generally removed for analytical purposes. In addition, there are words common to all bulletins which add little value in analysis - we'll add these to the `stop_words` lexicon.
```{r}
bulletin_sw <- data_frame(word = c("phe","friday", "dear", "week", "health", "published", "bulletin", "press", "public", "phe's", "www.gov.uk", "news", "publications", "gateway", "formore"), lexicon = "SMART" )
stop_words1 <- bind_rows(stop_words, bulletin_sw)
```
## Visualisation
### Words per document
We can do some simple analysis.
```{r echo=FALSE, fig.height=6, cache=TRUE}
df %>%
group_by(date1) %>%
unnest_tokens(word, text) %>%
count(word, sort = TRUE)%>%
summarise(words = sum(n)) %>%
ggplot(aes(reorder(date1, words), words)) +
geom_col(fill = "blue") +
theme_gov() +
coord_flip() +
labs(x="",
y = "Number of words",
title = "Number of words per blog") +
theme(axis.text.y = element_text(size = 10))
```
### Simple wordcloud
And wordclouds...
```{r echo=FALSE, fig.height=6, fig.width=6, cache=TRUE}
library(wordcloud)
cloud <- df %>%
unnest_tokens(word, text) %>%
anti_join(stop_words1) %>%
count(word, sort = TRUE)
with(cloud, wordcloud(word, n, max.words = 'INF', scale = c(8, 0.2),
rot.per = 0.4, random.order = FALSE,
colors = brewer.pal(8, "Dark2")))
```
```{r echo=FALSE, fig.height=8, fig.width=8}
library(wordcloud2)
library(tidyr)
cloud1 <- df %>%
unnest_tokens(bigram, text, token = "ngrams", n = 2) %>%
count(bigram, sort = TRUE)
## Split the bigrams back into separate words
cloud_sep <- cloud1 %>%
separate(bigram, c("word1", "word2"), sep = " ")
## and filter out the 'stop words'
cloud_filt <- cloud_sep %>%
filter(!word1 %in% stop_words1$word) %>%
filter(!word2 %in% stop_words1$word)
## and recombine
cloud_new <- cloud_filt %>%
unite(bigram, word1, word2, sep = " ")
## and replot the word cloud
wordcloud2(cloud_new, size = 0.6)
```
This shows the main topics or themes of the messages. "Local authorities" is the most commonly mentioned topic, with "suicide prevention", "bowel cancer" and "blood pressure" amongst other common themes.
# Analysis by blog
We can extend the anlaysis further by looking at the distribution of terms in individual bulletins, and then looking for patterns to see if bulletins can be clustered according to content.
First we can look at a single bulletin:
```{r}
df%>%
sample_n(1) %>%
knitr::kable(format = "pandoc")
```
We can then create a per document per term table known as a Document Term Matrix (DTM). We can count the terms per document.
```{r echo=FALSE}
corp_dtm <- df %>%
group_by(date1) %>%
unnest_tokens(word, text) %>%
anti_join(stop_words1) %>%
count(date1, word, sort = TRUE)
```
Next we can create the DTM.
```{r include=FALSE}
corp_dtm <- corp_dtm %>%
cast_dtm(date1, word, n)
```
## Word associations
We can see which words tend to appear together in the bulletins.
```{r associations}
## For example...
### Alcohol
findAssocs(corp_dtm, "alcohol", 0.8)
### Sugar
findAssocs(corp_dtm, "sugar", 0.7)
```
And the next step is topic modelling - this allows us to analyse the whole body of bulletins and look for themes or topics - groupings of words within and between documents.
```{r include=FALSE}
library(topicmodels)
corp_lda <- LDA(corp_dtm, k = 6, control = list(seed = 1234))
corp_lda_tidy <- tidy(corp_lda)
```
```{r, fig.height=12}
corp_topterms <- corp_lda_tidy %>%
group_by(topic) %>%
top_n(15, beta) %>%
ungroup() %>%
arrange(topic, -beta)
corp_topterms %>%
mutate(term = reorder(term, beta)) %>%
mutate(order = row_number()) %>%
ggplot(aes(rev(order), beta, fill = factor(topic), label = term)) +
geom_col(show.legend = FALSE) +
geom_text(hjust = 0, size = 10) +
coord_flip() +
facet_wrap(~topic, ncol =2, scales = "free") +
theme_gov() +
labs(fill = "Topic",
y = "term")
```
### Classifying documents
```{r document classification}
lda_gamma <- tidytext:::tidy.LDA(corp_lda, matrix = "gamma")
lda_gamma
abs_class <- lda_gamma %>%
group_by(document) %>%
top_n(1, gamma) %>%
ungroup() %>%
arrange(gamma)
abs_class
abs_class %>%
group_by(topic) %>%
count()
abs_class %>%
ggplot(aes(lubridate::ymd(document), as.factor(topic), label = document)) +
geom_point(aes(colour = topic)) +
geom_path(aes(colour = topic))+
geom_text(size = 2, angle = 45, hjust = 0, vjust = 0)+
coord_cartesian(ylim = c(0, 9)) +
labs(y = "Topic",
x = "Date") +
theme(legend.position = "")
```