-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path04_ana_armed_conflicts.Rmd
91 lines (67 loc) · 3.52 KB
/
04_ana_armed_conflicts.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---
title: "Data analysis: Armed conflicts locations"
author: "Linda Petutschnig"
date: "25 5 2023"
output:
html_document:
theme: paper
highlight: default
---
This script performs the following tasks:
- Applies a hotspot classification algorithm using a hexagonal grid as atomic units, categorizing the hexagons based on the number of ACLED events during a specified period.
- Converts the hotspot classes (no pattern, former hotspot, emerging hotspot, intermittent hotspot, persistent hotspot) into numeric values for better integration with other risk indicators.
- Writes the results to the hard drive ("hotspots_3months.gpkg").
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r load, message = FALSE, warning = FALSE}
# Loads necessary packages
library(sf) # to manipulate vector features
library(dplyr) # for tidy data manipulation
library(here) # to access data via relative paths
library(sfhotspot) # to perform hotspot analysis based on vector features
# Add aceld.api
# https://www.rdocumentation.org/packages/acled.api/versions/1.1.5
# Citing ACLED: https://developer.acleddata.com/dashboard/terms-of-use/
```
```{r emerging hotspots, message = FALSE, warning = FALSE}
# Reads the hexagon shapes from hard drive
aoi_shape <- st_read(here("data/geopackages","AOI_hex5.gpkg")) %>%
st_cast("POLYGON")
# Renames the column geom to geometry
st_geometry(aoi_shape) <- "geometry"
# Reads the ACLED data downloaded in acc_armed_conflicts.Rmd
acled_17_20_aoi <- st_read(here("data/geopackages","acled_17_20_aoi.gpkg"))
# As of 11.07.23 the hotspots_classify function gave an error, because in the just loaded dataset, some edges have duplicate
# vertexes with other edges. This has not been a problem before.
# Adding the bit below solves the issue.
sf_use_s2(FALSE)
# Calls the hotspot function on the just downloaded ACLED data
# The hexagons are used as grid. For "period", 1,3,6 and 12 months were tested and visually compared.
# 3 months were chosen for the analysis because it gave the most
# distinct pattern. Other periods may work as well.
hotspots_3m <- hotspot_classify(acled_17_20_aoi, time = event_date, period = "3 months", grid = aoi_shape)
# Plots the resulting hotspots
autoplot(hotspots_3m)
# Writes a column with the hotspots classes into aoi_shape
aoi_shape$hotspot <- hotspots_3m$hotspot_category
# The hotspot categories are changed from categorical to nominal values to enable integration with the rest of the indicators.
aoi_shape$hotspot_num[hotspots_3m$hotspot_category == 'no pattern'] <- "0"
aoi_shape$hotspot_num[hotspots_3m$hotspot_category == 'former hotspot'] <- "20"
aoi_shape$hotspot_num[hotspots_3m$hotspot_category == 'emerging hotspot'] <- "50"
aoi_shape$hotspot_num[hotspots_3m$hotspot_category == 'intermittent hotspot'] <- "80"
aoi_shape$hotspot_num[hotspots_3m$hotspot_category == 'persistent hotspot'] <- "100"
# Converts the newly defined values to numeric format
aoi_shape$hotspot_num <- as.numeric(aoi_shape$hotspot_num)
# Converts the result back into a MULTIPOLYGON, so that it can be matched with the other indicators.
aoi_shape_multipolygon <- aoi_shape %>%
group_by(h3_index) %>%
summarise(geometry = st_combine(geometry),
hotspot_num = first(hotspot_num)) %>%
st_cast("MULTIPOLYGON")
# Creates the map object
tmap::tm_shape(aoi_shape_multipolygon) +
tmap::tm_polygons(col = "hotspot_num")
# Writes the results to hard drive
st_write(aoi_shape_multipolygon, here("data/geopackages","AOI_hex5_ind1.gpkg"), append = FALSE)
```