Last updated: 2020-11-25

Checks: 7 0

Knit directory: GeoPKO/

This reproducible R Markdown analysis was created with workflowr (version 1.6.2). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20200629) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version b6fea7a. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Untracked files:
    Untracked:  data/ViEWS/

Unstaged changes:
    Modified:   analysis/_site.yml

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/merge.Rmd) and HTML (docs/merge.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd b6fea7a Nguyen Ha 2020-11-25 Revised viz, index, incorporated merge attempt 2.rmd
html 2011bda Nguyen Ha 2020-11-25 Build site.
Rmd 448b94c Nguyen Ha 2020-11-25 Revised viz, index, incorporated merge.rmd
Rmd ad652a3 Tanushree Rao 2020-11-24 changed ref to geopko so it opens with v1 data
Rmd 6d15379 Tanushree Rao 2020-11-24 updating merge page with new data
Rmd 2608fc0 Tanushree Rao 2020-09-10 merge file
Rmd 9174a7c Tanushree Rao 2020-09-09 adding kables
Rmd 113774e Tanushree Rao 2020-09-04 edits to data
Rmd 5afdf38 Tanushree Rao 2020-09-03 adding merge page

This page shows how to merge Geo-PKO data with conflict data and visualise the results. The examples used here are Uppsala University’s ViEWS project, which forecasts conflict risk, and the Uppsala Conflict Data Programme (UCDP), one of the world’s leading sources of data on armed conflict. Merging these datasets can provide insights into the links between conflict risk and peacekeeping deployments, and help policymakers make effective peacekeeping decisions where the risk of conflict is high.

Setting up

Load packages.

library(dplyr)
library(sp)
library(tidyr)
library(geojsonio)
library(broom)
library(rgdal)
library(ggplot2)
library(leaflet)
library(sf)
library(spdep)
library(maptools)
library(plyr)
library(rjson)
library(RJSONIO)
library(rmapshaper)
library(htmltools)
library(htmlwidgets)

Geo-PKO x ViEWS

First, we import the datasets. We’re using the published Geo-PKO dataset, and conflict forecast data from ViEWS for state-based conflict, non-state conflict, and one-sided violence over the next 36 months in Africa. Both datasets offer insights into the sub-national level, using a unique “PRIO-grid” identifier. The PRIO-grid is an innovative geospatial unit from the Peace Research Institute Oslo that divides the world into roughly 100km x 100km squares, allowing geographic analysis beyond the country level to be streamlined.

geopko <- read.csv("data/Geo_PKO_v2_ISO3.csv") #replace file later
#unzip("data/ViEWS.zip", exdir="data/ViEWS")
predictors <- read.csv("data/ViEWS/ensemble_pgm.csv")

The Geo-PKO dataset includes detail on troop deployment numbers, types of troops, non-troop deployments, and contributing countries.

library(kableExtra)

Attaching package: 'kableExtra'
The following object is masked from 'package:dplyr':

    group_rows
kable(geopko[9546:9550,]) %>% kable_styling() %>%
  scroll_box(width = "100%", height = "200px")
X Source Mission joined_date timepoint year month location geosplit country latitude longitude old_xy geocomment zone.de.confidence battalion company platoon other.size Comment.on.unit No.troops RPF RPF_No Inf. Inf_No FPU FPU_No RES RES_No FP FP_No Eng Sig Trans Riv HeSup SF Med Maint Recon Avia MP Demining UAV Obs.Base Cantonment Disarmament Other.Type Armor HeSup_LW Troop.type No.TCC nameoftcc_1 notroopspertcc_1 nameoftcc_2 notroopspertcc_2 nameoftcc_3 notroopspertcc_3 nameoftcc_4 notroopspertcc_4 nameoftcc_5 notroopspertcc_5 nameoftcc_6 notroopspertcc_6 nameoftcc_7 notroopspertcc_7 nameoftcc_8 notroopspertcc_8 nameoftcc_9 notroopspertcc_9 nameoftcc_10 notroopspertcc_10 nameoftcc_11 notroopspertcc_11 nameoftcc_12 notroopspertcc_12 nameoftcc_13 notroopspertcc_13 nameoftcc_14 notroopspertcc_14 nameoftcc_15 notroopspertcc_15 nameoftcc_16 notroopspertcc_16 nameoftcc_17 notroopspertcc_17 UNPOL.dummy UNMO.dummy coding.quality.for.UNMO..1.unsure..0.perfectly.fine. HQ LO JMCO Security.Group..dummy. comments cow_code gnwo TCC1 TCC2 TCC3 TCC4 TCC5 TCC6 TCC7 TCC8 TCC9 TCC10 TCC11 TCC12 TCC13 TCC14 TCC15 TCC16 TCC17 ADM1_id ADM1_name PRIOID iso3c MonthName
9546 9546 Map no. 4249 Rev. 08 UNMIS 2007: August 2007 August 2007 8 Bentiu 0 Sudan 9.233333 29.83333 NA GNS has two locations pointing to basically the same place. I took the coords for “seat of first-order admin division”, rahter than “populated place”, both are in line with GE. Also, today’s south sudan NA 0 1 0 0 NA 150 NA NA 1 150 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 NA NA NA 0 0 NA 1 1 India 150 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0 1 0 0 0 0 0 NA 625 625 750 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 622 Unity 142980 SDN August
9547 9547 Map no. 4249 Rev. 08 UNMIS 2007: August 2007 August 2007 8 Bor 0 Sudan 6.205931 31.55633 NA GNS has two locations, the “seat of first-order admin division” pointing to location of Google Earth by same name, as well as corresponding to source map, hence that one coded. Also, today’s south sudan NA 0 0 0 0 NA 0 NA NA 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 NA NA NA 0 0 NA 0 0 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0 1 0 0 0 0 0 NA 625 625 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 621 Jungoli 138664 SDN August
9548 9548 Map no. 4249 Rev. 08 UNMIS 2007: August 2007 August 2007 8 Dilling 0 Sudan 12.050000 29.65000 NA NA NA 0 0 0 0 NA 0 NA NA 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 NA NA NA 0 0 NA 0 0 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0 1 0 0 0 0 0 NA 625 625 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 720 South Kordufan 147300 SDN August
9549 9549 Map no. 4249 Rev. 08 UNMIS 2007: August 2007 August 2007 8 Ed Damazin 0 Sudan 11.789100 34.35920 NA NA NA 1 4 0 0 NA 1250 NA NA 1 650 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 NA NA NA 0 0 NA 1, 2, 4, 11, 14 1 Pakistan 1250 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0 1 0 2 0 0 0 NA 625 625 770 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 287 Blue Nile 146589 SDN August
9550 9550 Map no. 4249 Rev. 08 UNMIS 2007: August 2007 August 2007 8 El Obeid 0 Sudan 13.183333 30.21667 NA NA NA 0 1 0 0 NA 150 NA NA 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 NA NA NA 1 0 NA 99 NA unknown 150 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 0 0 NA 0 0 0 0 “other” troop type refers to LOG company, unknown TCC 625 625 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 721 North Kordufan 148741 SDN August

Filtering, joining and subsetting the datasets at the PRIO-grid level

The predictor database begins with July 2020 and forecasts the risk of conflict over the next 36 months ahead. Here’s a preview of the data within it, showing state-based (sb), non-state (ns) and one-sided violence (os) forecasts. The variable month_id codes months differently to the Geo-PKO dataset, with every month assigned a different numeric value.

kable(predictors[90545:90550,]) %>% kable_styling() %>%
  scroll_box(width = "100%", height = "200px")
month_id pg_id average_allwthematic_sb average_allwthematic_ns average_allwthematic_os
90545 494 141560 0.0056672 0.0040548 0.0049985
90546 494 141561 0.0054137 0.0032145 0.0038274
90547 494 141562 0.0115811 0.0073864 0.0045633
90548 494 141563 0.0093885 0.0043294 0.0027875
90549 494 141564 0.0085510 0.0184678 0.0043437
90550 494 141565 0.0098269 0.0046215 0.0037409

The Geo-PKO dataset we’re working with includes data from previous years, but for this visualisation, we will only use the latest year (July 2019 - June 2020). First, we need to filter the data to include only that period. This way we’ll be looking at deployment status against projected conflict risk from the end of that period until three years from that period. You’ll see both the Geo-PKO and ViEWS datasets include the PRIO-grid identification variable (pg_id or PRIOID), which corresponds to a specific grid square on the map. This is what we’ll use to merge the datasets. In addition to filtering for the time period we want, we will also calculate the average number of troops deployed over that time period.

# filtering for troop deployments over Jul 2019 - Jun 2020

geopko2 <- geopko %>%
  select(Mission, year, month, PRIOID, No.troops, country, location, latitude, longitude) %>%
  filter(year==2019 & month>6 | year==2020 & month<7)

geopko2$No.troops <- as.numeric(geopko2$No.troops)

# calculating an average number of troops

geopko3 <- geopko2 %>% 
  group_by(PRIOID) %>%
  dplyr::mutate(No.troops = mean(No.troops, na.rm=TRUE)) %>%
  ungroup() %>%
  filter (! duplicated(No.troops))

geopko3$No.troops <- round(geopko3$No.troops)

Here, we also calculate the average conflict risk (state-based, non-state, and one-sided) for each location.

predictors2 <- predictors %>% 
  group_by(pg_id) %>%
  dplyr::mutate(average_allwthematic_sb = mean(average_allwthematic_sb, na.rm=TRUE)) %>%
  dplyr::mutate(average_allwthematic_ns = mean(average_allwthematic_ns, na.rm=TRUE)) %>%
  dplyr::mutate(average_allwthematic_os = mean(average_allwthematic_os, na.rm=TRUE)) %>%
  filter (! duplicated(average_allwthematic_sb))

Finally, we merge the two datasets.

# merging geopko with conflict forecast data

geopko3$pg_id <- geopko3$PRIOID

priogriddf <- full_join(
  geopko3, predictors2,
  by = c("pg_id"), 
  na.rm = TRUE)

Merging the data and preparing the shapefile

Like we mentioned before, the PRIO-grid unit involves dividing the entire world into roughly 100km x 100km squares. That means that if we want to map it, we’ll be working with large files, so keep that in mind when you’re reading in the shapefile:

shapefile <- rgdal::readOGR("data/ViEWS/priogrid.geojson")
OGR data source with driver: GeoJSON 
Source: "C:\Users\Nguyen Ha\Documents\DPCR\GeoPKO\Pages\GeoPKO\data\ViEWS\priogrid.geojson", layer: "priogrid"
with 64818 features
It has 1 fields

The shapefile contains both geospatial polygon data and numerical data that corresponds to the ViEWS dataset; specifically, a PRIO-grid ID and a country ID. Here’s what the non-spatial data looks like, showing five rows in the dataset.

kable(shapefile@data[101:106,]) %>% kable_styling() %>%
  scroll_box(width = "100%", height = "200px")
x
54217
54218
54219
54220
54221
54222

To work with the data within this shapefile, we need to fortify the shapefile. We also convert the IDs to rownames to make it easier to work with. And, finally, we merge it with pgnewdf2, which we created earlier.

# fortify
shapefile@data$id <- rownames(shapefile@data)
shapefile.df <- fortify(shapefile, region = "id")

# merge data of interest
shapefile.df <- merge(shapefile, priogriddf, by.x = "pg_id", by.y = "pg_id", all.x=F, all.y=T, duplicateGeoms=TRUE)

So now shapefile.df has the new attributes, including variables from both Geo-PKO and ViEWS. id is a variable that ties the ‘polygon’, or a single square on the grid, to its location on the map

kable(shapefile.df@data[356:360,]) %>% kable_styling() %>%
  scroll_box(width = "100%", height = "200px")
pg_id id Mission year month PRIOID No.troops country location latitude longitude month_id average_allwthematic_sb average_allwthematic_ns average_allwthematic_os
356 89685 3245 NA NA NA NA NA NA NA NA NA 486 0.0024289 0.0015213 0.0013923
357 89686 3246 NA NA NA NA NA NA NA NA NA 486 0.0029618 0.0021277 0.0020474
358 89687 3247 NA NA NA NA NA NA NA NA NA 486 0.0026179 0.0018690 0.0020587
359 89688 3248 NA NA NA NA NA NA NA NA NA 486 0.0023497 0.0017310 0.0018626
360 89689 3249 NA NA NA NA NA NA NA NA NA 486 0.0023957 0.0016043 0.0016756

Mapping Geo-PKO and ViEWS data

To map the data, we’re going to use the leaflet package (and a bunch of others to support it). The first thing we do is set up our colour palette and bins. We’re using the ‘viridis’ colour palette, designed for accessibility and continuous-scale representation. The other thing we include is a small segment of code that fixes spacing between any NA value in the legend, and the remainder of the legend, making it easier to see.

bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorNumeric("viridis", NULL)

#to fix spacing of NA in legend
css_fix <- "div.info.legend.leaflet-control br {clear: both;}" # CSS to correct spacing
html_fix <- htmltools::tags$style(type = "text/css", css_fix)  # Convert CSS to HTML

Next, let’s map. We include three colour layers to shade squares according to their conflict forecast value. These layers cover state-based conflict, non-state conflict, and one-sided violence. Simple markers show where troops are deployed. Troop deployment numbers are included as labels, which you can see for each square on hover.

map <- leaflet(shapefile.df) %>%
  addTiles() %>%
  addPolygons(color = "#444444", weight = 0.25, smoothFactor = 0.5,
              opacity = 0.05, fillOpacity = 0.4,
              fillColor = ~pal(shapefile.df$average_allwthematic_sb),
              group = "State-Based Conflict",
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = FALSE)) %>%
  addPolygons(color = "#444444", weight = 0.25, smoothFactor = 0.5,
              opacity = 0.05, fillOpacity = 0.4,
              fillColor = ~pal(shapefile.df$average_allwthematic_ns),
              group = "Non-State Conflict",
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = FALSE)) %>%
  addPolygons(color = "#444444", weight = 0.25, smoothFactor = 0.5,
              opacity = 0.05, fillOpacity = 0.4,
              fillColor = ~pal(shapefile.df$average_allwthematic_os),
              group = "One-Sided Violence",
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = FALSE)) %>%
    addCircleMarkers((data = shapefile.df@data$No.troops>0), lat = ~latitude, lng = ~longitude, 
              weight = 1, radius = 2, fillOpacity = 0.6, color = "darkblue") %>%
  addPolygons(color = "#444444", weight = 0.1, smoothFactor = 0.5,
              opacity = 0.0, fillOpacity = 0.0,
              fillColor = ~pal(shapefile.df$No.troops),
              label=paste("Troops Deployed: ", shapefile.df$No.troops),
              labelOptions = labelOptions(
                style = list("font-weight" = "normal", padding = "3px 8px", color="blue"),
                textsize = "15px", direction = "auto"),
              highlightOptions = highlightOptions(color = "white", weight = 2,
                                                  bringToFront = FALSE)) %>%
  addLegend("bottomright",
            pal = pal,
            values = shapefile.df$average_allwthematic_sb,
            title = "Conflict Forecast",
            opacity = 1) %>%
  
  addLayersControl(
    baseGroups = c("State-Based Conflict", "Non-State Conflict", "One-Sided Violence"),
    options = layersControlOptions(collapsed = FALSE)
  )
Warning in validateCoords(lng, lat, funcName): Data contains 10630 rows with
either missing or invalid lat/lon values and will be ignored
map <- map %>% htmlwidgets::prependContent(html_fix) # legend NA fix

# to save as HTML, you can use the following code:
# saveWidget(map, file="pkoviews - priogrid.html")

map

And here we have it: an interactive map to view recent peacekeeping deployments (2019-2020) and projected conflict risk over the next 36 months. Extensions of this visualisation can be even more useful, particularly with a time-slider that can help us identify how the risk of conflict changes given peacekeeping deployments (and vice versa). That might be a project for the future.

Geo-PKO x UCDP

Another useful dataset is from the Uppsala Conflict Data Programme, which offers insights into deaths from armed conflict. Looking at this data in conjunction with peacekeeping data can be useful to draw conclusions into peacekeeping given the severity of armed conflict, or lack thereof. How to merge with this dataset, and a few examples, will be added soon.


sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Sweden.1252  LC_CTYPE=English_Sweden.1252   
[3] LC_MONETARY=English_Sweden.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Sweden.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] kableExtra_1.1.0  htmlwidgets_1.5.1 htmltools_0.5.0   rmapshaper_0.4.4 
 [5] RJSONIO_1.3-1.4   rjson_0.2.20      plyr_1.8.6        maptools_1.0-2   
 [9] spdep_1.1-5       spData_0.3.8      sf_0.9-5          leaflet_2.0.3    
[13] ggplot2_3.3.2     rgdal_1.5-16      broom_0.7.0       geojsonio_0.9.2  
[17] tidyr_1.1.1       sp_1.4-2          dplyr_1.0.2       workflowr_1.6.2  

loaded via a namespace (and not attached):
 [1] nlme_3.1-148       fs_1.5.0           RColorBrewer_1.1-2 webshot_0.5.2     
 [5] httr_1.4.2         gmodels_2.18.1     rprojroot_1.3-2    tools_4.0.2       
 [9] backports_1.1.7    R6_2.4.1           KernSmooth_2.23-17 rgeos_0.5-3       
[13] DBI_1.1.0          lazyeval_0.2.2     colorspace_1.4-1   raster_3.3-13     
[17] withr_2.2.0        gridExtra_2.3      tidyselect_1.1.0   curl_4.3          
[21] compiler_4.0.2     git2r_0.27.1       rvest_0.3.6        expm_0.999-5      
[25] xml2_1.3.2         scales_1.1.1       classInt_0.4-3     readr_1.3.1       
[29] stringr_1.4.0      digest_0.6.25      foreign_0.8-80     rmarkdown_2.3     
[33] pkgconfig_2.0.3    highr_0.8          jsonvalidate_1.1.0 rlang_0.4.7       
[37] rstudioapi_0.11    httpcode_0.3.0     farver_2.0.3       generics_0.0.2    
[41] jsonlite_1.7.1     crosstalk_1.1.0.1  gtools_3.8.2       magrittr_1.5      
[45] Matrix_1.2-18      Rcpp_1.0.5         munsell_0.5.0      viridis_0.5.1     
[49] lifecycle_0.2.0    stringi_1.4.6      whisker_0.4        yaml_2.2.1        
[53] MASS_7.3-52        jqr_1.1.0          grid_4.0.2         gdata_2.18.0      
[57] promises_1.1.1     crayon_1.3.4       deldir_0.2-3       lattice_0.20-41   
[61] splines_4.0.2      geojson_0.3.4      hms_0.5.3          knitr_1.29        
[65] pillar_1.4.6       boot_1.3-25        geojsonlint_0.4.0  codetools_0.2-16  
[69] LearnBayes_2.15.1  crul_1.0.0         glue_1.4.1         evaluate_0.14     
[73] V8_3.2.0           vctrs_0.3.2        httpuv_1.5.4       gtable_0.3.0      
[77] purrr_0.3.4        xfun_0.16          e1071_1.7-3        coda_0.19-4       
[81] later_1.1.0.1      viridisLite_0.3.0  class_7.3-17       tibble_3.0.3      
[85] units_0.6-7        ellipsis_0.3.1