require(stringr)
require(reshape2)
require(ggplot2)
require(vars)

# Create dataframes with only non-sensational (i) and sensational (s) issue columns
drop_s <- which(str_detect(names(issues), "^s"))
drop_i <- which(str_detect(names(issues), "^i"))
issues_i <- issues[,-drop_s]
issues <- issues[,-drop_i]

# #
# ENTROPY
# #
# Entropy non-sensational issues
issues_i$total <- rowSums(issues_i[2:ncol(issues_i)])
issues_i$entropy <- 0
for(r in 1:nrow(issues_i)) {
  curtotal <- as.numeric(issues_i$total[r])
  curp <- 0
  for(c in 2:ncol(issues_i)) {
    curcount <- as.numeric(issues_i[r,c])
    curp[c] <- curcount / curtotal
  }
  curp <- curp [2:length(curp)-2]
  curdrop <- which(curp==0)
  curp <- curp[-curdrop]
  issues_i$entropy[r] <- sum(-1 * curp * log(curp))
}

# Entropy sensational issues
issues$total <- rowSums(issues[2:ncol(issues)])
issues$entropy <- 0
for(r in 1:nrow(issues)) {
  curtotal <- as.numeric(issues$total[r])
  curp <- 0
  for(c in 2:ncol(issues)) {
    curcount <- as.numeric(issues[r,c])
    curp[c] <- curcount / curtotal
  }
  curp <- curp [2:length(curp)-2]
  curdrop <- which(curp==0)
  curp <- curp[-curdrop]
  issues$entropy[r] <- sum(-1 * curp * log(curp))
}

# Entropy overall
issues$total <- rowSums(issues[2:ncol(issues)])
issues$entropy <- 0
for(r in 1:nrow(issues)) {
  curtotal <- as.numeric(issues$total[r])
  curp <- 0
  for(c in 2:ncol(issues)) {
    curcount <- as.numeric(issues[r,c])
    curp[c] <- curcount / curtotal
  }
  curp <- curp [2:length(curp)-2]
  curdrop <- which(curp==0)
  curp <- curp[-curdrop]
  issues$entropy[r] <- sum(-1 * curp * log(curp))
}



# Compare total tweets vs. total sensational & total unsensational
stats_total <- data.frame(date=drange)
stats_total$tpd <- 0
stats_total$ipd <- issues_i$total
stats_total$spd <- issues$total
# Total number of tweets per day over time
for(r in 1:length(drange)) {
  stats_total$tpd[r] <- length(tweets[tweets[, "created_at"] == drange[r], "id_str"])
}

# VISUALS: Tweets per day vs. sensational vs. general findings
stats_melt <- melt(stats_total, id="date")
g_perday <- ggplot(data = stats_melt, aes(x=date,y=value,colour=variable, group=variable)) + 
  geom_line()+ 
  geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color=1) +
  xlab("Zeitraum") + ylab("Tweets pro Tag") + 
  scale_colour_discrete(name  = "Tweets", labels = c("Gesamt", "Allgemein", "Sensation")) + 
  theme(legend.title = element_text(size=14)) + 
  theme(legend.text = element_text(size=12)) + 
  theme(axis.title = element_text(size = 14))
g_perday

# Visuals for entropy in time series
stats_entropy <- data.frame(date=drange)
stats_entropy$entropy <- issues$entropy

stats_entropy <- melt(stats_entropy, id="date")

g_entrop <- ggplot(data = stats_entropy, aes(x=date,y=value,colour=variable, group=variable)) + 
  geom_line() + 
  geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color=1) +
  xlab("Zeitraum") + ylab("Entropie") + 
  scale_colour_discrete(name  = "", labels = "Entropie") + 
  theme(legend.title = element_text(size=14)) + 
  theme(legend.text = element_text(size=12)) + 
  theme(axis.title = element_text(size = 14))
g_entrop



# VAR ---------------------------------------------------------------------

# test <- VAR(issues[,2:32], p=1, type=c("const", "trend", "both", "none"), season=NULL, exogen = NULL, lag.max = NULL, ic = c("AIC", "HQ", "SC", "FPE"))
# test <- VAR(issues_i[,2:22], p=1, type="none", exogen = issues[,2:3])
# test <- VAR(issues[,2:11], p=1, type="none")
# VAR(issues[,2:23], p=1, type=c("const", "trend", "both", "none"), season=NULL, exogen = issues_i[2:22])

issues_ts <- as.ts(issues[,2:44])

# Tests
VARselect(issues_ts, lag.max = 5, type = "both")
i <- 0
i <- i + 1
ur.df(issues_ts[, i], type ="none", lags=1)
summary(issues[2:44])


# VAR and IRF
vIssues <- VAR(issues_ts, p=1, type="both")
vIRF <- irf(vIssues, impulse = names(issues[2:23]), response = names(issues_i[2:22]))
plot(vIRF)

# capture.output(print(summary(test), prmsd=TRUE, digits=1), file="out.txt")

# SOME TESTS --------------------------------------------------------------

stats <- data.frame(date=drange)
stats$tpd <- 0

# Total number of tweets per day over time
for(r in 1:length(drange)) {
  stats$tpd[r] <- length(tweets[tweets[, "created_at"] == drange[r], "id_str"])
}

stats_melt <- melt(stats, id="date")
g1 <- ggplot(data = stats_melt, aes(x=date,y=value,colour=variable, group=variable)) + 
  geom_line() + 
  geom_smooth(size=1,formula = y ~ x, method="loess", se=FALSE, color=1)
g1

rm(g1, r)


# Show party percentage of twitter users
acc_parties <- data.frame(party = c("cducsu", "spd", "gruene", "linke"))
acc_parties$btw13 <- c(49.3, 30.6, 10.0, 10.1) # seats of party / 631 seats
acc_parties$twitter <- 0
for(p in 1:nrow(acc_parties)) {
  acc_parties$twitter[p] <- round(nrow(acc_df[acc_df$party == as.character(acc_parties$party[p]), ]) / 280 * 100)
}
pie(acc_parties$btw13, col=c("black", "red", "green", "purple"), 
    labels = c("CDU/CSU (49,3%)", "SPD (30,6%)", "Bündnis 90/Grüne(10,0%)", "Die LINKE (10,1%)"), 
    clockwise = T)
pie(acc_parties$twitter, col=c("black", "red", "green", "purple"), 
    labels = c("CDU/CSU (36%)", "SPD (30%)", "Bündnis 90/Grüne(19%)", "Die LINKE (15%)"), 
    clockwise = T)

rm(acc_parties, p)


# Count all tags
num <- 0
for(i in 1:length(issuelist)) {
  j <- length(issuelist[[i]])
  num <- num + j
  rm(j)
}
num


# VISUALS -----------------------------------------------------------------


# 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)



# POSSIBLY USEFUL CODE ----------------------------------------------------

# List all issues in one row
for(i in 1:length(issueheads)) {
  cat(issueheads[i], "\n")
}

# 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")