knitr::opts_chunk$set(echo = TRUE)

Preliminaries

Installing github libraries

To use spatmap, you must install it directly from github. This is best done with install_github from the remotes package. We will also need to do the for rgeoda and geodaData. rgeoda does all of the statistical computation behind spatmap visualizations. geodaData gives us the data for this vignette.

remotes::install_github("morrisonge/spatmap")
remotes::install_github("spatialanalysis/geodaData")
remotes::install_github("lixun910/rgeoda")

Loading the libraries

To load the libraries we use library. If there is a library that is not installed, use install.packages. This function will work for libraries we did not remote install from github because these packages have been uploaded to CRAN.

library(spatmap)
library(sf)
library(tmap)
library(geodaData)
library(rgeoda)
library(tidyverse)

Loading the Data

The dataset is from a classic social science study by Andre-Michel Guerry on crime, suicide , and other “moral” statistics. It is available on the GeoDa website. The spatial resolution is the prefecture-level, which is similar to the county-level. Each polygon has measures for different “moral” statistics of the area. There is some degree of temporal resolution with averages of the variables pertaining to different sets of years. These years range from 1815 to 1834. There are 23 variables in the dataset, and 85 observations. I will only be using a few of these variables for the project vignette. It can be loaded directly from geodaData.

guerry <- geodaData::guerry

Creating custom weights

In order to demonstarte the custom weights option of the mapping functions in spatmap, we will create some weights with rgeoda. The default option for weights is 1st order queen contiguity in the mapping functions. We will generate some rook contiguity weights to demonstrate the custom weight option.

First, we need to convert the sf object to geoda. We use sf_to_geoda to do this. To create rook contiguity weights, we use rook_weights on guerry_gda. This will give us weights of the rgeoda data structure.

guerry_gda <- sf_to_geoda(guerry)
rook <- rook_weights(guerry_gda, order = 2)

Mapping functions

Local Moran Map

To make a local moran map, we use moran_map. The parameters are an sf data frame and the name of the variable. In our case this is guerry, and "Donatns". This will give us a basic local moran cluster map with 999 permutations and a significance cutoff level of .05. These will be the default parameters for all of the mapping functions in spatmap.

moran_map(guerry, "Donatns") 

To make the corresponding significance map, we use significance_map. The parameters are the same as moran_map, but we have to also specify the type of local spatial statistic. In this case it is "moran".

significance_map(guerry,"Donatns",type = "moran")

Permuations and Significance

Changing the number of permuations

To change the number of permutations in a map, we use the parameter permutations =. The default is 999 for every mapping function in spatmap. Below we set it equal to 99999.

moran_map(guerry, "Donatns", permutations = 99999) 

The process for changing the number of permutations is the same for significance_map, we just set permutation = 99999.

significance_map(guerry,"Donatns",type = "moran", permutations = 99999)
Changing the Significance Cutoff

To change the significance cutoff level, we use alpha =. Here we will change it to .01.

moran_map(guerry, "Donatns", permutations = 99999, alpha = .01) 

The process is the same for significance_map, we set alpha = .01.

significance_map(guerry,"Donatns",type = "moran", permutations = 99999, alpha = .01)

tmap styling options

One of the main advantages of spatmap is that it integrates with the tmap mapping package. The mapping functions allow you to add addtional layers to the map, give a vast array of formating options. To start, we set tmap_mode("view") to get interactive maps. We add borders to the map with tm_borders. Lastly we set the title of the map with tm_layout

tmap_mode("view")
moran_map(guerry, "Donatns", permutations = 99999) +
  tm_borders() +
  tm_layout(title = "Local Moran Map of Donatns")

We set the same styling options for the significance map as well, but with a different title.

tmap_mode("plot")
significance_map(guerry,"Donatns",type = "moran", permutations = 99999) +
  tm_borders() +
  tm_layout(legend.outside = TRUE, title = "Local Moran Significance Map ")

Custom weights

All of the mapping functions have a parameter for custom weights, which is always weights =. The default option is NULL and the function computes 1st order queen contiguity weights. We can specify the custom weights to be something else, by building different weights with rgeoda. Earlier in the notebook, we constructed 2nd order rook contiguity weights. We use these weights in the moran_map with weights = parameter.

moran_map(guerry, "Donatns", permutations = 99999, weights = rook) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Local Moran Map of Donatns")

As we can see the resulting map is very different from the default weights.

To make the corresponding significance map with these weights, we use the same parameter as with the local moran map above. We set weights = rook.

significance_map(guerry,"Donatns",type = "moran", permutations = 99999, weights = rook) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Local Moran Significance Map ")

Local Geary Map

To make the local geary cluster map, we use geary_map. This function has the same parameters and default options as moran_map with 999 permutations, a cutoff significance level of .05, and a custom weights options. We can also add the tmap formatting functions too.

geary_map(guerry,"Donatns",permutations = 99999) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Local Geary Cluster Map Donatns")

To make the corresponding significance map, we use significance_map and set type = "geary".

significance_map(guerry,"Donatns",type = "geary", permutations = 99999) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Local Geary Significance Map ")

Multivariate Geary

We can make a multivariate local geary map with multi_geary_map. The difference here is that we use a vector of variable names as opposed to one variable name as in the other mapping functions of spatmap.

multi_geary_map(guerry,c("Donatns","Infants"), permutations = 99999, alpha = .01) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Local Geary Cluster Map Donatns")

To make the significance map that corresponds to the multivariate geary map, we use significance_map, and enter a vector of variable names instead of just one. We also need to set type = "geary".

significance_map(guerry,c("Donatns", "Infants"),type = "geary", permutations = 99999, alpha = .01)  +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Multivariate Local Geary Significance Map Donatns")

Local G Map

To make a Local G Cluster Map, we use g_map. This function has the same default parameters and formatting options as the other mapping functions in spatmap.

g_map(guerry,"Donatns", permutation = 99999) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Local G Cluster Map")

To make the significance map, we use significance_map and set type = "g"

significance_map(guerry,"Donatns",type = "g", permutations = 99999) +
  tm_borders() +
   tm_layout(legend.outside = TRUE,title = "Local G Significance Map")

Local G* Map

For the Local $G*$ Cluster Map, we use gstar_map.

gstar_map(guerry,"Donatns", permutation = 99999) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Local G* Cluster Map")

For the associated significance map, we use significance_map and set type = "gstar".

significance_map(guerry,"Donatns",type = "gstar", permutations = 99999) +
  tm_borders() +
   tm_layout(legend.outside = TRUE,title = "Local G* Significance Map")

Local Join Count Map

The local join count is used primarily on binary variables, so we will create some for the univariate and multivariate maps. We will make both of the binary variables based on existing variables. To do this we will create them based off of the quintiles of Donatns and Infants.

To get the break for the top quintile of both variables, we use quantile with the variables as inputs and .8 to get the top quintile break, or 80th percentile.

doncatbreak <- quantile(guerry$Donatns, .8)
infantbreak <- quantile(guerry$Infants, .8)

Next, we create two vectors to store the binary variables by using rep and starting with all 0 values. We use conditional indexing to assign 1 to all values that are greater than the 80th percentile break that we computed above.

n <- nrow(guerry)
doncat <- rep(0,n)
infcat <- rep(0,n)
doncat[guerry$Donatns > doncatbreak] <- 1
infcat[guerry$Infants > infantbreak] <- 1
guerry$doncat <- doncat
guerry$infcat <- infcat

We make a Local Join Count Cluster Map with joincount_map and use doncat as the input variable.

joincount_map(guerry,"doncat",permutations = 99999) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,title = "Local Join Count Cluster Map doncat")

Multivariate Join Count Map

For the multivariate join count map, we use multi_joincount_map, and input both binary variables, doncat and infcat

multi_joincount_map(guerry,c("doncat","infcat"),permutations = 99999) +
  tm_borders() +
  tm_layout(legend.outside = TRUE,"Multivariate Local Join Count Cluster Map")


morrisonge/spatmap documentation built on June 9, 2020, 1:22 p.m.