Skip to content

Commit 6b205e5

Browse files
committed
adding changes to LSA1
1 parent a987cee commit 6b205e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+5675
-786
lines changed

LSA1.Rmd

Lines changed: 86 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,27 @@ After completing the notebook, you should know how to carry out the following ta
5555

5656
- Assess the sensitivity of different significance cut-off values
5757

58-
- Interpret significance by means of Bonferroni bounds and the False Discovery Rate (FDR)
58+
- Interpret significance by means of Bonferroni bounds
5959

6060
#### R Packages used
6161

62-
- **sf**: To read in the shapefile and make queen contiguity weights
62+
- **spatmap**: To construct significance and cluster maps for a variety of local statistics
6363

64-
- **spdep**: To create spatial weights structure from neighbors structure
65-
66-
67-
- **tmap**: To construct significance and cluster maps with custom functions
64+
- **geodaData**: To load the data for this notebook
6865

66+
- **tmap**: To format the maps made with **spatmap**
6967

7068
#### R Commands used
7169

7270
Below follows a list of the commands used in this notebook. For further details
7371
and a comprehensive list of options, please consult the
7472
[R documentation](https://www.rdocumentation.org).
7573

76-
- **Base R**: `install.packages`, `library`, `setwd`, `summary`, `attributes`, `lapply`, `class`, `length`, `rev`, `cut`, `mean`, `sample`, `as.data.frame`, `matrix`, `unique`, `as.character`, `which`, `order`, `data.frame`, `ifelse`, `sum`, `rep`, `set.seed`
77-
78-
- **sf**: `st_read`, `st_relate`
74+
- **Base R**: `install.packages`, `library`, `setwd`, `set.seed`, `cut`, `rep`
7975

8076
- **tmap**: `tm_shape`, `tm_borders`, `tm_fill`, `tm_layout`, `tm_facets`
8177

82-
83-
78+
- **spatmap**: `moran_map`, `geary_map`, `g_map`, `gstar_map`, `joincount_map`, `significance_map`
8479

8580
## Preliminaries
8681

@@ -90,12 +85,13 @@ actually be saving any files.^[Use `setwd(directorypath)` to specify the working
9085

9186
### Installing spatmap and geodaData
9287

93-
**spatmap** and **geodaData** are not yet loaded to CRAN, so to install them, you need `remotes_install_github`. This
88+
**rgeoda**, **spatmap** and **geodaData** are not yet loaded to CRAN, so to install them, you need `remotes_install_github`. This
9489
function is from package **remotes**, so use `install.packages("remotes")` if you do not have this package. To install
9590
**geodaData** use `remotes::install_github("spatialanalysis/geodaData")`. To install **spatmap** use
96-
`remotes::install_github("morrisonge/spatmap")`.
91+
`remotes::install_github("morrisonge/spatmap")`. To install **rgeoda** use `remotes::install_github("lixun910/rgeoda")`.
9792

9893
```{r}
94+
remotes::install_github("lixun910/rgeoda")
9995
remotes::install_github("spatialanalysis/geodaData")
10096
remotes::install_github("morrisonge/spatmap")
10197
```
@@ -110,8 +106,8 @@ their dependencies.^[Use
110106

111107
```{r, message = FALSE}
112108
library(sf)
113-
library(spdep)
114109
library(tmap)
110+
library(rgeoda)
115111
library(geodaData)
116112
library(spatmap)
117113
```
@@ -120,11 +116,9 @@ library(spatmap)
120116
### spatmap
121117

122118
The main package used throughout this notebook will be **spatmap**. This package provides functions
123-
that compute and visualize a variety of local spatial statistics. The visualizations include cluster maps
124-
and their associated significance maps. The mapping functions are built off of **tmap** and can have additional
125-
layers added to them like `tm_borders` or `tm_layout`.
126-
127-
119+
that visualize a variety of local spatial statistics. This package is built off of **rgeoda** for the statistical computations and **tmap** for the mapping component. All of the visualizations are built with a similar style
120+
to GeoDa. The visualizations include cluster maps and their associated significance maps. The mapping functions
121+
are built off of **tmap** and can have additional layers added to them like `tm_borders` or `tm_layout`.
128122

129123
### geodaData
130124

@@ -190,44 +184,25 @@ and should not be interpreted in an absolute sense.
190184
### Implementation
191185

192186
With the function `moran_map` from **spatmap**, we can create a local moran cluster map. The parameters
193-
needed are a **sf** dataframe, which is **guerry** in our case, and the name of a variable from the **sf**
187+
needed are an **sf** dataframe, which is **guerry** in our case, and the name of a variable from the **sf**
194188
dataframe. It is important to note the default parameters of `moran_map`. These include `permutations = 999`,
195-
`alpha = .05`, and `weights = NULL`. We will show examples of these later, but they are important to keep in
196-
mind.
197-
189+
`alpha = .05`, and `weights = NULL`. Permutations is the number of permutations used in computing the reference distributions
190+
of the local statistic for each location. Alpha is the cutoff significance level. The weights parameter is where we specify
191+
the weights used for the computation of the local statistics. In the NULL case, 1st order queen contiguity are computed.
198192
```{r}
199193
moran_map(guerry,"Donatns")
200194
```
201195

202-
To get a significance map for the local moran cluster map, we use `significance_map`. The default
196+
To get a significance map for the local moran, we use `significance_map`. The default
203197
parameters are the same for this function as `moran_map`. Default number of permutations is 999,
204-
the alpha level is .05, and there is option for custom weights, but 1st order queen contiguity
198+
the alpha level is .05, and there is an option for custom weights, but 1st order queen contiguity
205199
are used as default. For the significance map that corresponds with the local moran cluster map,
206-
we set `type = "moran"`. To get the significance map that directly corresponds to the cluster map,
207-
we will need to set the same ranomization seed before running each function.
200+
we set `type = "moran"`.
208201
```{r}
209202
significance_map(guerry,"Donatns", type = "moran")
210203
```
211204

212205

213-
#### Random seeds
214-
215-
Both the cluster and significance mapping functions use randomization to assess significance of
216-
local statistics. The significance map shows the p-values of location, while the cluster map shows
217-
the specific cluster classifcation. To reproduce the randomization used in one map, the same random
218-
seed must be set before using the associated function. We demonstrate this below by using `set.seed`,
219-
before `moran_map`, and again before `significance_map`. We use **2020** for the random seed in both cases,
220-
this will give use the same randomization in both functions. We use `tmap_arrange` To display the maps
221-
side by side. They both show the same significant counties because the randomization seeds are the same,
222-
and the same cut-off p-value is used to assess significance in both maps. If we do not set the same randomization
223-
seeds, the resulting maps are likely to show differences in significanct locations.
224-
```{r}
225-
set.seed(2020)
226-
p1 <- moran_map(guerry,"Donatns")
227-
set.seed(2020)
228-
p2 <- significance_map(guerry,"Donatns", type = "moran")
229-
tmap_arrange(p1,p2,ncol = 2)
230-
```
231206

232207

233208
#### tmap additions
@@ -249,7 +224,7 @@ We can set the **tmap** mode to "view"" to get an interactive base map with `tma
249224
tmap_mode("view")
250225
moran_map(guerry,"Donatns") +
251226
tm_borders() +
252-
tm_layout(title = "Local Moran Cluster Map of Donatns")
227+
tm_layout(title = "Local Moran Cluster Map of Donatns",legend.outside = TRUE)
253228
```
254229

255230
We set `tmap_mode("plot")` to get normal maps for the rest of the notebook. While basemaps are a nice
@@ -260,18 +235,21 @@ tmap_mode("plot")
260235

261236
### Randomization Options
262237

263-
To obtain higher significance levels, we need to use more permutations in the calculation
238+
To obtain higher significance levels, we need to use more permutations in the computation
264239
of the the local moran for each location. For instance, a pseudo pvalue of .00001 would
265240
require 999999 permutations. To get more permutations, we set `permutations = 99999` in
266241
`moran_map`. It is important to note that the maximum number of permutations for this function is 99999.
267242
```{r}
268243
moran_map(guerry,"Donatns", permutations = 99999) +
269-
tm_borders()
244+
tm_borders() +
245+
tm_layout(title = "Local Moran Cluster Map of Donatns", legend.outside = TRUE)
270246
```
271247

272-
For the significance map the process is the same, we set `permutations = 99999`.
248+
For the significance map, the process is the same, we set `permutations = 99999`.
273249
```{r}
274-
significance_map(guerry,"Donatns", type = "moran", permutations = 99999)
250+
significance_map(guerry,"Donatns", type = "moran", permutations = 99999) +
251+
tm_borders() +
252+
tm_layout(title = "Local Moran Significance Map of Donatns", legend.outside = TRUE)
275253
```
276254

277255

@@ -288,12 +266,16 @@ To change the cut-off level of significance in the local moran cluster mapping f
288266
parameter `alpha =`. The default option is .05, but if we want another level, say .01, we set
289267
`alpha = .01`.
290268
```{r}
291-
moran_map(guerry,"Donatns", permutations = 99999, alpha = .01)
269+
moran_map(guerry,"Donatns", permutations = 99999, alpha = .01) +
270+
tm_borders() +
271+
tm_layout(title = "Local Moran Cluster Map of Donatns", legend.outside = TRUE)
292272
```
293273

294274
The process is the same in `significance_map`, we set `alpha = .01`.
295275
```{r}
296-
significance_map(guerry,"Donatns", type = "moran",permutations = 99999, alpha = .01)
276+
significance_map(guerry,"Donatns", type = "moran",permutations = 99999, alpha = .01) +
277+
tm_borders() +
278+
tm_layout(title = "Local Moran Significance Map of Donatns", legend.outside = TRUE)
297279
```
298280

299281

@@ -307,12 +289,16 @@ the cutoff p-value to be used to determine significance. We assign **bonferroni*
307289
will give us a local moran cluster map with a bonferroni significance cut-off.
308290
```{r}
309291
bonferroni <- .01 / 85
310-
moran_map(guerry,"Donatns", permutations = 99999, alpha = bonferroni)
292+
moran_map(guerry,"Donatns", permutations = 99999, alpha = bonferroni) +
293+
tm_borders() +
294+
tm_layout(title = "Local Moran Cluster Map of Donatns", legend.outside = TRUE)
311295
```
312296

313297
To make the significance map with the bonferroni bound, we set `alpha = bonferroni`.
314298
```{r}
315-
significance_map(guerry,"Donatns", type = "moran",permutations = 99999, alpha = bonferroni)
299+
significance_map(guerry,"Donatns", type = "moran",permutations = 99999, alpha = bonferroni) +
300+
tm_borders() +
301+
tm_layout(title = "Local Moran Significance Map of Donatns", legend.outside = TRUE)
316302
```
317303

318304

@@ -399,7 +385,7 @@ the squaring of the differences removes the sign.
399385
### Implementation
400386

401387
For the local geary map, we use `geary_map`. It has the same default parameters as `moran_map`.
402-
These are 999 permutations, and alpha level of .05, and queen contiguity weights. There is option
388+
These are 999 permutations, an alpha level of .05, and 1st order queen contiguity weights. There is an option
403389
to use custom weights with `weights =` parameter, but this can be left empty. The inputs are the
404390
same as `moran_map` with an **sf** dataframe: **guerry**, and a variable name: **Donatns**. We can
405391
add **tmap** layers to this mapping function too. Here we use `tm_borders` and `tm_layout`
@@ -430,19 +416,16 @@ geary_map(guerry,"Donatns",permutations = 99999) +
430416
tm_layout("Local Geary Cluster Map", legend.outside = TRUE)
431417
```
432418

433-
419+
We do the same thing to get more permutations for the significance map.
434420
```{r}
435421
significance_map(guerry,"Donatns",type = "geary",permutations = 99999) +
436422
tm_borders() +
437423
tm_layout("Local Geary Significance Map", legend.outside = TRUE)
438424
```
439425

440-
441-
442426
#### Changing the significance threshold
443427

444-
445-
428+
We can change the significance cut-off with `alpha =`, as with `moran_map` and `significance_map`
446429
```{r}
447430
geary_map(guerry,"Donatns",permutations = 99999,alpha = .01) +
448431
tm_borders() +
@@ -489,48 +472,52 @@ the Getis-Ord approach does not consider spatial outliers.
489472
Inference is based on conditional permutation, using an identical procedure as for the
490473
other statistics.
491474

492-
493-
494475
### Implementation
495476

477+
We can make a cluster map for the local G statistic with `g_map`. The formatting, parameters, and default options
478+
are the same with this function as the other mapping functions in **spatmap**. The main difference is that
479+
this function has a parameter for `type`. This parameter speficies whether the local G statistic is G or
480+
$G*$. As with the other mapping functions, we use **tmap** to format the maps.
496481
```{r}
497-
significance_map(guerry,"Donatns",type = "g")
482+
g_map(guerry, "Donatns") +
483+
tm_borders() +
484+
tm_layout(title = "Local G Cluster Map",legend.outside = TRUE)
498485
```
499486

500-
501-
487+
To make the $G*$ cluster map, we just change `type = "gstar"`.
502488
```{r}
503-
g_map(guerry, "Donatns", type = "gstar")
489+
gstar_map(guerry, "Donatns") +
490+
tm_borders() +
491+
tm_layout(title = "Local G* Cluster Map",legend.outside = TRUE)
504492
```
505493

506-
507-
494+
For the significance map, we use `significance_map` and set `type = g`. For the $G*$
495+
significance map, we set `type = "gstar"`
508496
```{r}
509-
g_map(guerry, "Donatns", type = "gstar")
497+
significance_map(guerry,"Donatns",type = "g") +
498+
tm_borders() +
499+
tm_layout(title = "Local G Significance Map",legend.outside = TRUE)
510500
```
511501

512-
513-
514-
515502
### Interpretation and significance
516503

517-
518-
504+
To change the permutations and the cut-off significance level, we use `permutation =`, and `alpha =`. The
505+
default options for these parameters are 999 for permutations and .05 for alpha, as with the other
506+
**spatmap** mapping functions. Here we change `permutations = 99999` and `alpha = .01`.
519507
```{r}
520-
significance_map(guerry,"Donatns",type = "g", permutations = 99999)
508+
g_map(guerry,"Donatns",permutations = 99999,alpha = .01) +
509+
tm_borders() +
510+
tm_layout(title = "Local G Cluster Map",legend.outside = TRUE)
521511
```
522512

523-
524-
513+
The process is the same for the corresponding significance map. Increasing the permutations gives us
514+
more detailed information about the significance at each location.
525515
```{r}
526-
g_map(guerry,"Donatns",permutations = 99999,alpha = .01)
516+
significance_map(guerry,"Donatns",type = "g", permutations = 99999,alpha = .01) +
517+
tm_borders() +
518+
tm_layout(title = "Local G Significance Map",legend.outside = TRUE)
527519
```
528520

529-
530-
531-
532-
533-
534521
## Local Join Count Statistic
535522

536523

@@ -562,43 +549,31 @@ comparisons and the sensitivity of the pseudo p-value to the actual simulation e
562549

563550
### Implementation
564551

565-
566-
567-
568-
569-
552+
Since the local join count only uses binary variables(numeric variables of 1 or 0), we must make one
553+
for **guerry**. To get the number of observations in **guerry** we use `nrow`. We create and empty vector
554+
of 0's of length **n** with `rep`. We assign 1 for the locations that have **Donatns** greater than 10996.
555+
Lastly we add the binary variable **doncat** to the **sf** dataframe.
570556
```{r}
571-
doncat <- rep(0, 85)
557+
n <- nrow(guerry)
558+
doncat <- rep(0, n)
572559
doncat[guerry$Donatns > 10996] <- 1
573560
guerry$doncat <- doncat
574561
```
575562

576-
577-
578-
579-
563+
We map these locations using **tmap** functions. We set `style = "cat"` because the variable is and only has two
564+
possible values. We use color white for 0 and color blue for 1.
580565
```{r}
581566
tm_shape(guerry) +
582567
tm_fill("doncat", style = "cat", palette = c("white", "blue")) +
583-
tm_borders()
584-
```
585-
586-
587-
588-
589-
```{r}
590-
significance_map(guerry,"doncat",type = "join_count", permutations = 99999)
568+
tm_borders() +
569+
tm_layout(legend.outside = TRUE)
591570
```
592571

593-
594-
595-
596-
572+
To make the local join count cluster map, we use `joincount_map` with **doncat** as the input variables. We change
573+
permutations to be 99999. This function has the same default options and paramters as the other mapping functions
574+
of **spatmap**.
597575
```{r}
598-
jc_map(guerry,"doncat",permutations = 99999) +
599-
tm_borders()
576+
joincount_map(guerry,"doncat",permutations = 99999) +
577+
tm_borders() +
578+
tm_layout(title = "Local G Cluster Map",legend.outside = TRUE)
600579
```
601-
602-
603-
604-

0 commit comments

Comments
 (0)