Files
uni-ba-socialagenda/issuecomp-analysis.R
T

123 lines
3.5 KiB
R
Raw Normal View History

2014-12-07 21:06:59 +01:00
require(lubridate)
2014-12-07 22:51:17 +01:00
require(XML)
require(ggplot2)
require(reshape2)
2015-01-12 14:12:25 +01:00
require(stringr)
2014-12-07 21:06:59 +01:00
# Create date range
date_start <- as.Date("2014-01-01")
2015-01-12 12:48:10 +01:00
date_end <- as.Date("2014-12-31")
2014-12-07 21:06:59 +01:00
drange <- as.integer(date_end - date_start)
2014-12-07 22:51:17 +01:00
drange <- date_start + days(0:drange)
2014-12-07 21:06:59 +01:00
2015-01-12 15:36:14 +01:00
# MATCH TWEETS ------------------------------------------------------------
issues <- data.frame(date = drange)
2014-12-07 21:06:59 +01:00
issuelist <- xmlToList("issues.xml")
issueheads <- names(issuelist)
issues[issueheads] <- 0
for(d in 1:nrow(issues)) {
2015-01-12 14:12:25 +01:00
# Go through every day
2014-12-07 21:06:59 +01:00
curdate <- issues$date[d]
cat(as.character(curdate),"\n")
2015-01-12 14:12:25 +01:00
2014-12-07 21:06:59 +01:00
# Put all tweets from specific day in a temporary DF
tweets_curday <- tweets[tweets[, "created_at"] == curdate, ]
for(t in 1:nrow(tweets_curday)){
# Select tweet's text, make it lowercase and remove hashtag indicators (#)
2015-01-12 14:59:51 +01:00
curtext <- as.character(tweets_curday$text[t])
2014-12-07 21:06:59 +01:00
curtext <- str_replace_all(curtext, "#", "")
2015-01-12 15:36:14 +01:00
# Now test each single issue (not tag!)
2014-12-07 21:06:59 +01:00
for(i in 1:length(issuelist)) {
curtags <- as.character(issuelist[[i]])
curissue <- names(issuelist)[i]
2015-01-12 15:36:14 +01:00
# Now test all tags of a single issue
2015-01-12 14:59:51 +01:00
for(t in 1:length(curtags)) {
2015-01-12 15:36:14 +01:00
curtag <- str_c("\\W", curtags[t], "\\W")
curchars <- nchar(curtag, type = "chars") - 4
2015-01-12 14:59:51 +01:00
2015-01-12 15:36:14 +01:00
# Match current tweet with tag. If >= 5 letters allow 1 changed letter, if >=8 letters allow 2 (Levenshtein distance)
2015-01-12 14:59:51 +01:00
tags_found <- smartPatternMatch(curtext, curtag, curchars)
if(tags_found == 1) {
2015-01-12 15:36:14 +01:00
#cat("Matched", curtag, "with", curtext,"\n")
issues[d,curissue] <- issues[d,curissue] + 1
2015-01-12 14:59:51 +01:00
break
}
2015-01-12 15:36:14 +01:00
else {
#cat("Nothing found\n")
}
} # /for curtags
2014-12-07 21:06:59 +01:00
} # /for issuelist
} # /for tweets_curday
} # /for drange
2015-01-12 15:36:14 +01:00
# WEEKLY INTERVALS --------------------------------------------------------
2014-12-07 22:51:17 +01:00
## Do not use days but week intervals
wrange <- (as.integer(date_end - date_start) / 7)
wrange <- floor(wrange) - 1
wrange <- date_start + weeks(0:wrange)
issues_week <- data.frame(week = wrange)
issues_week[issueheads] <- 0
for(w in 1:nrow(issues_week)) {
curweek <- issues_week$week[w]
currange <- curweek + days(0:6)
day <- 1
for(d in 1:nrow(issues)) {
curday <- issues$date[d]
if(curweek == curday) {
for(c in 2:ncol(issues)) {
curissue <- names(issues)[c]
d2 <- d + 6
curvalue <- sum(issues[d:d2,curissue])
issues_week[w, curissue] <- curvalue
} # /for issues columns
} # /if day matches first day of week
} # /for issues rows
} # /for issues_week
2014-12-07 21:44:03 +01:00
# VISUALS -----------------------------------------------------------------
2014-12-07 22:51:17 +01:00
# Level: days
issues_melt <- melt(issues,id="date")
ggplot(issues_melt,aes(x=date,y=value,colour=variable,group=variable)) + geom_line(size=1)
ggplot(issues_melt,aes(x=date,y=value,colour=variable,group=variable)) + geom_smooth(size=1,method="loess",formula = y ~ x, se=FALSE)
2014-12-07 22:51:17 +01:00
# Level: weeks
issues_week_melt <- melt(issues_week,id="week")
ggplot(issues_week_melt,aes(x=week,y=value,colour=variable,group=variable)) + geom_line(size=1)
ggplot(issues_week_melt,aes(x=week,y=value,colour=variable,group=variable)) + geom_smooth(size=1,method="loess",formula = y ~ x, se=FALSE)
2014-12-07 22:51:17 +01:00
2014-12-07 21:06:59 +01:00
# POSSIBLY USEFUL CODE ----------------------------------------------------
# Limits of list
length(issuelist)
length(issuelist[[2]])
# Select all tweets from current day in drange
tweets_curday <- tweets[tweets[, "created_at"] == drange[5], ]
# Is column a issue counting column?
str_detect(names(issues[2]), "^issue")