Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briatte/19875675f4a59b94740f40ace6cd8df7 to your computer and use it in GitHub Desktop.
Save briatte/19875675f4a59b94740f40ace6cd8df7 to your computer and use it in GitHub Desktop.
Model terrorism patterns in the United Kingdom
# You won't need all of these packages. I just copied it from another,related analysis:
# https://github.com/svmiller/etjc
library(car)
library(arm)
library(countrycode)
library(data.table)
library(tidyverse)
library(sqldf)
# library(pdftools)
library(stringr)
library(labelled)
library(countrycode)
library(WDI)
library(mirt)
GTD93 <- readxl::read_excel("~/Dropbox/data/gtd/20160629-download/gtd1993_0616dist.xlsx")
GTD <- readxl::read_excel("~/Dropbox/data/gtd/20160629-download/globalterrorismdb_0616dist.xlsx")
States <- read_csv("~/Dropbox/data/MID/states/states2011.csv")
GTD$ccode <- countrycode(GTD$country_txt, "country.name", "cown")
GTD93$ccode <- countrycode(GTD93$country_txt, "country.name", "cown")
GTD %>%
mutate(intnl = ifelse(INT_LOG == 1 | INT_IDEO == 1 | INT_MISC == 1, 1, 0),
pubtarg = ifelse(targtype1 == 1 | targtype1 == 5 | targtype1 == 6 | targtype1 == 8 |
targtype1 == 9 | targtype1 == 14 | targtype1 == 15 |
targtype1 == 15 | targtype1 == 16 | targtype1 == 18 |
targtype1 == 19, 1, 0)) %>%
select(ccode, country_txt, iyear, imonth, iday,
success, nkill, nwound, propextent, intnl, pubtarg) -> GTDs
GTD93 %>%
mutate(intnl = NA,
pubtarg = ifelse(targtype1 == 1 | targtype1 == 5 | targtype1 == 6 | targtype1 == 8 |
targtype1 == 9 | targtype1 == 14 | targtype1 == 15 |
targtype1 == 15 | targtype1 == 16 | targtype1 == 18 |
targtype1 == 19, 1, 0)) %>%
select(ccode, country_txt, iyear, imonth, iday,
success, nkill, nwound, propextent, intnl, pubtarg) %>%
bind_rows(., GTDs) %>%
arrange(iyear, imonth, iday, ccode) %>%
rename(year = iyear, month = imonth, day = iday) -> GTDf
GTDf %>%
mutate(dependency = ifelse(is.na(ccode), 1, 0),
ccode = ifelse(country_txt == "West Germany (FRG)", 260, ccode),
ccode = ifelse(country_txt == "West Bank and Gaza Strip", 666, ccode),
ccode = ifelse(country_txt == "Guadeloupe", 220, ccode),
ccode = ifelse(country_txt == "Martinique", 220, ccode),
ccode = ifelse(country_txt == "New Caledonia", 220, ccode),
ccode = ifelse(country_txt == "Northern Ireland", 200, ccode),
# Fortunately, no HK terror incidents in 1997
ccode = ifelse((country_txt == "Hong Kong" & year < 1998), 200, ccode),
ccode = ifelse((country_txt == "Hong Kong" & year > 1997), 710, ccode),
# Fortunately, no Macau terror incidents in 1999
ccode = ifelse((country_txt == "Macau" & year < 2000), 235, ccode),
ccode = ifelse((country_txt == "Serbia" |
country_txt == "Serbia-Montenegro"), 345, ccode),
ccode = ifelse(country_txt == "French Guiana", 220, ccode),
ccode = ifelse(country_txt == "Corsica", 220, ccode),
ccode = ifelse(country_txt == "Gibraltar", 200, ccode),
ccode = ifelse(country_txt == "Falkland Islands", 200, ccode),
ccode = ifelse(country_txt == "French Polynesia", 220, ccode),
ccode = ifelse(country_txt == "Wallis and Futuna", 220, ccode),
dependency = ifelse(country_txt == "Northern Ireland" | ccode == 345 | ccode == 710, 0, dependency)
) -> GTDf
# Some recoding decisions ----------
# success: if we don't know if it was successful, it wasn't.
# nkill: if it's missing, we'll say 1. People died, but we'll be conservative.
# nwound: if it's missing, we'll say 1. People were injured, but we'll be conservative.
# propextent: if it's missing, code it as zero. This is consistent w/ GTI.
GTDf %>%
mutate(ones = 1,
success = ifelse(is.na(success), 0, success),
nkill = ifelse(is.na(nkill), 1, nkill),
nwound = ifelse(is.na(nwound), 1, nwound),
propextent = ifelse(is.na(propextent), 0, propextent)) %>%
# Might as well summarize now...
group_by(year, ccode) %>%
summarize(ones = sum(ones),
success = sum(success),
nkill = sum(nkill),
nwound = sum(nwound),
propextent = sum(propextent)) -> GTDf
# Side track to create a PDF of state-years. ----------
# States data nominally end in 2011, so let's fudge that.
States %>%
mutate(endyear = 2015) %>%
rowwise() %>%
mutate(year = list(seq(styear, endyear))) %>%
ungroup() %>%
unnest() %>%
arrange(ccode, year) %>%
select(ccode, year) %>%
filter(year >= 1970) %>%
distinct(ccode, year) -> CYs
CYs %>%
group_by(ccode, year) %>%
filter(n()>1)
CYs <- left_join(CYs, GTDf) %>%
mutate(ones = ifelse(is.na(ones), 0, ones),
success = ifelse(is.na(success), 0, success),
nkill = ifelse(is.na(nkill), 0, nkill),
nwound = ifelse(is.na(nwound), 0, nwound),
propextent = ifelse(is.na(propextent), 0, propextent))
CYs %>%
group_by(ccode) %>%
mutate(gtiraw = ones + 3*(nkill) + .5*(nwound) + 2*(propextent),
l1_gtiraw = lag(gtiraw, 1),
l2_gtiraw = lag(gtiraw, 2),
l3_gtiraw = lag(gtiraw, 3),
l4_gtiraw = lag(gtiraw, 4),
l5_gtiraw = lag(gtiraw, 5),
gtiraw5ya = 16*(l1_gtiraw) + 8*(l2_gtiraw) + 4*(l3_gtiraw) + 2*(l4_gtiraw) + l5_gtiraw,
loggti5ya = log(gtiraw5ya + 1)) %>%
group_by(year) %>%
mutate(#maxgtiraw = max(gtiraw5ya, na.rm=T),
#gtilogbase = maxgtiraw^(1/20),
loggti5ya = log(gtiraw5ya + 1, base=max(gtiraw5ya, na.rm=T)^(1/20))
) %>%
select(-l1_gtiraw, -l2_gtiraw, -l3_gtiraw, -l4_gtiraw, -l5_gtiraw) -> CYs
CYs %>%
filter(ccode == 200) -> UKG
theme_steve <- function() {
theme_bw() +
theme(panel.border = element_blank(),
plot.caption=element_text(hjust=1, size=9,
margin=margin(t=10),
face="italic"),
plot.title=element_text(hjust=0, size=18,
margin=margin(b=10),
face="bold"),
axis.title.y=element_text(size=12,hjust=1,
face="italic"),
axis.title.x=element_text(hjust=1, size=12, face="italic"))
}
UKG %>%
ggplot(.,aes(year,ones)) + geom_line() + theme_steve() +
xlab("Year") + ylab("Number of Terror Incidents") +
annotate("rect",xmin=1970,xmax=1998, ymin=0, ymax=300, alpha=.3, fill="red") +
annotate("text", x=1980, y=50, label="The Troubles\n(1968-1998)") +
annotate("text", x=2007, y=200, label="Post-9/11\n(2001-2015)") +
geom_segment(aes(x = 2001, y=0, xend=2001, yend=300), linetype="dashed", size=0.3) +
scale_x_continuous(breaks=c(seq(1970, 2015, 5))) +
ggtitle("Number of Terrorism Incidents in the United Kingdom (1970-2015)") +
labs(subtitle = "Data come from June 2016 update of Global Terrorism Database")
UKG %>%
ggplot(.,aes(year,success)) + geom_line() + theme_steve() +
xlab("Year") + ylab("Number of Successful Terror Incidents") +
annotate("rect",xmin=1970,xmax=1998, ymin=0, ymax=300, alpha=.3, fill="red") +
annotate("text", x=1980, y=50, label="The Troubles\n(1968-1998)") +
annotate("text", x=2007, y=200, label="Post-9/11\n(2001-2015)") +
geom_segment(aes(x = 2001, y=0, xend=2001, yend=300), linetype="dashed", size=0.3) +
scale_x_continuous(breaks=c(seq(1970, 2015, 5))) +
ggtitle("Number of Successful Terrorism Incidents in the United Kingdom (1970-2015)") +
labs(subtitle = "Data come from June 2016 update of Global Terrorism Database")
UKG %>%
ggplot(.,aes(year,nkill)) + geom_line() + theme_steve() +
xlab("Year") + ylab("Number of People Killed in Terror Incidents") +
annotate("rect",xmin=1970,xmax=1998, ymin=0, ymax=400, alpha=.3, fill="red") +
annotate("text", x=1980, y=50, label="The Troubles\n(1968-1998)") +
annotate("text", x=2007, y=200, label="Post-9/11\n(2001-2015)") +
geom_segment(aes(x = 2001, y=0, xend=2001, yend=400), linetype="dashed", size=0.3) +
scale_x_continuous(breaks=c(seq(1970, 2015, 5))) +
ggtitle("Number of People Killed in Terrorism Incidents in the United Kingdom (1970-2015)") +
labs(subtitle = "Data come from June 2016 update of Global Terrorism Database")
UKG %>%
ggplot(.,aes(year,nkill)) + geom_line() + theme_steve() +
xlab("Year") + ylab("Number of Terror Incidents") +
annotate("rect",xmin=1970,xmax=1998, ymin=0, ymax=400, alpha=.3, fill="red") +
annotate("text", x=1980, y=50, label="The Troubles\n(1968-1998)") +
annotate("text", x=2007, y=200, label="Post-9/11\n(2001-2015)") +
geom_segment(aes(x = 2001, y=0, xend=2001, yend=400), linetype="dashed", size=0.3) +
scale_x_continuous(breaks=c(seq(1970, 2015, 5))) +
ggtitle("Number of People Killed in Terrorism Incidents in the United Kingdom (1970-2015)") +
labs(subtitle = "Data come from June 2016 update of Global Terrorism Database")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment