## Individual Exercise

Use the Global Terrorism Database contained in GTD.csv to estimate a model where the number of terrorist attacks in a country-year is explained by GDP per capita and VDEM’s polyarchy score (v2x_polyarchy). WDI and the vdem packages (https://github.com/xmarquez/vdem) are your friends. Include a random intercept term by country, and allow the mean of country random intercepts to vary by year. Produce a publication quality table of your results. Is there more variation between countries or between years?

library(tidyverse)
library(WDI)
library(vdem)
library(lme4)
library(texreg)

## get GDP
wb <- WDI(indicator = c('NY.GDP.PCAP.KD'), start = 1970, end = 2014) %>%
rename(gdp = NY.GDP.PCAP.KD) %>%
select(-iso2c)

## get polyarchy
vdem <- extract_vdem(name_pattern = 'v2x_polyarchy', include_uncertainty = F) %>%
select(country = vdem_country_name, year, polyarchy = v2x_polyarchy) %>%
filter(year %in% 1970:2014)

## collapse GTD and combine data
gtd <- gtd %>% group_by(country_txt, iyear) %>%
summarize(attacks = n()) %>% # calculate attacks per year
rename(country = country_txt, year = iyear) %>% # rename columns for joining
filter(country != '') %>% # drop attacks without a country
left_join(wb) %>% # join gdp
left_join(vdem) %>% # join polyarchy
mutate_at(vars(gdp, polyarchy), scale) %>% # scale gdp and polyarchy
na.omit() # drop incomplete observations

## fit model
mod <- glmer(attacks ~ gdp + polyarchy + (1 | year / country), data = gtd, family = poisson(link = 'log'))

## present table
htmlreg(mod, stars = .05, custom.coef.names = c('Intercept', 'GDP<sub>PC</sub>',
'Polyarchy'))
Statistical models
Model 1
Intercept 2.02*
(0.05)
GDPPC -0.06
(0.04)
Polyarchy 0.17*
(0.04)
AIC 21607.07
BIC 21636.45
Log Likelihood -10798.54
Num. obs. 2634
Num. groups: country:year 2634
Num. groups: year 44
Var: country:year (Intercept) 3.15
Var: year (Intercept) 0.05
*p < 0.05

The variance of the year random intercept is 0.05 while the variance of the country random intercept is 3.15, so there is much more variation across countries than across years.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6768836,"math_prob":0.8274673,"size":2127,"snap":"2023-14-2023-23","text_gpt3_token_len":659,"char_repetition_ratio":0.13754122,"word_repetition_ratio":0.0,"special_character_ratio":0.32722145,"punctuation_ratio":0.16666667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98418355,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-03T02:06:41Z\",\"WARC-Record-ID\":\"<urn:uuid:1d31c63e-b516-4654-926f-23d4ea8fa744>\",\"Content-Length\":\"680929\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:96ba9a8f-20af-448b-9f82-7b72657b6ec2>\",\"WARC-Concurrent-To\":\"<urn:uuid:a561f60e-355f-4986-948b-ce8c4f4d1570>\",\"WARC-IP-Address\":\"104.21.50.202\",\"WARC-Target-URI\":\"https://www.manospanaousis.com/files/html/teaching-materials/Multilevel_GLMs_Key.html\",\"WARC-Payload-Digest\":\"sha1:RPMWCVTBCXYNL2KAWQHR7SVCEHLAJURI\",\"WARC-Block-Digest\":\"sha1:TM62Z24S6EZF4HHBO7FQWAQVQRHVIVDN\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224648911.0_warc_CC-MAIN-20230603000901-20230603030901-00625.warc.gz\"}"}