forked from kmunger/Text_as_Data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecitation_3.R
291 lines (177 loc) · 7.75 KB
/
recitation_3.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
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
####Recitation 3
## Some Examples taken wholesale from Ken Benoit's NYU Dept. of Politics short course Fall 2014
## Avaliable on his website: www.kenbenoit.net
library(quanteda)
##load data
##load in data
data(iebudgetsCorpus, package = "quantedaData")
df <- texts(iebudgetsCorpus)
## Lexical diversity measures
# TTR
## might want toLower(iebudgetsCorpus)
tokens <- tokenize(iebudgetsCorpus, removePunct=TRUE)
# tokenz <- lapply(tokens, length)
tokenz <- lengths(tokens)
# typez <- lapply(lapply(tokens, unique ), length)
typez <- ntype(tokens)
#TTRz <- mapply("/", typez, tokenz, SIMPLIFY = FALSE)
# df$ttr<-unlist(TTRz)
ttr <- typez / tokenz
##basic plot
#plot(df$ttr)
plot(ttr)
##
#df$year<-as.numeric(df$year)
#aggregate(df$ttr, by=list(df$year), FUN=mean)
aggregate(ttr, by = list(iebudgetsCorpus[["year"]]), FUN = mean)
# table(df$year)
aggregate(ttr, by = list(iebudgetsCorpus[["party"]]), FUN = mean)
#table(df$party)
# another way:
lexdiv(dfm(iebudgetsCorpus, groups = "year", verbose = FALSE))
lexdiv(dfm(iebudgetsCorpus, groups = "party", verbose = FALSE))
##Let's think about if it matters
#readability measure
?readability
##let's look at FRE
#df$read_FRE<-readability(df$texts, "Flesch")
readability(iebudgetsCorpus, "Flesch")
#aggregate(df$read_FRE, by=list(df$year), FUN=mean)
readability(texts(iebudgetsCorpus, groups = "year"), "Flesch")
#aggregate(df$read_FRE, by=list(df$party), FUN=mean)
readability(texts(iebudgetsCorpus, groups = "party"), "Flesch")
##Dale-Chall measure
#df$read_DC<-readability(df$texts, "Dale.Chall")
readability(iebudgetsCorpus, "Dale.Chall")
#aggregate(df$read_DC, by=list(df$year), FUN=mean)
readability(texts(iebudgetsCorpus, groups = "year"), "Dale.Chall")
#aggregate(df$read_DC, by=list(df$party), FUN=mean)
readability(texts(iebudgetsCorpus, groups = "party"), "Dale.Chall")
##let's look at all of em
#read<-readability(df$texts)
cor(readability(iebudgetsCorpus, c("Flesch", "Dale.Chall", "SMOG", "Coleman.Liau", "Fucks")))
#cor(read$Flesch, read$SMOG)
#cor(read$Coleman.Liau, read$Dale.Chall)
# cor(read$Fucks, read$Dale.Chall)
####Bootstrapping!
# remove smaller parties
iebudgetsCorpSub <- subset(iebudgetsCorpus, !(party %in% c("WUAG", "SOC", "PBPA" )))
library(boot)
bsReadabilityByGroup <- function(x, i, groups = NULL, measure = "Flesch")
readability(texts(x[i], groups = groups), measure)
R <- 50
# by party
groups <- factor(iebudgetsCorpSub[["party"]])
b <- boot(texts(iebudgetsCorpSub), bsReadabilityByGroup, strata = groups, R = R, groups = groups)
colnames(b$t) <- names(b$t0)
apply(b$t, 2, quantile, c(.025, .5, .975))
# by year
groups <- factor(iebudgetsCorpSub[["year"]])
b <- boot(texts(iebudgetsCorpSub), bsReadabilityByGroup, strata = groups, R = R, groups = groups)
colnames(b$t) <- names(b$t0)
apply(b$t, 2, quantile, c(.025, .5, .975))
## can get the SEs the same way, from b$t
#Let's consider the different speeches by different parties/years, say we want to get standard errors on Flesch scores
library(dplyr)
##initialize data frames
year_FRE <- data.frame(matrix(ncol = 5, nrow = 100))
#Let's filter out the parties with only one speech
df <- filter(df, party != "WUAG" & party != "SOC" & party != "PBPA" )
party_FRE<-data.frame(matrix(ncol = 6, nrow = 100))
#run the bootstraps
for(i in 1:100){
#sample 200
bootstrapped<-sample_n(df, 200, replace=TRUE)
bootstrapped$read_FRE<-readability(bootstrapped$texts, "Flesch")
#store results
year_FRE[i,]<-aggregate(bootstrapped$read_FRE, by=list(bootstrapped$year), FUN=mean)[,2]
party_FRE[i,]<-aggregate(bootstrapped$read_FRE, by=list(bootstrapped$party), FUN=mean)[,2]
}
#name the data frames
colnames(year_FRE)<-names(table(df$year))
colnames(party_FRE)<-names(table(df$party))
#define the standard error function
std <- function(x) sd(x)/sqrt(length(x))
##calculate standard errors and point estimates
year_ses<-apply(year_FRE, 2, std)
year_means<-apply(year_FRE, 2, mean)
party_ses<-apply(party_FRE, 2, std)
party_means<-apply(party_FRE, 2, mean)
###Plot results--year
coefs<-year_means
ses<-year_ses
y.axis <- c(1:5)
min <- min(coefs - 2*ses)
max <- max(coefs + 2*ses)
var.names <- colnames(year_FRE)
adjust <- 0
par(mar=c(2,8,2,2))
plot(coefs, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19, cex = .8,
xlim=c(min,max),ylim = c(.5,6.5), main = "")
rect(min,.5,max,1.5, col = c("grey97"), border="grey90", lty = 2)
rect(min,1.5,max,2.5, col = c("grey95"), border="grey90", lty = 2)
rect(min,2.5,max,3.5, col = c("grey97"), border="grey90", lty = 2)
rect(min,3.5,max,4.5, col = c("grey95"), border="grey90", lty = 2)
rect(min,4.5,max,5.5, col = c("grey97"), border="grey90", lty = 2)
#rect(min,5.5,max,6.5, col = c("grey97"), border="grey90", lty = 2)
axis(1, at = seq(min,max,(max-min)/10),
labels = c(round(min+0*((max-min)/10),3),
round(min+1*((max-min)/10),3),
round(min+2*((max-min)/10),3),
round(min+3*((max-min)/10),3),
round(min+4*((max-min)/10),3),
round(min+5*((max-min)/10),3),
round(min+6*((max-min)/10),3),
round(min+7*((max-min)/10),3),
round(min+8*((max-min)/10),3),
round(min+9*((max-min)/10),3),
round(max,3)),tick = T,cex.axis = .75, mgp = c(2,.7,0))
axis(2, at = y.axis, label = var.names, las = 1, tick = FALSE, cex.axis =.8)
abline(h = y.axis, lty = 2, lwd = .5, col = "white")
segments(coefs-qnorm(.975)*ses, y.axis+2*adjust, coefs+qnorm(.975)*ses, y.axis+2*adjust, lwd = 1)
segments(coefs-qnorm(.95)*ses, y.axis+2*adjust-.035, coefs-qnorm(.95)*ses, y.axis+2*adjust+.035, lwd = .9)
segments(coefs+qnorm(.95)*ses, y.axis+2*adjust-.035, coefs+qnorm(.95)*ses, y.axis+2*adjust+.035, lwd = .9)
points(coefs, y.axis+2*adjust,pch=21,cex=.8, bg="white")
##real world data
table(df$year)
observed<-aggregate(df$read_FRE, by=list(df$year), FUN=mean)
###Plot results--party
coefs<-party_means
ses<-party_ses
y.axis <- c(1:6)
min <- min(coefs - 2*ses)
max <- max(coefs + 2*ses)
var.names <- colnames(party_FRE)
adjust <- 0
par(mar=c(2,8,2,2))
plot(coefs, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19, cex = .8,
xlim=c(min,max),ylim = c(.5,6.5), main = "")
rect(min,.5,max,1.5, col = c("grey97"), border="grey90", lty = 2)
rect(min,1.5,max,2.5, col = c("grey95"), border="grey90", lty = 2)
rect(min,2.5,max,3.5, col = c("grey97"), border="grey90", lty = 2)
rect(min,3.5,max,4.5, col = c("grey95"), border="grey90", lty = 2)
rect(min,4.5,max,5.5, col = c("grey97"), border="grey90", lty = 2)
rect(min,5.5,max,6.5, col = c("grey97"), border="grey90", lty = 2)
axis(1, at = seq(min,max,(max-min)/10),
labels = c(round(min+0*((max-min)/10),3),
round(min+1*((max-min)/10),3),
round(min+2*((max-min)/10),3),
round(min+3*((max-min)/10),3),
round(min+4*((max-min)/10),3),
round(min+5*((max-min)/10),3),
round(min+6*((max-min)/10),3),
round(min+7*((max-min)/10),3),
round(min+8*((max-min)/10),3),
round(min+9*((max-min)/10),3),
round(max,3)),tick = T,cex.axis = .75, mgp = c(2,.7,0))
axis(2, at = y.axis, label = var.names, las = 1, tick = FALSE, cex.axis =.8)
abline(h = y.axis, lty = 2, lwd = .5, col = "white")
segments(coefs-qnorm(.975)*ses, y.axis+2*adjust, coefs+qnorm(.975)*ses, y.axis+2*adjust, lwd = 1)
segments(coefs-qnorm(.95)*ses, y.axis+2*adjust-.035, coefs-qnorm(.95)*ses, y.axis+2*adjust+.035, lwd = .9)
segments(coefs+qnorm(.95)*ses, y.axis+2*adjust-.035, coefs+qnorm(.95)*ses, y.axis+2*adjust+.035, lwd = .9)
points(coefs, y.axis+2*adjust,pch=21,cex=.8, bg="white")
##real world data
table(df$party)
aggregate(df$read_FRE, by=list(df$party), FUN=mean)
df$
browseVignettes(package = "dplyr")