This module introduces various R packages for accessing, visualizing, and analyzing spatial data, with a specific focus on examples relevant to Somalia. These skills are crucial for students in Applied Statistics and Medical Statistics & Health Data Science for applications in disease mapping and spatial analysis. This module will guide you through downloading, visualizing, and working with various types of spatial data, including administrative boundaries, climate data, elevation, OpenStreetMap data, and socio-economic data. We will also introduce essential R packages for spatial analysis, focusing on data handling, manipulation, and analysis.
We’ll use the rnaturalearth package to download
administrative boundaries for Somalia.
First we install and load necessary R libraries.
# install.packages("devtools")
# devtools::install_github("ropensci/rnaturalearthhires")
library(rnaturalearth)
library(sf)
library(ggplot2)
library(patchwork)
Here we download the map of Somalia.
somalia_map <- ne_countries(type = "countries", country = "Somalia", scale = "medium", returnclass = "sf")
Now we visualize the map of Somalia.
ggplot(somalia_map) +
geom_sf() +
ggtitle("Map of Somalia") +
theme_minimal()
Note: Somalia doesn’t have administrative divisions
within rnaturalearth. For subnational boundaries, you’d
likely need to find specific sources.
We’ll use the geodata package to download climate data
for Somalia.
library(geodata)
library(terra) # raster functionality
Here we download the minimum monthly temperature for Somalia.
somalia_tmin <- worldclim_country(country = "Somalia", var = "tmin", path = tempdir())
Here we plot the average minimum monthly temperature.
plot(mean(somalia_tmin), plg = list(title = "Mean Min. Temp"), main = "Mean Minimum Temperature in Somalia")
Using chirps package, we will download precipitation
data for Mogadishu, Somalia.
library(chirps)
Here we provide coordinates for the city of Mogadishu.
mogadishu_coords <- data.frame(long = 45.3254, lat = 2.0469)
Here we download daily precipitaion data for Mogadishu for 2 years.
mogadishu_precip <- get_chirps(mogadishu_coords, dates = c("2021-01-01", "2022-12-31"), server = "ClimateSERV")
Here we plot the daily precipitation using a line graph.
ggplot(mogadishu_precip, aes(x = date, y = chirps)) +
geom_line() +
labs(y = "Precipitation (mm)", title = "Daily Precipitation in Mogadishu") +
theme_minimal()
Let’s obtain elevation data for Somalia using elevatr
package.
library(elevatr)
We use the previously downloaded Somalia map to download the elevation data.
somalia_elev <- get_elev_raster(locations = somalia_map, z = 7, clip = "locations")
Here we visualise the elevation using terra package.
plot(rast(somalia_elev), plg = list(title = "Elevation (m)"), main = "Elevation in Somalia")
We’ll use the osmdata package to retrieve OSM data for
Mogadishu, Somalia.
library(osmdata)
library(leaflet)
Here we define a bounding box for Mogadishu.
mogadishu_bb <- getbb("Mogadishu, Somalia")
mogadishu_bb
## min max
## x 45.181918 45.501918
## y 1.874931 2.194931
Here we download the hospitals in Mogadishu area.
mogadishu_hospitals <- mogadishu_bb %>%
opq() %>%
add_osm_feature(key = "amenity", value = "hospital") %>%
osmdata_sf()
Here we download main roads (motorways) in Mogadishu area.
mogadishu_motorways <- mogadishu_bb %>%
opq() %>%
add_osm_feature(key = "highway", value = "motorway") %>%
osmdata_sf()
We’ll use the wbstats package to retrieve socio-economic
data for Somalia.
library(wbstats)
We search for relevant indicators about health and education.
indicators <- wb_search(pattern = "health|education")
# View(indicators) # Uncomment to view the retrieved indicators
Here we download the Human Development Index data for Somalia for 2019.
somalia_hdi <- wb_data(indicator = "MO.INDEX.HDEV.XQ", start_date = 2019, end_date = 2019)
print(head(somalia_hdi))
## # A tibble: 0 × 0
Using spocc we obtain the occurence data for different
species.
library(spocc)
library(sf)
Here we download locations of occurrences of wild dog (Lycaon pictus) in Somalia.
somalia_wild_dogs <- occ(query = "Lycaon pictus", from = "gbif",
date = c("2000-01-01", "2023-12-31"),
gbifopts = list(country = "SO"),
has_coords = TRUE, limit = 1000)
d_dogs <- occ2df(somalia_wild_dogs)
In addition to the packages used above for data access and visualization, several R packages are essential for spatial analysis. These packages provide tools for spatial statistics, spatial econometrics, and point pattern analysis. Here’s an overview of some key packages:
spdepspdep package provides
tools for spatial dependence analysis, including spatial autocorrelation
measures, spatial weights matrices, and spatial regression
diagnostics.poly2nb(): Creates neighborhood lists from polygon
data.nb2listw(): Creates spatial weights lists from
neighborhood lists.moran.test(): Performs Moran’s I test for spatial
autocorrelation.lm.morantest(): Performs Moran’s I test for linear
model residuals.spatialregspatialreg package
provides functions for fitting spatial regression models, including
spatial lag, spatial error, and spatial Durbin models.lagsarlm(): Fits spatial lag models.errorsarlm(): Fits spatial error models.spatial.diagnostics(): Performs diagnostic tests for
spatial regression models.spatstatspatstat package is a
comprehensive suite of tools for analyzing spatial point patterns,
including point process models, intensity estimation, and spatial
clustering.ppp(): Creates a point pattern object.density(): Estimates the intensity of a point
pattern.Kest(): Calculates the K-function for point pattern
analysis.ppm(): Fits point process models.sf (already used)sf package provides a
unified way to work with spatial vector data, including reading,
writing, manipulating, and visualizing spatial data.st_read(): Reads spatial data from various
formats.st_write(): Writes spatial data to various
formats.st_transform(): Transforms spatial data to a different
coordinate reference system.st_join(): Joins spatial data with attribute data.These packages provide functions for data handling, manipulation, and analysis:
sf: Use st_read() to
import spatial data from various formats (e.g., shapefiles, GeoJSON) and
st_write() to export spatial data.raster and terra: Use
raster() or rast() to import raster data and
writeRaster() to export raster data.read.csv() and
write.csv(): Use these functions to import and
export attribute data.sf: Use functions like
st_transform() for coordinate transformations,
st_buffer() for creating buffers,
st_intersection() for spatial intersections, and
st_union() for spatial unions.dplyr: Use functions like
filter(), select(), mutate(), and
group_by() for data manipulation.terra: Use functions like
crop(), resample(), and
aggregate() for raster data manipulation.spdep: Use functions like
moran.test() and localmoran() to assess
spatial autocorrelation.spatialreg: Use functions like
lagsarlm() and errorsarlm() to fit spatial
regression models.spatstat: Use functions like
density(), Kest(), and ppm() to
analyze point patterns.raster and terra: Use
functions for raster calculations, such as overlay() and
distance().The examples in this module demonstrate how to use these packages for hands-on data analysis. For instance, you can:
spdep.spatialreg.spatstat.This module introduced you to several R packages for accessing and working with spatial data. You can apply these skills to various applications in disease mapping and spatial analysis, particularly relevant to countries like Somalia where data can be challenging to obtain. We have also introduced essential R packages for spatial analysis, focusing on data handling, manipulation, and analysis.
wbstats.This module provides a foundation for spatial data analysis; it is highly recommended that the students further explore and extend upon these methods.