-
Notifications
You must be signed in to change notification settings - Fork 4
/
.Rhistory
577 lines (577 loc) · 27.2 KB
/
.Rhistory
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
setwd("/Volumes/Macintosh HD 2/Github/info480/week7")
x = seq( from = -2 , to = 2 , by = 0.1 ) # Specify vector of x values.
y = x^2 # Specify corresponding y values.
plot( x , y , type = "l" ) # Make a graph of the x,y points.
HDIofMCMC = function( sampleVec , credMass=0.95 ) {
# Computes highest density interval from a sample of representative values,
# estimated as shortest credible interval.
# Arguments:
# sampleVec
# is a vector of representative values from a probability distribution.
# credMass
# is a scalar between 0 and 1, indicating the mass within the credible
# interval that is to be estimated.
# Value:
# HDIlim is a vector containing the limits of the HDI
sortedPts = sort( sampleVec )
ciIdxInc = floor( credMass * length( sortedPts ) )
nCIs = length( sortedPts ) - ciIdxInc
ciWidth = rep( 0 , nCIs )
for ( i in 1:nCIs ) {
ciWidth[ i ] = sortedPts[ i + ciIdxInc ] - sortedPts[ i ]
}
HDImin = sortedPts[ which.min( ciWidth ) ]
HDImax = sortedPts[ which.min( ciWidth ) + ciIdxInc ]
HDIlim = c( HDImin , HDImax )
return( HDIlim )
}
# Specify known values of prior and actual data.
priorA = 100
priorB = 1
actualDataZ = 8
actualDataN = 12
# Compute posterior parameter values.
postA = priorA + actualDataZ
postB = priorB + actualDataN - actualDataZ
# Number of flips in a simulated sample should match the actual sample size:
simSampleSize = actualDataN
# Designate an arbitrarily large number of simulated samples.
nSimSamples = 10000
# Set aside a vector in which to store the simulation results.
simSampleZrecord = vector( length=nSimSamples )
# Now generate samples from the posterior.
for ( sampleIdx in 1:nSimSamples ) {
# Generate a theta value for the new sample from the posterior.
sampleTheta = rbeta( 1 , postA , postB )
# Generate a sample, using sampleTheta.
sampleData = sample( x=c(0,1) , prob=c( 1-sampleTheta , sampleTheta ) ,
size=simSampleSize , replace=TRUE )
# Store the number of heads in sampleData.
simSampleZrecord[ sampleIdx ] = sum( sampleData )
}
# Make a histogram of the number of heads in the samples.
hist( simSampleZrecord )
# Kruschke, J. K. (2011). Doing Bayesian data analysis: A
# Tutorial with R and BUGS. Academic Press / Elsevier.
datatset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname, n)
rect.hclust(modelname, 3)
rect.hclust(modelname, 5)
plot(modelname)
rect.hclust(modelname, h=height)
height=3
rect.hclust(modelname, h=height)
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname, 5)
plot(modelname)
rect.hclust(modelname, h=height)
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname, 5)
plot(modelname)
rect.hclust(modelname, h=7)
rect.hclust(modelname, k=7)
plot(modelname)
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname, 5)
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname,9)
europe
dataset
europe <-read.csv("europe.csv")
euroclust<-hclust(dist(europe[-1]))
plot(euroclust, labels=europe$Country)
europe
library(gtools)
# Bayesian bootstrap
mean.bb <- function(x, n) {
apply(rdirichlet(n, rep(1, length(x))), 1, weighted.mean, x = x)
}
# standard bootstrap
mean.fb <- function(x, n) {
replicate(n, mean(sample(x, length(x), TRUE)))
}
set.seed(1)
reps <- 100000
x <- cars$dist
system.time(fbq <- quantile((mean.fb(x, reps)), c(0.025, 0.075)))
system.time(bbq <- quantile((mean.bb(x, reps)), c(0.025, 0.075)))
install.packages("gtools")
library(gtools)
# Bayesian bootstrap
mean.bb <- function(x, n) {
apply(rdirichlet(n, rep(1, length(x))), 1, weighted.mean, x = x)
}
# standard bootstrap
mean.fb <- function(x, n) {
replicate(n, mean(sample(x, length(x), TRUE)))
}
set.seed(1)
reps <- 100000
x <- cars$dist
system.time(fbq <- quantile((mean.fb(x, reps)), c(0.025, 0.075)))
system.time(bbq <- quantile((mean.bb(x, reps)), c(0.025, 0.075)))
bbq
x
x = seq( from = -2 , to = 2 , by = 0.1 ) # Specify vector of x values.
y = x^2 # Specify corresponding y values.
plot( x , y , type = "l" ) # Make a graph of the x,y points.
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname,9)
plot(modelname)
rect.hclust(modelname, k=7)
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname,9)
plot(modelname)
rect.hclust(modelname, k=3)
europe
europe <-read.csv("europe.csv")
euroclust<-hclust(dist(europe[-1]))
plot(euroclust, labels=europe$Country)
rect.hclust(euroclust, 2)
europe <-read.csv("europe.csv")
euroclust<-hclust(dist(europe[-1]))
plot(euroclust, labels=europe$Country)
rect.hclust(euroclust, y)
europe <-read.csv("europe.csv")
euroclust<-hclust(dist(europe[-1]))
plot(euroclust, labels=europe$Country)
rect.hclust(euroclust, 7)
europe <-read.csv("europe.csv")
euroclust<-hclust(dist(europe[-2]))
plot(euroclust, labels=europe$Country)
rect.hclust(euroclust, 7)
europe <-read.csv("europe.csv")
euroclust<-hclust(dist(europe[-1]))
plot(euroclust, labels=europe$Country)
rect.hclust(euroclust, 7)
<<<<<<< HEAD
setwd("/Volumes/Macintosh HD 2/Dropbox/00. Writing/138. Hunger versus Dark")
###iconv -f iso-8859-1 -t utf-8 "officialdemocrat.csv" > "officialdemocrat.utf8.csv"
####erase all data in R
rm(list=ls(all=TRUE))
startime<-Sys.time()
###Input file structure - .csv
##created_at,text,from_user,source,rt,to
##########VERSION 5 CHANGES
#######RETWEET EXTERNAL/INTERNAL IDENTIFICATION AND OUTPUT
#######CHANGED REGEX TO ACCOUNT FOR _'S IN USERNAMES
###TODO::::: IDENTIFY IF A USERS ONLY CONTRIBUTION IS A RETWEET
####TODO::::POWER USER SYNTACTICAL FEATURES
####TODO:::CREATE EDGE LIST WITH USERS AND TWEETS
#####TODO::::TRY TO IDENTIFY SIMILAR TWEET TEXT
####TODO::::ADD TRIM QUOTE
##################################################################################
#########THIS SECTION NORMALIZES THE DATA, CREATES THE TRIM FUNCTION TO ELIMINATE @ FROM THE DATA AND CREATE THE REPLY TO MESSAGES AND RETWEET MESSAGES
library(twitteR)
library(stringr)
library(zoo)
####input file
#inputfile <- "Debate8.csv"
inputfile <- "Hunger Games All.txt"
df <- read.csv(file=inputfile)
#####convert UTF-8 just to be safe, if the script is hanging up, it might be because of this
#df$text=sapply(df$text,function(row) iconv(row,to='UTF-8'))
numberoftweets<-nrow(df)
####function to trim the @ from the columns we extract
trimat <- function (x) sub('@','',x)
###function trims out period before the .@ messages
trimperiod <- function (x) sub('\\.','',x)
####take out colon
trimcolon <- function (x) sub(':','',x)
####take out comma
trimcomma <- function (x) sub(',','',x)
trimslash <- function (x) sub('\\/', '', x)
#####ADD TRIM QUOTE FUNCTION
####
#pull out reply to messages and create a new column
df$to=trimperiod(trimcomma(trimcolon(trimat(str_extract(df$text,"^(@[[:graph:]_]*)")))))
#pull out messages that preceded by a period, this functions as a public reply to
df$fauxto=trimcomma(trimcolon(trimperiod(trimat(str_extract(df$text,"^(.@[[:graph:]_]*)")))))
#pull out retweets and create a new column
#####create trim RT to sub RT with a space and then pull out the retweeted individual
trimRT <- function (x) sub('RT ','',x)
df$retweet=trimslash(trimperiod(trimcomma(trimcolon(trimRT(trimat(str_extract(df$text,"RT (@[[:graph:]_]*)")))))))
####via is another form of retweet that we should examine
trimVIA <- function (x) sub('via ','',x)
df$via=trimperiod(trimcomma(trimcolon(trimVIA(trimat(str_extract(df$text, "via (@[[:graph:]_]*)"))))))
######MT indicates a modified tweet that is retweeted
trimMT <- function (x) sub('MT ','',x)
df$modifiedtweet=trimperiod(trimcomma(trimcolon(trimMT(trimat(str_extract(df$text,"MT (@[[:graph:]_]*)"))))))
##################################################################################
##################################################################################
##################################################################################
##################################################################################
######EXTRACTS DEVICE NAME BY EXTRACTING THE TEXT BETWEET > AND < AND PRINTS TO NEW COLUMN
####NOTE: SOMETIMES THE ACCESS STRING SHOWS UP AS "web" WITH NO WRAPPER SO THE PRINTING OF "character(0)" IN THE SOURCE FIELD INDICATES WEB
#####TRIM FUNCTIONS AT THE END ADDRESS SOME IRREGULARITIES IN THE CHARACTER STRINGS
trimLT <- function (x) sub('<','',x)
trimGT <- function (x) sub('>','',x)
trimBlackberry <- function (x) sub('\U3e65613c','',x)
trimUber <- function (x) sub('?ber','Uber',x)
####NOTE: SOMETIMES THE ACCESS STRING SHOWS UP AS "web" WITH NO WRAPPER SO THE PRINTING OF "character(0)" IN THE SOURCE FIELD INDICATES WEB ###THE BELOW ADDRESSES THAT, BUT CANNOT ELIMINATE (0) FOR NOW THIS IS FINE BECAUSE THAT (0) MAY BE INDICATIVE OF DIFFERENT ACCESS MECHANISM
trimWEB <- function (x) sub("character.0.",'web',x)
####without this trim function at the beginning it gives a multibyte error
df$source=trimLT(df$source)
df$source=trimGT(str_extract_all(df$source,'(>).*?(<)'))
df$source=trimLT(df$source)
df$source=trimBlackberry(df$source)
df$source=trimUber(df$source)
df$source=trimWEB(df$source)
##################################################################################
######ANALYSIS OF DEVICE COUNTS TAKEN FROM THE ABOVE
devicecounts=table(df$source)
numberofdevices <- length(devicecounts)
#devicecounts=subset(devicecounts, devicecounts>1)
filename=paste("output/devicecounts", inputfile)
write.csv(devicecounts, file = filename, row.names=TRUE)
##################################################################################
##################################################################################
###PRINTS EDGELIST FOR DEVICE AFFILIATION NETWORKS
######NEED TO FIGURE OUT HOW TO GET RID OF NA's in the data
###13 is the column in the data we output
deviceedge <- df[c(3,4)]
#deviceedge = na.omit(deviceedge)
#write.csv(deviceedge, file="deviceedgelist.csv", row.names=FALSE)
colnames(deviceedge)[1]<-"source"
colnames(deviceedge)[2]<-"target"
filename=paste("output/deviceedgelist", inputfile)
write.csv(deviceedge, file = filename, row.names=FALSE)
##################################################################################
#####TEXT ANALYSIS
####returns the length of the df$text column which is the average number of characters
characterlength<-nchar(as.character(df$text))
dfcharacterlength<-as.data.frame(characterlength)
meantweetcharacterlength<-colMeans(dfcharacterlength)
##################################################################################
####identifies individuals who have used more than one device
uniquedeviceedge <- unique(deviceedge)
uniquedevicedgecounts=table(uniquedeviceedge$source)
uniquedeviceedgecountsusers=subset(uniquedevicedgecounts, uniquedevicedgecounts >2)
multipledeviceusers <- length(uniquedeviceedgecountsusers)
#write.csv(uniquedeviceedgecountsusers, file="uniquedeviceedgecountsusers.csv")
filename=paste("output/uniquedeviceedgecountsusers", inputfile)
write.csv(uniquedeviceedgecountsusers, file = filename, row.names=TRUE)
##################################################################################
####calculate percentage of users who use more than 1 device/application
t<-length(uniquedeviceedgecountsusers)
k<-length(uniquedevicedgecounts)
deviceduplicatepercentage=(t/k)
##################################################################################
##################################################################################
##################################################################################
##################################################################################
###DETECTS PRESENCE OF A LINK/HASHTAG/MENTION IN A BODY OF TEXT AND PRINTS TRUE/FALSE
df$linkpresence=str_detect(df$text, '(http................)')
linkpresence=table(df$linkpresence)
linktruecount <- sum(df$linkpresence == 'TRUE')
linkfalsecount <- sum(df$linkpresence == 'FALSE')
linkpresencepercentage <- (linktruecount/(linktruecount+linkfalsecount))
#write.csv(linkpresence, file="linkpresence.csv")
df$fauxtopresence=str_detect(df$text, "^(.@[[:graph:]_]*)")
fauxtopresence=table(df$fauxtopresence)
fauxtotruecount <- sum(df$fauxtopresence == 'TRUE')
fauxtofalsecount <- sum(df$fauxtopresence == 'FALSE')
fauxtopresencepercentage <- (fauxtotruecount/(fauxtotruecount+fauxtofalsecount))
df$hashpresence=str_detect(df$text, "(#[[:graph:]_]*)")
hashpresence=table(df$hashpresence)
hashtruecount <- sum(df$hashpresence == 'TRUE')
hashfalsecount <- sum(df$hashpresence == 'FALSE')
hashpresencepercentage <- (hashtruecount/(hashtruecount+hashfalsecount))
#write.csv(hashpresence, file="hashpresence.csv")
df$mentionpresence = str_detect(df$text, "(@[[:graph:]_]*)")
mentionpresence=table(df$mentionpresence)
mentiontruecount <- sum(df$mentionpresence == 'TRUE')
mentionfalsecount <- sum(df$mentionpresence == 'FALSE')
mentionpresencepercentage <- (mentiontruecount/(mentiontruecount+mentionfalsecount))
#write.csv(mentionpresence, file="mentionpresence.csv")
df$replytopresence = str_detect(df$text, "^(@[[:graph:]_]*)")
replytopresence=table(df$replytopresence)
replytotruecount <- sum(df$replytopresence == 'TRUE')
replytofalsecount <- sum(df$replytopresence == 'FALSE')
replytopresencepercentage <- (replytotruecount/(replytotruecount+replytofalsecount))
#write.csv(replytopresence, file="replytopresence.csv")
df$rtpresence = str_detect(df$text, "RT (@[[:graph:]_]*)")
rtpresence=table(df$rtpresence)
rttruecount <- sum(df$rtpresence == 'TRUE')
rtfalsecount <- sum(df$rtpresence == 'FALSE')
rtpresencepercentage <- (rttruecount/(rttruecount+rtfalsecount))
#write.csv(rtpresence, file="rtpresence.csv")
df$viapresence=trimVIA(trimat(str_detect(df$text, "via (@[[:graph:]_]*)")))
viapresence=table(df$viapresence)
viatruecount <- sum(df$viapresence == 'TRUE')
viafalsecount <- sum(df$viapresence == 'FALSE')
viapresencepercentage <- (viatruecount/(viatruecount+viafalsecount))
#write.csv(viapresence, file="viapresence.csv")
df$mtpresence=trimVIA(trimat(str_detect(df$text, "MT (@[[:graph:]_]*)")))
mtpresence=table(df$mtpresence)
mttruecount <- sum(df$mtpresence == 'TRUE')
mtfalsecount <- sum(df$mtpresence == 'FALSE')
mtpresencepercentage <- (mttruecount/(mttruecount+mtfalsecount))
#write.csv(mtpresence, file="mtpresence.csv")
####DETECTS THE NUMBER OF LINKS/HASHTAG/MENTION IN EACH OF THE COLUMNS AND PRINTS THE NUMBER
df$linknumber=str_count(df$text, '(http................)')
linknumber=table(df$linknumber)
#write.csv(linknumber, file="linknumber.csv")
filename=paste("output/linknumber", inputfile)
write.csv(linknumber, file = filename, row.names=TRUE)
df$hashnumber=str_count(df$text, "(#[[:graph:]_]*)")
hashnumber=table(df$hashnumber)
#write.csv(hashnumber, file="hashnumber.csv")
filename=paste("output/hashnumber", inputfile)
write.csv(hashnumber, file = filename, row.names=TRUE)
df$mentionnumber = str_count(df$text, "(@[[:graph:]_]*)")
mentionnumber=table(df$mentionnumber)
#write.csv(mentionnumber, file="mentionednumber.csv")
filename=paste("output/mentionnumber", inputfile)
write.csv(mentionnumber, file = filename, row.names=TRUE)
##################################################################################
##################################################################################
######IDENTIFY FREQUENT TWEETS, THIS IS LIKELY TO MOSTLY BE RETWEETS, BUT CAN BE ANY TWEET, IT IS ALSO LIKELY TO PULL OUT TWEETS THAT JUST CONTAIN ONE COMMON HASHTAG, ONE DOWNSIDE IS THAT IF ONE CHARACTER IS OFF THEN IT WILL NOT WORK CORRECTLY
tweetcounts=table(df$text)
uniquetweets<-length(tweetcounts)
tweetcounts=subset(tweetcounts, tweetcounts>2)
#write.csv(tweetcounts, file="tweetcounts.csv")
filename=paste("output/tweetcounts", inputfile)
write.csv(tweetcounts, file = filename, row.names=TRUE)
##################################################################################
##################################################################################
#######IDENTIFY THOSE WHO HAVE BEEN RETWEETED THE MOST (more than 5 times in below example)
retweetcounts=table(df$retweet)
numberofpeopleretweeted <- length(retweetcounts)
retweetcounts=subset(retweetcounts, retweetcounts>5)
#write.csv(retweetcounts, file="retweetcounts.csv")
filename=paste("output/retweetcounts", inputfile)
write.csv(retweetcounts, file = filename, row.names=TRUE)
##################################################################################
##################################################################################
#######IDENTIFY THOSE WHO HAVE TWEETED THE MOST (more than 5 times in below example)
tweetscreenamecounts=df$from_user
tweetscreenamecounts=tolower(tweetscreenamecounts)
tweetscreenamecounts=table(tweetscreenamecounts)
totalusers=tolower(df$from_user)
totalusers=unique(totalusers)
###calculate number of single posters
singleton<-subset(tweetscreenamecounts, tweetscreenamecounts<2)
numberofsingleposters<-length(singleton)
numberofuniquetweeters <- length(totalusers)
####CALCULATE PERCENTAGE OF SINGLETON POSTERS
singletonpercentage<-numberofsingleposters/numberofuniquetweeters
#tweetscreenamecounts=subset(tweetscreenamecounts, tweetscreenamecounts>0)
#write.csv(tweetscreenamecounts, file="tweetscreennamecounts.csv")
filename=paste("output/tweetscreenamecounts", inputfile)
write.csv(tweetscreenamecounts, file = filename, row.names=FALSE)
#################
##################################################################################
##################################################################################
########EXTRACTS HASHTAGS, THEN TRIMS THE # OUT, MAKES THEM ALL LOWERCASE AND THEN OUTPUTS A FREQUENCY COUNT OF THE OCCURRENCES OF THE HASHTAGS
hash = str_extract_all(df$text, "(#[[:graph:]_]*)")
hashunlist <- unlist(hash)
trimhash <- function (x) sub('#','',x)
hashunlist <- trimhash(hashunlist)
lowerhash <- tolower(hashunlist)
hashcounts=table(lowerhash)
numberofuniquehashtags<-length(hashcounts)
#write.csv(hashcounts, file="hashlistcounts.csv", row.names=FALSE)
hashcounts<-sort(table(lowerhash), decreasing=TRUE)
filename=paste("output/hashcounts", inputfile)
write.csv(hashcounts, file = filename, row.names=TRUE)
##################################################################################
##################################################################################
########EXTRACTS MENTIONS, THEN TRIMS THE @ OUT, AND THEN OUTPUTS A FREQUENCY COUNT OF THE OCCURRENCES OF THE HASHTAGS ###NO NEED TO MAKE THEM LOWERCASE BECAUSE THEY ARE USERNAMES
mention = str_extract_all(df$text, "(@[[:graph:]_]*)")
mentionunlist <- unlist(mention)
mentionunlist <- trimat(mentionunlist)
mentionunlist <- trimcolon(mentionunlist)
mentionunlist <- trimperiod(mentionunlist)
mentionunlist <- trimcomma(mentionunlist)
lowermention <- tolower(mentionunlist)
mentioncounts<-as.data.frame(table(lowermention))
colnames(mentioncounts)[1]<-"mention"
colnames(mentioncounts)[2]<-"frequency"
numberofuniquementions<-length(mentioncounts)
#mentioncounts<-sort(mentioncounts, decreasing=TRUE)
filename=paste("output/mentioncounts", inputfile)
write.csv(mentioncounts, file = filename, row.names=FALSE)
##################################################################################
##################################################################################
###PRINTS EDGELIST FOR REPLYTO STRUCTURE, the 3 and 6 are the column placements for the screename and to results
######NEED TO FIGURE OUT HOW TO GET RID OF NA's in the data
####12 is the column in the data we output
replytoedge <- df[c(3,6)]
replytoedge = na.omit(replytoedge)
colnames(replytoedge)[1]<-"source"
colnames(replytoedge)[2]<-"target"
#write.csv(replytoedge, file="replytoedgelist.csv", row.names=FALSE)
filename=paste("output/replytoedge", inputfile)
write.csv(replytoedge, file = filename, row.names=FALSE)
##################################################################################
##################################################################################
###PRINTS EDGELIST FOR RETWEET STRUCTURE, the 3 and 7 are the column placements for the screename and to results
###13 is the column in the data we output
retweetedge <- df[c(3,8)]
retweetedge = na.omit(retweetedge)
colnames(retweetedge)[1]<-"source"
colnames(retweetedge)[2]<-"target"
retweetedgetarget<-tolower(retweetedge$target)
#write.csv(retweetedge, file="retweetedgelist.csv", row.names=FALSE)
filename=paste("output/retweetedge", inputfile)
write.csv(retweetedge, file = filename, row.names=FALSE)
##################################################################################
######RANK THE MOST RETWEETED INDIVIDUALS
mostretweeted<-table(retweetedge$target)
highestretweeters<-table(retweetedge$target)
##################################################################################
#####PULLS OUT LINKS FROM TWEET TEXT
###THE URL REGEX PULLS OUT ANYTHING FOLLOWING AN HTTP UNTIL THE NEXT WHITESPACE SO IT IS POSSIBLE THAT PEOPLE THAT DO NOT PUT A A SPACE AFTER LINK WOULD GET EXTRA CHARACTERS IN, THAT IS WHY THE STR.MATCH IS LEFT AS HTTP........
####EXTRACTS ALL OF THE URLS, UNLISTS THEM, COUNTS THEM AND THEN CREATES A TABLE OF THOSE THAT APPEAR MORE THAN FIVE TIMES
df$link=str_extract_all(df$text, '(http://[[:graph:]\\s]*)')
linkunlist <- unlist(df$link)
linkcounts=table(linkunlist)
numberofuniquelinks<-length(linkcounts)
linkcounts<-as.data.frame(linkcounts)
colnames(linkcounts)[1]<-"link"
colnames(linkcounts)[2]<-"frequency"
filename=paste("output/linkcounts", inputfile)
write.csv(linkcounts, file = "linkcounts.csv", row.names=FALSE)
##################################################################################
##################################################################################
##################################################################################
##################################################################################
########
#####CREATE A SUMMARY TABLE OF THE ABOVE PERCENTAGES AND WRITE TO FILE
syntacticfeatureoverview <- table(inputfile, numberoftweets, linkpresencepercentage, hashpresencepercentage, mentionpresencepercentage, replytopresencepercentage, rtpresencepercentage, viapresencepercentage, mtpresencepercentage, deviceduplicatepercentage, numberofdevices, multipledeviceusers, numberofpeopleretweeted, numberofuniquetweeters, numberofuniquehashtags, numberofuniquementions, numberofuniquelinks, uniquetweets, numberofsingleposters, singletonpercentage, fauxtopresencepercentage, meantweetcharacterlength)
filename=paste("output/syntacticfeatureoverview", inputfile)
write.csv(syntacticfeatureoverview, file = filename, row.names=TRUE)
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
timeseriestable <- as.data.frame(table(df$created_at))
colnames(timeseriestable)[1]<-"time"
colnames(timeseriestable)[2]<-"tweets"
filename=paste("output/timeseries", inputfile)
write.csv(timeseriestable, file = filename, row.names=FALSE)
######
#window(ts, start=as.Date('2011-11-19'), end=as.Date('2011-11-22'))
###write to pdf file
filename=paste("TimeSeries", inputfile,".pdf")
pdf(file=filename)
# Plot your graph
plot(timeseriestable)
# Write the file
dev.off()
############################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
endtime<-Sys.time()
runtime=endtime-startime
runtimemessage<-paste("This took", runtime)
print(runtimemessage)
=======
x = seq( from = -2 , to = 2 , by = 0.1 ) # Specify vector of x values.
y = x^2 # Specify corresponding y values.
plot( x , y , type = "l" ) # Make a graph of the x,y points.
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname,9)
plot(modelname)
rect.hclust(modelname, k=3)
setwd("~/Documents/github/info480-2/week7")
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname,9)
plot(modelname)
rect.hclust(modelname, k=3)
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname,9)
rect.hclust(modelname,3)
plot(modelname)
rect.hclust(modelname,3)
plot(modelname)
rect.hclust(modelname, k=3)
plot(modelname)
rect.hclust(modelname,3)
plot(modelname)
rect.hclust(modelname, k=3)
plot(modelname)
rect.hclust(modelname,2)
plot(modelname)
rect.hclust(modelname, k=3)
plot(modelname)
rect.hclust(modelname, k=5)
europe <-read.csv("europe.csv")
euroclust<-hclust(dist(europe[-1]))
plot(euroclust, labels=europe$Country)
rect.hclust(euroclust, 7)
plot(euroclust, labels=europe$Country)
rect.hclust(euroclust, 15)
europe
europe
j\\;
?hclust
clear()
clear
europe
setwd("~/Documents/github/info480-2/week7")
dataset <-read.csv("europe.csv")
modelname<-hclust(dist(dataset))
plot(modelname, labels=dataset$variable)
plot(modelname)
rect.hclust(modelname,2)
plot(modelname)
rect.hclust(modelname, k=5)
europe <-read.csv("europe.csv")
euroclust<-hclust(dist(europe[-1]))
plot(euroclust, labels=europe$Country)
rect.hclust(euroclust, 7)
rstudio::viewData(dataset)
>>>>>>> 6f1bbda40ba9ac1e62ba0dc77e16538c4cdd933b