# Calculate Political Competition
#' pol_comp
#' @description Provides an multi-party indicator of political competition.
#' @param competitor1 first competing country.
#' @param competitor2 second competing country.
#' @param competed the country that is competed for.
#' @param years a vector of years for which to calculate the values
#' @param out the method to be used in fitting the model. The default method "pi" calculates the complete indicator while "epsilon" only calculates the distance of the competed country and "delta" calculates the distance of the two main countries.
#' @param weights apply different weights that are used for pi (defalt option is 0.5 for Epsilon and 0.5 for Delta).
#' @param important only select votes identified as important by U.S. State Department report Voting Practices in the United Nations (default is False).
#' @param issue select a specific issue dimension by Voeten (see 2004) (ME: Palestinian conflict, NU: Nuclear weapons and nuclear material, DI: arms control and disarmament, CO: colonialism, HR: human rights, EC: (economic) development). Default is "all" issues.
#' @details When analyzing the competition of multiple countries, we need to take into account how external countries compete and where the third country is placed. The first element of the measurement (delta) provides information on how countries compete in general. Second, we are interested whether a third-party country has the same distance to other countries (high competition), or if a country is closer to one and far away from the other country (low competition). Therefore, the competition measurement exists of two components: The first takes into account the competition of multiple countries in respective to the third-party country (epsilon). The second uses the information of the political competition between the other countries without the competed country (delta). The final measurement „pi“ is a product of both single estimators. The measurements limit 1 if there is a competition and zero if there is no competition.
#' @return A data frame containing output dataframe lalala
#' @examples
#' print("hallo")
#' @export
pol_comp <- function(competitor1 = "USA",
competitor2 = "RUS",
competed = "POL",
years = c(2005:2015),
out = "pi",
weights = c(0.5,0.5),
important= F,
issue= "all"){
triad <- c(competitor1,competitor2,competed)
if(!exists("completeVotes")) {
largeData <- load("inst/extdata/UN-73new_small.RData")
print(paste0('Data Source: Voeten, Erik; Strezhnev, Anton; Bailey, Michael, 2009, "UN-73new.RData", United Nations General Assembly Voting Data, https://doi.org/10.7910/DVN/LEJUQZ/KKG7SW, Harvard Dataverse, V21 '))
}
un <-
completeVotes %>%
ungroup() %>%
filter(Country %in% triad) %>%
filter(year %in% years)
rm(completeVotes)
unwide <-
un %>%
spread(Country,vote) %>%
select(rcid,triad)
match <-
left_join(un %>%
filter(Country %in% triad),
unwide,
by = c("rcid"))
# Replace vote by coding (1 = yes, 0.5 abstain, 0 no)
match2 <-
match %>%
mutate_at(vars(vote,triad),
funs(ifelse(. == 1,1,
ifelse(. %in% c(2),0.5,
ifelse(. %in% c(3),0,NA)))))
match3 <-
match2 %>%
mutate_at(vars(triad),
funs(abs(vote - .))) %>%
select(-vote)
match4 <-
if(important==T) match3[match3$importantvote==1,] else match3
match5 <-
if(issue=="all") match4 else
if (issue=="me") match4[match4$me==1,] else
if (issue=="nu") match4[match4$nu==1,] else
if (issue=="di") match4[match4$di==1,] else
if (issue=="hr") match4[match4$hr==1,] else
if (issue=="co") match4[match4$co==1,] else
if (issue=="ec") match4[match4$ec==1,]
match6 <-
match5 %>%
group_by(Country,year) %>%
summarise_at(vars(triad),
funs(mean(.,na.rm = T)))
dist_DON_DON <-
match6 %>%
ungroup() %>%
filter(Country == competitor1) %>%
select(year,Country,competitor2) %>%
gather(Country2,value,-year,-Country)
epsilon1 <-
match6%>%
ungroup() %>%
filter(Country == competed) %>%
select(-competed)
epsilon2 <-
purrr::pmap(list(epsilon1 %>% pull(competitor1),
epsilon1%>% pull(competitor2)),
function(a,b){
((abs(a-b)-1)*-1)
}) %>%
unlist() %>%
data.frame(epsilon1,epsilon = .)
names(epsilon2)[3:4] <- c("competition1","competition2")
pi <-
data.frame(dist_DON_DON,epsilon2 %>%
rename(competed = Country) %>%
select(-year)) %>%
mutate(pi = weights[1]*value * weights[2]*epsilon)
#mutate(pi = value * epsilon)
pre_out <- pi %>% select(year,
competitor1 = Country,
competition1,
competitor2 = Country2,
competition2,
competed,
delta = value,
epsilon,
pi,
)
if(out == "all") output <- pre_out else
if(out == "pi") output <- pre_out %>% select(-delta,
-epsilon,
-competition1,
-competition2) else
if(out == "epsilon") out <- pre_out %>% select(-delta,
-pi,
-competition1,
-competition2) else
if(out == "delta") out <- pre_out %>% select(-pi,
-epsilon,
-competition1,
-competition2)
return(output)
}
# Test
library(tidyverse)
system.time({
result <-
pol_comp(competitor1 = "USA",
competitor2 = "RUS",
competed = "POL",
years = c(2005:2012),
out = "all",
weights = c(0.5,0.5))
print(result)
})
# Programmatically for multiple "recipients"
recipients <- c("CMR","TGO")
library(purrr)
list_out <-
purrr::map(.x = recipients,
.f = ~ pol_comp(competitor1 = "USA",
competitor2 = "CHN",
competed = .,
years = c(2008:2012),
out = "all",
weights = c(0.5,0.5))
)
print(list_out)
# bind the lists together:
x<-print(list_out %>% bind_rows())
# or for multiple competitors as well
competitors <- c("USA","RUS","CHN")
recipients = recipients
triple_list_out <-
list_out <-
map(competitors,
function(comp1){
pmap(list(competitors[competitors != comp1],comp1),
function(comp1,comp2){
pmap(list(comp1,comp2,recipients),
function(comp1,comp2,competed){
print(comp1)
print(comp2)
print(competed)
pol_comp(competitor1 = comp1,
competitor2 = comp2,
competed = competed,
years = c(2018),
out = "all",
weights = c(0.5,0.5))
})
})
})
# bind lowest level
triple_list_out %>%
map(~ .x %>%
map(~ .x %>% bind_rows()))
# bind all levels
triple_list_out %>%
map(~ .x %>%
map(~ .x %>% bind_rows()) %>%
bind_rows()) %>%
bind_rows()
print(triple_list_out)
View(triple_list_out)
# Much more probably use case for a research project:
dataset =
expand.grid(donor = c("USA","RUS","CHN"),
lagdonor = c("USA","RUS","CHN"),
recipient = recipients,
year = 2010) %>%
filter(donor != lagdonor) %>%
mutate(aidSum = rnorm(n(),100,5),
lagAidSum = rnorm(n(),100,5)) %>%
mutate_if(is.factor,as.character)
# option1: Bind list together und left_join
competition_list_binded <-
triple_list_out %>%
map(~ .x %>%
map(~ .x %>% bind_rows()) %>%
bind_rows()) %>%
bind_rows()
joined <-
left_join(dataset,
competition_list_binded %>% mutate_at(vars(year),
funs(as.numeric(.))),
by = c("donor" = "competitor1",
"lagdonor" = "competitor2",
"recipient" = "competed",
"year" = "year"))
print(joined)
# option2: Calculate "on the fly"
dataset$pi <-
pmap(list(dataset$donor,
dataset$lagdonor,
dataset$recipient,
dataset$year),
function(a,b,c,d){
print(a)
print(b)
print(c)
print(d)
pol_comp(competitor1 = a,
competitor2 = b,
competed = c,
years = d,
out = "all",
weights = c(0.5,0.5))
}
) %>% bind_rows() %>% pull(pi)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.