-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path03-week3demo.Rmd
264 lines (203 loc) · 8.77 KB
/
03-week3demo.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
# Week 3 Demo
In this section, we'll have a quick overview of how we're processing text data when conducting basic sentiment analyses.
## Setup
We'll first load the packages we need.
```{r, message=F, warning=F}
library(stringi)
library(dplyr)
library(tidytext)
library(ggplot2)
library(scales)
```
## Happy words
As we discussed in the lectures, we might find in our text of the class's collective thoughts that there was an increase in "happy" words over time.
I have simulated a dataset of text split by weeks, students, and words plus whether or not the word is the word "happy" where `0` means it is not the word "happy" and `1` means it is.
```{r, echo = F}
## Varying total happy word numbers example
lipsum_text <- data.frame(text = stri_rand_lipsum(5000, start_lipsum = TRUE))
lipsum_text$happyn <- as.character("")
lipsum_text$happyu <- as.character("")
lipsum_text$happyd <- as.character("")
lipsum_text$week <- as.integer(rep(seq.int(1:10), 5000/10))
lipsum_text$student <- sample(1:30,5000, replace = T)
for(i in 1:nrow(lipsum_text)) {
week <- lipsum_text[i,5]
happyn <- paste(rep("happy", times = sample(1:100, 1)), collapse = " ")
happyu <- paste(rep("happy", times = sample(1:100, 1)*week), collapse = " ")
happyd <- paste(rep("happy", times = sample(1:100, 1)*1/week), collapse = " ")
lipsum_text[i, 2] <- happyn
lipsum_text[i, 3] <- happyu
lipsum_text[i, 4] <- happyd
}
lipsum_text$happyn <- paste(lipsum_text$text, lipsum_text$happyn)
lipsum_text$happyu <- paste(lipsum_text$text, lipsum_text$happyu)
lipsum_text$happyd <- paste(lipsum_text$text, lipsum_text$happyd)
happyn <- lipsum_text %>%
select(happyn, week, student) %>%
unnest_tokens(word, happyn) %>%
group_by(week, student)
happyu <- lipsum_text %>%
select(happyu, week, student) %>%
unnest_tokens(word, happyu) %>%
group_by(week, student)
happyd <- lipsum_text %>%
select(happyd, week, student) %>%
unnest_tokens(word, happyd) %>%
group_by(week, student)
happyn$happy <- as.integer(grepl("happy", x = happyn$word))
happyu$happy <- as.integer(grepl("happy", x = happyu$word))
happyd$happy <- as.integer(grepl("happy", x = happyd$word))
happys <- list(happyn, happyu, happyd)
```
We have three datasets: one with a constant number of "happy" words; one with an increasing number of "happy" words; and one with a decreasing number of "happy" words. These are called: `happyn`, `happyu`, and `happyd` respectively.
```{r}
head(happyn)
```
```{r}
head(happyu)
```
```{r}
head(happyd)
```
We can then see the trend in "happy" words over by week and student.
First, the dataset where we have a constant number of happy words over time.
```{r, echo = F}
happyn %>%
group_by(week, student) %>%
mutate(index_total = n()) %>%
filter(happy == 1) %>%
summarise(
sum_hap = sum(happy),
index_total = index_total,
prop_hap = sum_hap / index_total
) %>%
distinct() %>%
ggplot() +
geom_point(aes(week, prop_hap)) +
geom_smooth(aes(week, prop_hap)) +
scale_x_continuous(breaks = pretty_breaks())
```
And now the simulated data with an increasing number of happy words.
```{r, echo = F}
happyu %>%
group_by(week, student) %>%
mutate(index_total = n()) %>%
filter(happy == 1) %>%
summarise(
sum_hap = sum(happy),
index_total = index_total,
prop_hap = sum_hap / index_total
) %>%
distinct() %>%
ggplot() +
geom_point(aes(week, prop_hap)) +
geom_smooth(aes(week, prop_hap)) +
scale_x_continuous(breaks = pretty_breaks())
```
And finally a decreasing number of happy words.
```{r, echo = F}
happyd %>%
group_by(week, student) %>%
mutate(index_total = n()) %>%
filter(happy == 1) %>%
summarise(
sum_hap = sum(happy),
index_total = index_total,
prop_hap = sum_hap / index_total
) %>%
distinct() %>%
ggplot() +
geom_point(aes(week, prop_hap)) +
geom_smooth(aes(week, prop_hap)) +
scale_x_continuous(breaks = pretty_breaks())
```
## Normalizing sentiment
But as we discussed in the lecture, we also know that just because the total number of happy words increases, this isn't indication on its own that we're getting happier as a class over time.
Before we can begin to make any such inference, we need to normalize by the total number of words each week.
Below, I simulate data where the number of happy words is actually the same each week (our `happyn` dataset above).
I join these data with three other datasets: `happylipsumn`, `happylipsumu`, and `happylipsumd`. These are datasets of random text, with the *same* number of happy words.
The first of these also has the *same* number of total words each week. The second two, however, have *differing* number of *total* words each week: `happylipsumu` has an increasing number of total words each week; `happylipsumd` has a decreasing number of total words each week.
```{r, echo=F}
lipsum_text$lipsumn <- as.character("")
lipsum_text$lipsumu <- as.character("")
lipsum_text$lipsumd <- as.character("")
for(i in 1:nrow(lipsum_text)) {
week <- lipsum_text[i,5]
lipsumn <- paste(rep(stri_rand_lipsum(1, start_lipsum = TRUE),
times = 1), collapse = " ")
lipsumu <- paste(rep(stri_rand_lipsum(1, start_lipsum = TRUE),
times = sample(1:10,1)*week), collapse = " ")
lipsumd <- paste(rep(stri_rand_lipsum(1, start_lipsum = TRUE),
times = sample(1:10,1)*1/week), collapse = " ")
lipsum_text[i, 7] <- lipsumn
lipsum_text[i, 8] <- lipsumu
lipsum_text[i, 9] <- lipsumd
}
lipsum_text$happylipsumn <- paste(lipsum_text$lipsumn, lipsum_text$happyn)
lipsum_text$happylipsumu <- paste(lipsum_text$lipsumu, lipsum_text$happyn)
lipsum_text$happylipsumd <- paste(lipsum_text$lipsumd, lipsum_text$happyn)
happylipsumn <- lipsum_text %>%
select(happylipsumn, week, student) %>%
unnest_tokens(word, happylipsumn) %>%
group_by(week, student)
happylipsumu <- lipsum_text %>%
select(happylipsumu, week, student) %>%
unnest_tokens(word, happylipsumu) %>%
group_by(week, student)
happylipsumd <- lipsum_text %>%
select(happylipsumd, week, student) %>%
unnest_tokens(word, happylipsumd) %>%
group_by(week, student)
happylipsumn$happy <- as.integer(grepl("happy", x = happylipsumn$word))
happylipsumu$happy <- as.integer(grepl("happy", x = happylipsumu$word))
happylipsumd$happy <- as.integer(grepl("happy", x = happylipsumd$word))
lipsums <- list(happylipsumn, happylipsumu, happylipsumd)
```
Again, as you see below, we're splitting by week, student, word, and whether or not it is a "happy" word.
```{r}
head(happylipsumn)
head(happylipsumu)
head(happylipsumd)
```
Then if we plot the number of happy words *divided* by the number of total words each week for each student in each of these datasets, we get the below.
To get this normalized sentiment score--or "happy" score--we need to create a variable (column) in our dataframe that is the sum of happy words divided by the total number of words in the dataframe.
We can do this in the following way.
```{r}
happylipsumn %>%
group_by(week, student) %>%
mutate(index_total = n()) %>%
filter(happy==1) %>%
summarise(sum_hap = sum(happy),
index_total = index_total,
prop_hap = sum_hap/index_total) %>%
distinct()
```
Then if we repeat this for each of our datasets and plot we see the following.
```{r, echo = F, message = F, warning = F}
library(gridExtra)
plots=list()
for (i in seq_along(lipsums)) {
lipsum <- lipsums[[i]]
plots[[i]] <- lipsum %>%
group_by(week, student) %>%
mutate(index_total = n()) %>%
filter(happy==1) %>%
summarise(sum_hap = sum(happy),
index_total = index_total,
prop_hap = sum_hap/index_total) %>%
distinct() %>%
ggplot() +
geom_point(aes(week, prop_hap)) +
geom_smooth(aes(week, prop_hap)) +
scale_x_continuous(breaks= pretty_breaks()) +
theme(axis.text.y=element_text(size=20),
axis.text.x=element_text(size=20),
axis.title.x=element_text(size=20, face="bold"),
axis.title.y=element_text(size=20, face="bold"))
}
do.call("grid.arrange", c(plots, ncol = 2))
```
Why do the plots look like this?
Well, in the first, we have about the *same* number of total words each week and about the *same* number of happy words each week. If we divided the latter by the former, we get a proportion that is also stable over time.
In the second, however, we have an *increasing* number of total words each week, but about the *same* number of happy words over time. This means that we are dividing by an ever larger number, giving ever smaller proportions. As such, the trend is decreasing over time.
In the third, we have a *decreasing* number of total words each week, but about the *same* number of happy words over time. This means that we are dividing by an ever smaller number, giving ever larger proportions. As such, the trend is increasing over time.