-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTAMOS_Capstone_code_Analysis.R
481 lines (363 loc) · 14.8 KB
/
TAMOS_Capstone_code_Analysis.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# Analysis Code - Capstone Project
# Tyler Amos
# Define list of organizations
organizations <- c("ICC", "OHCHR", "HRW", "USSTATE", "FIDH", "CRISISGROUP", "AMNESTY")
# Country codes, e.g., "IR" for Iran. This list contains Iran, Israel, Turkey, and Egypt
country_code <- c("IR", "IS", "TU", "EG")
# Merge all tone data
require(stringr)
tone_all <- data.frame()
for (i in 1:7) {
tone_working <- data.frame()
tone_working <- read.delim(str_c(organizations[i], "tone_data_flat.txt"))
tone_working[,9] <- organizations[i]
tone_all <- rbind(tone_all, tone_working)
rm(tone_working)
}
# Merge all location data
locations_all <- data.frame()
for (i in 1:7) {
locations_working <- data.frame()
locations_working <- read.delim(str_c(organizations[i], "locations_data_flat.txt"))
locations_working[,9] <- organizations[i]
locations_all <- rbind(locations_all, locations_working)
rm(locations_working)
}
colnames(locations_all)[9]<- "source"
# Merge tone and location data, using gkgrecordid as key
tone_location <- merge(locations_all[,c(1,4,9)], tone_all[,c(1:7)], by = c("gkgrecordid"), all.x = FALSE, all.y = FALSE)
# Clean environment to free up space
rm(locations_all)
rm(tone_all)
# Take only complete cases
tone_location <- na.omit(tone_location)
# Select a stratified random sample to work with, extracting data for the country list above, equally divided among organizations.
# First, verify there are enough records from each source, for each country, to work with
table(tone_location$source[tone_location$flat_location_countrycode == "IR"])
table(tone_location$source[tone_location$flat_location_countrycode == "IS"])
table(tone_location$source[tone_location$flat_location_countrycode == "TU"])
# Second, in order to create our baseline group, we need to assign "EG" to all other countries
tone_location[!(tone_location$flat_location_countrycode %in% country_code),"flat_location_countrycode"] <- levels(tone_location$flat_location_countrycode)[53]
# There are very few ICC records to use for all the countries, thus ICC is not referenced
# and dropped from the for loop below, 1200 records to be randomly selected from each country-source combination
require(dplyr)
tone_location_3country <- data.frame()
for (i in 1:4)
{
tone_locationcntry <- tone_location[tone_location$flat_location_countrycode == country_code[i],]
for (i in 2:7)
{
tone_locationorg <- sample_n(tone_locationcntry[tone_locationcntry$source == organizations[i],], 1200)
tone_location_3country <- rbind(tone_location_3country, tone_locationorg)
}
rm(tone_locationorg)
rm(tone_locationcntry)
}
# Clean up the environment by saving tone_location and removing unnecessary variables
require(readr)
write_delim(tone_location, "tone_location_all_countries.txt", delim = "\t")
rm(tone_location)
# Check for normality visually
# While not perfect, boxplots indicate data is for the most part normally distributed
boxplot(tone_location_3country$flat_tone_avtone
~ tone_location_3country$source,
main = "Average Tone across Sources")
boxplot(tone_location_3country$flat_tone_positive
~ tone_location_3country$source,
main = "Positive Tone across Sources")
boxplot(tone_location_3country$flat_tone_negative
~ tone_location_3country$source,
main = "Negative Tone across Sources")
# Descriptive statistics confirm the distributions are all fairly normal
summary(tone_location_3country$flat_tone_avtone)
summary(tone_location_3country$flat_tone_positive)
summary(tone_location_3country$flat_tone_negative)
# variance is fairly uniform across the countries
aggregate(tone_location_3country$flat_tone_avtone
~ tone_location_3country$flat_location_countrycode,
FUN = var)
aggregate(tone_location_3country$flat_tone_positive
~ tone_location_3country$flat_location_countrycode,
FUN = var)
aggregate(tone_location_3country$flat_tone_negative
~ tone_location_3country$flat_location_countrycode,
FUN = var)
#relevel the country factor to make EG the reference, this will allow us to see the other countries, which use EG as a baseline
tone_location_3country$flat_location_countrycode <- relevel(tone_location_3country$flat_location_countrycode, ref = "EG")
# call broom package to make the ouput easy to read
install.packages("broom")
require(broom)
# try the model with just source as a predictor
model <- lm(flat_tone_avtone ~ source, data = tone_location_3country)
modelpos <- lm(flat_tone_positive ~ source, data = tone_location_3country)
modelneg <- lm(flat_tone_negative ~ source, data = tone_location_3country)
# print key statistics of model
glance(model)
glance(modelpos)
glance(modelneg)
#try the model with just country
model <- lm(flat_tone_avtone ~ flat_location_countrycode,
data = tone_location_3country
)
modelpos <- lm(flat_tone_positive ~ flat_location_countrycode,
data = tone_location_3country
)
modelneg <- lm(flat_tone_negative ~ flat_location_countrycode,
data = tone_location_3country
)
# printing key statistics
glance(model)
glance(modelpos)
glance(modelneg)
# model with source by country
model <- lm(flat_tone_avtone ~ source
*flat_location_countrycode,
data = tone_location_3country
)
glance(model)
summary(model)
modelpos <- lm(flat_tone_positive ~ source
*flat_location_countrycode,
data = tone_location_3country
)
glance(modelpos)
summary(modelpos)
modelneg <- lm(flat_tone_negative ~ source
*flat_location_countrycode,
data = tone_location_3country
)
glance(modelneg)
summary(modelneg)
# The model's r squared value shows ~ 12-15% of the variation in average tone is explained by source and location interaction.
# Overall, the hypothesized predictor factors explain only a small amount of variation in tone.
# Let's look at the fits. Residuals appear random - suggesting a good fit.
plot(residuals(model), main = "Residuals: Average Tone Predicted \n by Source and Country Code")
plot(residuals(modelpos), main = "Residuals: Positive Tone Predicted \n by Source and Country Code")
plot(residuals(modelneg), main = "Residuals: Negative Tone Predicted \n by Source and Country Code")
#If the hypothesized factors are not predicting a substantial amount of the
#dependent variables let's try incorporating information on
#specific events from the counts data.
# Clean up the environment and save the dataset
write_delim(tone_location_3country, "tone_location_3country.txt", delim = "\t")
rm(tone_location_3country)
rm(modelneg)
rm(modelpos)
rm(model)
require(stringr)
# import all the counts data
counts_all <- data.frame()
for (i in 2:7) {
require(stringr)
counts_working <- data.frame()
counts_working <- read.delim(str_c(organizations[i], "counts_data_flat.txt"))
counts_working[,12] <- organizations[i]
counts_all <- rbind(counts_all, counts_working)
rm(counts_working)
}
# Merge the dataset with the counts data
tone_location_3country <- read.delim("tone_location_3country.txt")
tone_location_counts_3country <- merge(tone_location_3country, counts_all[,c(1:3, 7)], by = c("gkgrecordid"), all.x = TRUE)
rm(counts_all)
rm(tone_location_3country)
#clear out any missing values
tone_location_counts_3country <- na.omit(tone_location_counts_3country)
#save the data before trimming to include just the variables needed
write_delim(tone_location_counts_3country, "tone_location_counts_3country.txt", delim = "\t")
t_l_c_3country <- tone_location_counts_3country[,c(2:7,10:12)]
rm(tone_location_counts_3country)
# Install biglm and call the package - it will be used later for the larger models
install.packages("biglm")
require(biglm)
# Try a model with just count type
model <- lm(t_l_c_3country$flat_tone_avtone ~
t_l_c_3country$flat_count_counttype
)
modelpos <- lm(t_l_c_3country$flat_tone_positive ~
t_l_c_3country$flat_count_counttype
)
modelneg <- lm(t_l_c_3country$flat_tone_negative ~
t_l_c_3country$flat_count_counttype
)
# print summary statistics
glance(model)
glance(modelpos)
glance(modelneg)
# Try a model with location by count type
model <- lm(t_l_c_3country$flat_tone_avtone ~
t_l_c_3country$flat_location_countrycode*
t_l_c_3country$flat_count_counttype
)
modelpos <- lm(t_l_c_3country$flat_tone_positive ~
t_l_c_3country$flat_location_countrycode*
t_l_c_3country$flat_count_counttype
)
modelneg <- lm(t_l_c_3country$flat_tone_negative ~
t_l_c_3country$flat_location_countrycode*
t_l_c_3country$flat_count_counttype
)
# print summary statistics
glance(model)
glance(modelpos)
glance(modelneg)
# Try a model with source by location and count type
model <- lm(t_l_c_3country$flat_tone_avtone ~
t_l_c_3country$source*
t_l_c_3country$flat_location_countrycode+
t_l_c_3country$flat_count_counttype
)
glance(model)
rm(model)
modelpos <- lm(t_l_c_3country$flat_tone_positive ~
t_l_c_3country$source*
t_l_c_3country$flat_location_countrycode+
t_l_c_3country$flat_count_counttype
)
glance(modelpos)
rm(modelpos)
modelneg <- lm(t_l_c_3country$flat_tone_negative ~
t_l_c_3country$source*
t_l_c_3country$flat_location_countrycode+
t_l_c_3country$flat_count_counttype
)
glance(modelneg)
rm(modelneg)
# This model provides a substantially higher r squared value, ~ .27
# If we repeat the same model, but with count values as a function of count type
# we obtain a lower r-squared value, suggesting count value is not a valuable predictor.
model <- lm(t_l_c_3country$flat_tone_avtone ~
t_l_c_3country$source*
t_l_c_3country$flat_location_countrycode+
t_l_c_3country$flat_count_counttype*
t_l_c_3country$flat_count_count
)
glance(model)
rm(model)
modelpos <- lm(t_l_c_3country$flat_tone_positive ~
t_l_c_3country$source*
t_l_c_3country$flat_location_countrycode+
t_l_c_3country$flat_count_counttype*
t_l_c_3country$flat_count_count
)
glance(modelpos)
rm(modelpos)
modelneg <- lm(t_l_c_3country$flat_tone_negative ~
t_l_c_3country$source*
t_l_c_3country$flat_location_countrycode+
t_l_c_3country$flat_count_counttype*
t_l_c_3country$flat_count_count
)
glance(modelneg)
rm(modelneg)
# A linear model with source and count type and location
model <- lm(flat_tone_avtone ~
source+
flat_count_counttype+
flat_location_countrycode, data = t_l_c_3country
)
modelpos <- lm(flat_tone_positive ~
source+
flat_count_counttype+
flat_location_countrycode, data = t_l_c_3country
)
modelneg <- lm(flat_tone_negative ~
source+
flat_count_counttype+
flat_location_countrycode, data = t_l_c_3country
)
# This model is as strong as source by location and count type
# suggesting there is no notable interaction between source, country code, and count type
glance(model)
rm(model)
glance(modelneg)
rm(modelneg)
glance(modelpos)
rm(modelpos)
# Let's try removing location data in model, and focusing on source by count type
model <- lm(t_l_c_3country$flat_tone_avtone ~
t_l_c_3country$source*
t_l_c_3country$flat_count_counttype
)
glance(model)
rm(model)
modelpos <- lm(t_l_c_3country$flat_tone_positive ~
t_l_c_3country$source*
t_l_c_3country$flat_count_counttype
)
glance(modelpos)
rm(modelpos)
modelneg <- lm(t_l_c_3country$flat_tone_negative ~
t_l_c_3country$source*
t_l_c_3country$flat_count_counttype
)
glance(modelneg)
rm(modelneg)
# This model is as strong or stronger than our previous best model, without using location data.
glance(model)
# This suggests that source and count type are the principal contributors to average tone.
# It also suggests that location is not a notable predictor for tone.
# Another possibility is source interacting with count type, which is in turn interacting with location.
# This model is too large for the regular R environment to handle.
# We need to use the biglm package and process the data in smaller pieces.
# split out the data set
half <- trunc(nrow(t_l_c_3country) / 2)
write_delim(t_l_c_3country, "tlc3country.txt", delim = "\t")
write_delim(t_l_c_3country[1:half,], "tlc3country1.txt", delim = "\t")
half <- half+1
full <- nrow(t_l_c_3country)
write_delim(t_l_c_3country[half:full,], "tlc3country2.txt", delim = "\t")
rm(half)
rm(full)
rm(t_l_c_3country)
bigmodel <- biglm(flat_tone_avtone ~
source*
flat_count_counttype*
flat_location_countrycode, data = read.delim("tlc3country1.txt")
)
bigmodel <- update(bigmodel, moredata = read.delim("tlc3country2.txt"))
# This model does not provide any notable gain in r squared value over our previous best model.
glance(bigmodel)
# calculate the p-value manually
r <- sqrt(as.numeric(glance(bigmodel)[1]))
n <- as.numeric(bigmodel["n"])
t <- (r * (sqrt(n-2)))/ (sqrt(1-(r*r)))
2*pt(q = -abs(t), df = n-1)
rm(r)
rm(n)
rm(t)
rm(bigmodel)
# p-value is very small
bigmodel <- biglm(flat_tone_positive ~
source*
flat_count_counttype*
flat_location_countrycode, data = read.delim("tlc3country1.txt")
)
bigmodel <- update(bigmodel, moredata = read.delim("tlc3country2.txt"))
# no substantial gains
glance(bigmodel)
# calculate the p-value manually
r <- sqrt(as.numeric(glance(bigmodel)[1]))
n <- as.numeric(bigmodel["n"])
rm(bigmodel)
t <- (r * (sqrt(n-2)))/ (sqrt(1-(r*r)))
2*pt(q = -abs(t), df = n-1)
rm(r)
rm(n)
rm(t)
bigmodel <- biglm(flat_tone_negative ~
source*
flat_count_counttype*
flat_location_countrycode, data = read.delim("tlc3country1.txt")
)
bigmodel <- update(bigmodel, moredata = read.delim("tlc3country2.txt"))
# again, no substantial gains
glance(bigmodel)
# calculate the p-value manually
r <- sqrt(as.numeric(glance(bigmodel)[1]))
n <- as.numeric(bigmodel["n"])
rm(bigmodel)
t <- (r * (sqrt(n-2)))/ (sqrt(1-(r*r)))
2*pt(q = -abs(t), df = n-1)
rm(r)
rm(n)
rm(t)
# It appears the best model is source by count type (average tone ~ source*count type)