::p_load(sf, sfdep, tmap, tidyverse, plotly, tidyverse, knitr) pacman
In-class_Exercise 2
Getting Started
Installing and Loading the R Package
Four packages will be used for this in-class exercise; they are sf, sfdep, tmap, tidyverse.
The Data
Two data sets will be used in this hands-on exercise, they are: - Hunan county boundary layer. This is a geospatial data set in ESRI shapefile format.
- Hunan_2012.csv: This csv file contains selected Hunan’s local development indicators in 2012.
Importing geospatial data
= st_read(dsn = 'data/geospatial',
hunan layer = 'Hunan')
Reading layer `Hunan' from data source
`C:\weipengten\ISSS624\In-class_Ex\In-class_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
Importing Attribute tavle
= read_csv('data/aspatial/Hunan_2012.csv') hunan2012
= read_csv('data/aspatial/Hunan_GDPPC.csv') GDPPC
Performing relational join
<- left_join(hunan,hunan2012) %>%
hunan_GDPPC select(1:4,7,15)
::: callout-important In order to retain the geospatial propertiese, the left data frame must be the sf dataframe
Deriving Contiguity Spatial Weights: Queen’s method
<- hunan_GDPPC %>%
wm_q mutate(nb = st_contiguity(geometry),
wt = st_weights(nb,
style = "W"),
.before = 1)
Computing local Moran’s I
In this section, you will learn how to compute Local Moran’s I of GDPPC at county level by using local_moran() of sfdep package.
<- wm_q %>%
lisa mutate(local_moran = local_moran(
nsim =99),
GDPPC, nb, wt, .before =1) %>%
unnest(local_moran)
The output of localmoran() is a sf data.frame which returns a matrix of values whose columns are:
Ii: the local Moran’s I statistics
E.Ii: the expectation of local moran statistic under the randomisation hypothesis
Var.Ii: the variance of local moran statistic under the randomisation hypothesis
Z.Ii:the standard deviate of local moran statistic
Pr(): the p-value of local moran statistic
Creating a Time Series Cube
I
<- spacetime(GDPPC, hunan,
GDPPC_st .loc_col = "County",
.time_col = "Year")
is_spacetime_cube(GDPPC_st)
[1] TRUE
is_spacetime_cube(GDPPC)
[1] FALSE
<- 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")
Computing Gi*
<- GDPPC_nb %>%
gi_stars group_by(Year) %>%
mutate(gi_star = local_gstar_perm(
%>%
GDPPC , nb, wt)) ::unnest(gi_star) tidyr
p <- ggplot(data =cbg, aes(x = Year, y = gi_star)) + geom_line() + theme_light()
ggplotly(p)
Performing Emerging Hotspot Analysis
<- emerging_hotspot_analysis(
ehsa x = GDPPC_st,
.var ="GDPPC",
k =1,
nsim =99
)
#hunan_ehsa <- left_join(hunan,ehsa)
ehsa_sig <- hunan_ehsa %>% filter(p_value < 0.05) tmap_mode(“plot”) tm_shape(human_ehsa) + tm_polygons() + tm_borders(alpha =0.5) + tm_shape(ehsa_sig) + tm_fill(“classification”) + tm_borders(alpha =0.4)