::p_load(sf, sfdep, tmap, plotly, tidyverse, Kendall) pacman
In-class_Ex2_EHSA
Overview
Here we will explore emerging hot spot analysis(EHSA) which represents a the change in hot and cold spots over time if there is any.
The steps needed can be summarised as the following: - Building a space-time cube - Calculate Getis-Ord local Gi* statistic using FDR correction - Use Mann-Kendall trend test to evaluate each hot and cold spot - categorise each region by referring to the z-score and p-value for each area and bin
Loading packages and data
Loading packages
Loading data
Geospatial data
The following uses the st_read() function from the sf package to read the geospatial data.
<- st_read(dsn = "data/geospatial",
hunan layer = "Hunan")
Reading layer `Hunan' from data source
`C:\Users\Lian Khye\Desktop\MITB\Geospatial\geartooth\ISSS624\In-class_Ex\Ex2\data\geospatial'
using driver `ESRI Shapefile'
Simple feature collection with 88 features and 7 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 108.7831 ymin: 24.6342 xmax: 114.2544 ymax: 30.12812
Geodetic CRS: WGS 84
Import attribute table
Here we will import the Hunan_GDPPC csv file.
<- read_csv("data/aspatial/Hunan_GDPPC.csv") GDPPC
Creating a time series cube
Here we will be creating the time-series cube using the spacetime() function of the sfdep package.
<- spacetime(GDPPC, hunan,
GDPPC_st .loc_col = "County",
.time_col = "Year")
Computing Gi*
Derive Spatial Weights
Here we will use the inverse distance weights for identifying neighbours.
<- GDPPC_st %>%
GDPPC_nb activate("geometry") %>%
mutate(nb = include_self(st_contiguity(geometry)),
wt = st_inverse_distance(nb, geometry,
scale = 1,
alpha = 1),
.before = 1) %>%
set_nbs("nb") %>%
set_wts("wt")
We will use activate() function to activate the geometry context and the mutate() to create 2 new columns named nb and wt. We will then use set_nbs() and set_wts() to copy the nb and wt columns.
Gi* calculation
Next we will calculate the local Gi* for each county by grouping the year and using the local_gstar_perm() fucntion and use the unnest() function to unnest the gi_star column of the new gi_stars dataframe.
<- GDPPC_nb %>%
gi_stars group_by(Year) %>%
mutate(gi_star = local_gstar_perm(
%>%
GDPPC, nb, wt)) ::unnest(gi_star) tidyr
Mann-Kendall Test
Here we will be using the Gi* values for calculating the Mann-Kendall test. This test will test for changes but not the magnitude of change.
The following code uses Changsha county.
<- gi_stars %>%
cbg ungroup() %>%
filter(County == "Changsha") |>
select(County, Year, gi_star)
Next we can then plot the change using ggplot package.
ggplot(data = cbg,
aes(x = Year,
y = gi_star)) +
geom_line() +
theme_light()
Here we can make the graph more interactive.
<- ggplot(data = cbg,
p aes(x = Year,
y = gi_star)) +
geom_line() +
theme_light()
ggplotly(p)
Finally we can calculate the p-value, signify by the sl column.
%>%
cbg summarise(mk = list(
unclass(
::MannKendall(gi_star)))) %>%
Kendall::unnest_wider(mk) tidyr
# A tibble: 1 × 5
tau sl S D varS
<dbl> <dbl> <dbl> <dbl> <dbl>
1 0.485 0.00742 66 136. 589.
We can apply this to all of the locations using a group_by() function.
<- gi_stars %>%
ehsa group_by(County) %>%
summarise(mk = list(
unclass(
::MannKendall(gi_star)))) %>%
Kendall::unnest_wider(mk) tidyr
EHSA
Here we can arrange the EHSA.
<- ehsa %>%
emerging arrange(sl, abs(tau)) %>%
slice(1:5)
emerging
# A tibble: 5 × 6
County tau sl S D varS
<chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Shuangfeng 0.868 0.00000143 118 136. 589.
2 Xiangtan 0.868 0.00000143 118 136. 589.
3 Xiangxiang 0.868 0.00000143 118 136. 589.
4 Chengbu -0.824 0.00000482 -112 136. 589.
5 Dongan -0.824 0.00000482 -112 136. 589.
Emerging hotspot analysis
Finally we will perform EHSA analysis using emerging_hotspot_analysis() function. It takes a spacetime object and the variable of interest as the .var argument. The k argument refers to the time lags specified and the nsim refers to the number of simulation to be performed.
<- emerging_hotspot_analysis(
ehsa x = GDPPC_st,
.var = "GDPPC",
k = 1,
nsim = 99
)
Visualisation of EHSA classes
We can the plot the EHSA using a bar chart by using ggplot2 package.
ggplot(data = ehsa,
aes(x = classification)) +
geom_bar()
Visualisation of EHSA
We can also visualise the distribution of EHSA using a choropleth map.
<- hunan %>%
hunan_ehsa left_join(ehsa,
by = join_by(County == location))
<- hunan_ehsa %>%
ehsa_sig filter(p_value < 0.05)
tmap_mode("plot")
tm_shape(hunan_ehsa) +
tm_polygons() +
tm_borders(alpha = 0.5) +
tm_shape(ehsa_sig) +
tm_fill("classification") +
tm_borders(alpha = 0.4)