#============================================
# 2. Load and visualize the data
#============================================
### 2.1 Load R libraries
#-----------------------
# library(dplyr) # deals with dataframes
library(sf) # spatial objects
library(mapsf) # Plot
### 2.2 Spatial data
#----------------------
# Load data
<- st_read("data/khm_admbnda_adm1_gov_20181004.shp", quiet = TRUE)
province_sf head(province_sf) # We need to define the projection
# set crs
<- st_transform(province_sf, crs = 32648)
province_sf
# Visualize
class(province_sf) # type of R object
dim(province_sf) # dimensions of the object = n columns + 1 geometry
summary(province_sf) # Summarize the information of the object
plot(province_sf[,1]) # Plot the first column to look at geometry
# (if you plot all of column it is sometimes too long)
### 2.3 Population data
#--------------------------
# Load
<- read.table(file = "data/Population_district_Cambodia.csv",
population_df sep = ',', header = TRUE)
# Visualize
head(population_df)
class(population_df)
dim(population_df)
colnames(population_df)
summary(population_df)
# Because we work on province we need to aggregate the values
# Tips: ?aggregate
<- aggregate(population_df$T_POP,
pop_by_district_df by = list(ADM1_PCODE = population_df$ADM1_PCODE),
FUN = sum)
colnames(pop_by_district_df) <- c("ADM1_PCODE", "pop") # rename column for later
### 2.4 Cases
#-------------------------
# Load number of cases
<- read.table(file = "data/R_infection_monthly_cases.csv",
cases_df header = TRUE, sep = ";")
head(cases_df)
dim(cases_df)
colnames(cases_df)
summary(cases_df)
# What are these data ?
# What represents each row ? each column ?
# What is the time span ?
Exercice
This exercice aims at summarizing what was shown througout the training. It includes dealing with shapefile and epidemiological data in “almost real” conditions. The data represents the number of cases of an imaginary disease across Cambodia called R infections from 2018 to 2022. This infection spread rapidly and started during a training in Phnom Penh. Symptoms are very specific and includes installing Rstudio, loading data and using R software for spatial analysis and mapping.
In R, it exist many differents implementation solution that lead to the same results. The solution presented here just provides one implementation among thousands of possibilities.
Comment your code ! (
# important informations on the code
)Check your R objects ! (
plot()
,print()
,View()
, …)Listen to R outputs ! (Errors AND Warnings)
Get help ! (
?name_of_function
, internet, other users)Keep calm and take a break !
Create your working environment
Create a R project called “RGeotraining”
Download and unzip the training data into a directory called “data/”.
Create a directory called “img/” for image outputs
Download and unzip shapefile of Cambodian provinces in the ‘data/’ directory: Province shapefile. A new province have been recently delimited in Cambodia (the 26th one). for this exercice you can use the old shapefile that contains 25 provinces in the data directory.
Check your working directory using
getwd()
Load and visualize the data
Steps
Load R libraries
sf
andmapsf
,Load province shapefile with
st_read()
and set the projectionst_transform()
,Load population data with
read.table()
and sum the counts over provinces usingaggregate()
,Load the number of cases per province from the csv file.
Solution
Merge data with shapefiles
Steps
Merge population with sf polygon using
merge()
,Merge the new sf object with number of cases,
Identify the merging issues and correct the datasets, you can compares columns of the datasets using boolean (
TRUE
/FALSE
) operation as==
(is equal to),%in%
(appear in at least once),!
(negate, invertTRUE
andFALSE
values) and extract row of interest using[row_selection, column_selection]
.
Solution
#=======================================================
# 3. Merge all data
#=======================================================
### 3.1 Merge population with sf polygons to prepare for mapping
#---------------------------------------------------------------
# What is the key column to merge the objects (column with IDs) ?
<- merge(pop_by_district_df, province_sf, by = "ADM1_PCODE")
province_pop_sf class(province_pop_sf) # What a mess !!! We lost the geometry !
<- merge(province_sf, pop_by_district_df, by = "ADM1_PCODE")
province_pop_sf class(province_pop_sf) # Much better !!!
head(province_pop_sf)
# Create a maps to represent population
mf_export(province_pop_sf, filename = 'img/Population_province.png', width = 500, res = 100)
mf_map(province_pop_sf)
mf_map(province_pop_sf,
var = "pop" ,
inches = .2,
type = "prop",
col = "#000066",
leg_title = "Population")
mf_layout(title = "Population in cambodian provinces")
dev.off()
### 3.2 Merge polygon with number of cases
#---------------------------------------------------------------
# What is (are) the key column(s) ?
<- merge(province_pop_sf, cases_df, by.x = 'ADM1_EN', by.y = "Province")
province_pop_cases_sf class(province_pop_cases_sf) # Perfect !
dim(province_pop_cases_sf) # Why do I have only 18 province instead of 25 ? 7 missing values
# detect names from cases_df that do not match names in province_pop_sf
!(cases_df$Province %in% province_pop_sf$ADM1_EN) # We have 6 provinces that do not match the names (FALSE value)!
sum(!(cases_df$Province %in% province_pop_sf$ADM1_EN))
# which ones ?
$Province[!(cases_df$Province %in% province_pop_sf$ADM1_EN)]
cases_df
# What are the names of these provinces in the sf file ?
$ADM1_EN[!(province_pop_sf$ADM1_EN %in% cases_df$Province)]
province_pop_sf# Some names do not have the same spelling ...
### 3.4 Correct the province names
#----------------------------------------------------------
## OPTION 1 : Open the csv file and change the names one by one
## OPTION 2 (advanced R): Change the names using R
# What are the names in the cases files ?
dput(cases_df$Province[!(cases_df$Province %in% province_pop_sf$ADM1_EN)])
# correct the names
$Province[!(cases_df$Province %in% province_pop_sf$ADM1_EN)] <- c("Ratanak Kiri",
cases_df"Banteay Meanchey",
"Siemreap",
"Mondul Kiri",
"Preah Sihanouk",
"Tboung Khmum")
# Be careful of the order of the provinces names !!!
# Merge again
<- merge(province_pop_sf, cases_df, by.x = 'ADM1_EN', by.y = "Province")
province_pop_cases_sf class(province_pop_cases_sf) # Perfect !
dim(province_pop_cases_sf) # One province is still missing !
# Which one ?
$ADM1_EN[!(province_pop_sf$ADM1_EN %in% cases_df$Province)]
province_pop_sf# This province does not exists in our cases data.
# We need to keep it as a NA value in the merged file
# Merge again
<- merge(province_pop_sf, cases_df,
province_pop_cases_sf by.x = 'ADM1_EN', by.y = "Province",
all.x = TRUE )
# We can use all.x = TRUE to keep all rows from x object event if it is not in the y object
class(province_pop_cases_sf) # Perfect !
dim(province_pop_cases_sf) # YEAH !!!! Wonderful !!!
# what happen to Pailin ? Let extract the row to look at it :
$ADM1_EN == "Pailin",]
province_pop_cases_sf[province_pop_cases_sf# NA values have been set for the missing data
Map the count data
Steps
Map the number of cases from 2018 to 2022 (
mf_map()
) in the same figure (you can split your plotting windows withpar(mfrow=c(number_of_line, number_of_columns))
) and save it (png()
),Add NA values on the map with
mf_map(type = "symb")
.
Solution
#=======================================================
# 4. Map the count data per year
#=======================================================
### 4.1 Create a maps to represent the number of cases
#---------------------------------------------------
# Save the map in img/ directory
png(filename = "img/Count_R_infections.png",
width = 700, res = 100) # Run the code until "dev.off()" function
par(mfrow = c(2,2)) # create subplots
# Count from 2018
mf_map(province_pop_cases_sf)
mf_map(province_pop_cases_sf,
var = "X2018" ,
inches = .2,
type = "prop",
col = "#000066",
leg_title = "Cases")
mf_layout(title = "Number of cases per province (2018)")
# Count from 2019
mf_map(province_pop_cases_sf)
mf_map(province_pop_cases_sf,
var = "X2019" ,
inches = .2,
type = "prop",
col = "#000066",
leg_title = "Cases")
mf_layout(title = "Number of cases per province (2019)")
# Count from 2020
mf_map(province_pop_cases_sf)
mf_map(province_pop_cases_sf,
var = "X2020" ,
inches = .2,
type = "prop",
col = "#000066",
leg_title = "Cases")
mf_layout(title = "Number of cases per province (2020)")
# Count from 2021
mf_map(province_pop_cases_sf)
mf_map(province_pop_cases_sf,
var = "X2021" ,
inches = .2,
type = "prop",
col = "#000066",
leg_title = "Cases")
mf_layout(title = "Number of cases per province (2021)")
dev.off()
# What about Pailin ? It is treated as a zero instead of NA value and this is a big issue !
# How can we add it as a NA values ?
# 4.2 Deals with NA values
#-----------------------------------------------
# There is no easy solution design for it yet ...
# What do you think ? We can try to do it by hand. Here is just a suggestions :
# Baseline map
mf_map(province_pop_cases_sf)
# Add symbol for Pailin
<- province_pop_cases_sf[province_pop_cases_sf$ADM1_EN == "Pailin",] # select row
pailin_sf $data_avail <- "No data" # Define no data value for pailin
pailin_sfmf_map(pailin_sf ,
var = "data_avail",
leg_title = NULL,
col = "black",
cex = 1.5,
pch = 22,
type = "symb")
# Count from 2021
mf_map(province_pop_cases_sf,
var = "X2021" ,
inches = .2,
type = "prop",
col = "#000066",
leg_title = "Cases")
mf_layout(title = "Number of cases per province (2021)")
# How can I improve this map ? change colors ? fix the scale for all subplot ?
# These count are not really informative since it depends on the population.
# We can compute incidence instead of cases in a new column
Transform data to incidence
Steps
Compute incidence for each column (number of cases/ population * 100,000)
Compute incidence using
apply()
and by creating afunction(){}
,Merge incidences with shapefile,
Map incidence for each year in the same figure (
par(mfrow)
) using a loopfor(variable in vector){}
, you can call help with?for (variable in vector) {}
Solution
#=======================================================
### 5. Transform to incidence
#=======================================================
### 5.1 Compute incidence
#---------------------------------------------------------
# OPTION 1 : compute columns by columns
$incidence_2017 <- province_pop_cases_sf$X2017/province_pop_cases_sf$pop * 100000
province_pop_cases_sf$incidence_2018 <- province_pop_cases_sf$X2018/province_pop_cases_sf$pop * 100000
province_pop_cases_sf$incidence_2019 <- province_pop_cases_sf$X2019/province_pop_cases_sf$pop * 100000
province_pop_cases_sf$incidence_2018 <- province_pop_cases_sf$X2020/province_pop_cases_sf$pop * 100000
province_pop_cases_sf
# OPTION 2 : use "apply" function (?apply)
# remove geometry to work on dataframe
<- st_drop_geometry(province_pop_cases_sf)
attribute_df # set rownames
row.names(attribute_df) <- attribute_df$ADM1_PCODE # easier to use the ID
# remove useless column
colnames(attribute_df) # show the column names of our dataframe
<- attribute_df[, 15:20] # select columns 15 to 20 (contains cases values)
attribute_cases_df
# Create your own function to compute incidence
<- function(cases, population){
compute_incidence # cases and population are parameters of the function
# case is a numerical values of a number of cases (can be a single value or a vector)
# population is a numerical values of population count (can be a single value or a vector)
# Both parameters must have the same length
<- cases/population * 100000
incidence return(incidence)
}
class(compute_incidence)
print(compute_incidence)
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# example:
<- province_pop_cases_sf$X2017/province_pop_cases_sf$pop * 100000
incidence_by_hand <- compute_incidence(cases = province_pop_cases_sf$X2017, population = province_pop_cases_sf$pop)
incidence_with_function == incidence_with_function # It gives the same results
incidence_by_hand #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Apply the same function to each column of the dataframe
<- apply(attribute_cases_df, 2, compute_incidence, population = attribute_df$pop)
attribute_inc_mat class(attribute_inc_mat) # apply return a matrix (table of numerical values only)
# turn it to dataframe to add a column of character
<- as.data.frame(attribute_inc_mat)
attribute_inc_df class(attribute_inc_df)
# Change the columns names to inform on data
colnames(attribute_inc_df) <- paste0('incidence_', colnames(attribute_inc_df))
# Retrieve ID code from row names
$ADM1_PCODE <- row.names(attribute_inc_df)
attribute_inc_df
# Have look at the final object
head(attribute_inc_df)
summary(attribute_inc_df)
dim(attribute_inc_df)
# Might look longer to use this option but reminds that when you have larger dataframe
# in hand this option is way more convenient than dealing with each line with the
# risque of producing mistakes
### 5.2 Merge with the spatial object
#---------------------------------------------------------
<- merge(x = province_pop_sf, y = attribute_inc_df, by = "ADM1_PCODE")
province_pop_inc_sf
class(province_pop_inc_sf)
head(province_pop_inc_sf)
summary(province_pop_inc_sf)
dim(province_pop_inc_sf)
### 5.3 Map incidence
#---------------------------------------------------------
# OPTION 1: Just like we did earlier with the number of cases, plot by plot
# OPTION 2: Use a loop !
# A loop repeat a part of your code for many values
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Simple examples:
for(i in 1:10){
# i is a variable that will successively takes the values contains in the vector given after 'in'
print(i)
}
<- "Fixed value ouside the loop"
j for(i in c("a", "b", "d", "end")){
print(i)
print(j)
}#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
png(filename = "img/Incidence_R_infections.png",
width = 700, res = 100) # Run the code until "dev.off()" function
par(mfrow = c(2,2)) # create subplots
for(year in c("2018", "2019", "2020", "2021")) {
print(paste0("Plot incidence for the year ", year))
<- paste0("incidence_X", year) # Define names of the plotted column
name_col mf_map(province_pop_inc_sf)
mf_map(province_pop_inc_sf,
var = c( "pop", name_col) ,
inches = .2,
type = "prop_choro",
col = "#000066",
leg_title = "Incidence")
mf_layout(title = paste0(" Incidence of R infections per province (", year, ")"))
}dev.off() # Don't forget to close the plotting window
Go further in analysis (Advanced R) …
The section gives suggestions to go further into the data description.
Work with averaged incidence
We are now interested in the averaged incidence between 2017 and 2022. In other terms, we want to compute the mean values of incidence for each row.
Let look at hospital distribution
Do I have higher incidence if there is more hospital in the province ? In other term, is there a bias in case detected cause by the access to health care ?