knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

When using data from UCDP you can either download it and load in the file yourself or access the data using an API. This package loads in the first 5o data entries from the UCDP API and produces a map and a graph of averages for the region. This package makes it easy to load in the data to work with it.

Vignette Info

UCDP package aims to provide basic UCDP analysis 'df()' loads in a dataframe of the first 50 observations 'map_plot()' plots a map of the df1 data * 'sum_stat(df1)' produces a graph of averages of df1

df1

Using df1 you can load in a data framme of the UCDP data

library(dplyr)
library(tidyr)
library(lubridate)
library(stringr)
library(httr)
library(jsonlite)
library(ggplot2)
library(ggmap)
library(reshape2)


#get the data from UCDP API
data <- GET("http://ucdpapi.pcr.uu.se/api/gedevents/17.2?pagesize=50")

# pull the content out of the data
data_content <- content(data)

# create a null dataframe to store data in
df1 <- data.frame(NULL)

# for loop to bind data from content into a dataframe
for(i in 1:50){
  result <- as.data.frame(data_content$Result[[i]])
  df1 <- rbind(result, df1)
}


df <- function(x){

#get the data from UCDP API
data <- GET("http://ucdpapi.pcr.uu.se/api/gedevents/17.2?pagesize=50")

# pull the content out of the data
data_content <- content(data)

# create a null dataframe to store data in
df1 <- data.frame(NULL)

# for loop to bind data from content into a dataframe
for(i in 1:50){
    result <- as.data.frame(data_content$Result[[i]])
    df1 <- rbind(result, df1)
  }
return(df1)

}
head(df1$country)

map_plot()

Using map_plot() you can plot a map of the world with UCDP points on it. These points are locations of conflict according to UCDP data

# map function
map_plot <- function(){

  mapWorld <- borders("world")
  mp <- ggplot() +   mapWorld

  #Now add conflicts
  map <- mp + geom_point(data=df(), aes(x=df1$longitude, y=df1$latitude), size=2)

  return(map)
}
map_plot()

sum_stat()

Using sum_stat() you can produce a graph displaying the average number of conflicts in a region and compare it to the averages of the other regions. This uses the low, high, and best estimate

#' Sum Stats
#' produces summary statistics
#'
#' @author Mike Kozel
#' @return a graph of averages
#' @export

#sum stat function
sum_stat <- function(df1){
  #convert lat/long into a numeric
  df1$longitude <- as.numeric(df1$longitude)
  df1$latitude <- as.numeric(df1$latitude)

  df1$random <- as.Date(paste(df1$year, 1, 1, sep="-"))

  #convert dates into characters
  df1$date_end <- as.character(df1$date_end)
  df1$date_start <- as.character(df1$date_start)

  #reformat date structure
  df1$date_end <- as.Date(df1$date_end, format = "%m/%d/%Y")
  df1$date_start <- as.Date(df1$date_end, format = "%m/%d/%Y")

  df1$date_end <- format(df1$date_end, "%d/%m/%Y")
  df1$date_start <- format(df1$date_start, "%d/%m/%Y")

  #create vectors of low, high, year, region, best estimate
  low_est <- as.numeric(df1$low)
  high_est <- as.numeric(df1$high)
  year <- df1$year
  region <- df1$region
  best_est <- as.numeric(df1$best)

  ## create new dataframe based on region with low, best and high
  df2 <- data.frame(year, region)
  df2$low_est <- low_est
  df2$high_est <- high_est
  df2$best_est <- best_est

  # make region and year character and number, and add country ID
  df2$region <- as.character(df2$region)
  df2$year <- as.numeric(df2$year)
  df2$country_id <- df1$country_id

  # find averages
  df3 <- df2 %>%
    group_by(region) %>%
    summarise(mean_best=mean(best_est),
              mean_low=mean(low_est),
              mean_high=mean(high_est))


  # melt data by region
  as.character(df3$region)
  df5 <- melt(df3, id.vars = 'region')

  # make graph
  graph <- ggplot() +
    geom_bar(data=df5, aes(x=region, y=value, fill=variable), stat = 'identity') +
    labs(x='', y='Mean Casualties Per Conflict') +
    theme_bw() +
    theme(legend.title = element_blank(),
          axis.text.x = element_text(angle=45, size=10),
          axis.ticks = element_blank(),
          panel.border = element_blank()) +
    scale_y_continuous(breaks=seq(0,120, 10), limits=c(0,120)) +
    ggtitle("Average Number of Casualties from Conflict")

  #produces graph
  return(graph)
}
sum_stat(df1)


MikeKozelMSU/UCDPpack documentation built on May 16, 2019, 4:03 a.m.