library(tidyverse)
library(lubridate)

Load datasets

mic <- readRDS("~/.rebeltrack/ucdp/mediation/micFINAL.rds")
term <- readRDS("~/.rebeltrack/ucdp/termination-conflict/ucdp-term-conf-2015.rds")

Code for parsing dates

parse_mixed_date_formats <- function(x) {
  parse_dates <- function(s, func = max, origin = "1900-01-01") {
    if (is.na(s))
      return(s)
    
    value <- suppressWarnings(as.numeric(s))
    
    if (!is.na(value)) 
      value <- as.Date(value, origin = origin) 
    else {
      value <- s %>% 
        str_split(";") %>% 
        unlist() %>% 
        trimws() %>% 
        as.Date() %>% 
        func()
    }
    return(as.character(value))
  }
  
  x %>% 
    map(parse_dates) %>% 
    unlist() %>% 
    as.POSIXct()
}

Get the end date of a conflict from termination dataset

term <- term %>% 
  filter(Outcome %in% c(1,2), 
         EpEndPrec <= 2) %>% 
  mutate(ceasefire_date = parse_mixed_date_formats(CfireDate)) %>% 
  mutate(peace_agreement_date = parse_mixed_date_formats(PeAgDate)) %>% 
  group_by(ConflictId) %>% 
  summarise(end_date = max(EpEndDate, na.rm = TRUE))

Merge mediation data with conflict termination dataset

fix_mediator_names <- function(s) {
  str_replace(s, "\\(shiite, sunni etc.\\)", "")
}

mediators <- mic %>% 
  mutate(ConflictId = paste("1", conflict_id, sep="-")) %>% 
  mutate(start_date = parse_date_time(start_event, "mdy"))  %>% 
  mutate(mediator_list = fix_mediator_names(third_name)) %>% 
  mutate(mediator = str_split(mediator_list, ",")) %>% 
  unnest(mediator) %>% 
  mutate(mediator = trimws(mediator)) %>% 
  group_by(ConflictId, mediator) %>% 
  summarize(
    conflict_name = unique(conflict_name),
    start_date = min(start_date, na.rm = TRUE)
  ) %>% 
  left_join(term, by = "ConflictId") %>% 
  filter(!is.na(end_date))
head(mediators)
## # A tibble: 6 x 5
## # Groups:   ConflictId [1]
##   ConflictId mediat… conflict_name start_date          end_date           
##   <chr>      <chr>   <chr>         <dttm>              <dttm>             
## 1 1-131      Benin   Angola        1995-06-14 00:00:00 2002-04-01 00:00:00
## 2 1-131      Congo   Angola        1995-11-25 00:00:00 2002-04-01 00:00:00
## 3 1-131      Democr… Angola        1994-07-07 00:00:00 2002-04-01 00:00:00
## 4 1-131      Gabon   Angola        1995-08-10 00:00:00 2002-04-01 00:00:00
## 5 1-131      Ivory … Angola        1993-09-14 00:00:00 2002-04-01 00:00:00
## 6 1-131      Lesotho Angola        1994-11-19 00:00:00 2002-04-01 00:00:00
mediators <- mediators %>% 
  mutate(mediation_duration = end_date - start_date) %>% 
  group_by(mediator) %>% 
  summarize(mediation_duration_mean = mean(mediation_duration)) %>% 
  filter(mediation_duration_mean > 0) 

Plot the results

ggplot(
  mediators, 
  aes(
    reorder(mediator, -mediation_duration_mean), 
    mediation_duration_mean)
  ) +
  geom_point() +
  coord_flip() +
  ggtitle("Peace Negotiation Duration by Mediator") +
  ylab("Mean # of days (from start of negtiation to agreement)") +
  theme_minimal() +
  theme(axis.text = element_text(size = 8),
        axis.title.y = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5)
  )
## Don't know how to automatically pick scale for object of type difftime. Defaulting to continuous.