<jupyter_start><jupyter_text>1. ESDA with rgeoda and spPrevious “introduction” page shows you:1. how to install rgeoda,2. how to load a spatial dataset and create a rgeoda instance3. how to access attribute data in rgeoowda instance4. how to create a Queen contiguity weights5. how to compute local indicators of spatial autocorrelation (LISA) using the Queen contiguity weights6. how to apply a spatially constrained clustering algorithm (SKATER) to find clustersFor people who using R to do spatial data analysis, a common routine is to use `rgdal` package to read spatial dataset, and then use `sp` package, or (in recent) `sf` package, to handle and analyze spatial data and plot the results on maps.rgeoda provides utility functions to create an `GeoDa` instance from an `sp` object or `sf` object, so that users can easily integrate the functions of spatial data analysis in rgeoda into their existing work.In this note, I will demonstrate how to work with `sp` and `rgeoda` package to do exploratory spatial data analysis (ESDA). Start from sp packageThe sp package has been an essential tool which provides spatial data-structures and many utility functions to do spatial analysis in R. It has been a core dependent library for many other packages, e.g. rgdal (IO), maptools (mapping), spdep (spatial weights, spatial statistics, and spatial models) etc.Using rgdal to read a ESRI Shapefile will return a sp (Spatial object) object, which could be either a SpatialPointsDataFrame (using an AttributeList for its data slot directly), a SpatialLinesDataFrame, or a SpatialPolygonsDataFrame.<jupyter_code>guerry_path <- system.file("extdata", "Guerry.shp", package = "rgeoda")
library(rgdal)<jupyter_output>Warning message:
“package ‘rgdal’ was built under R version 3.5.2”Loading required package: sp
rgdal: version: 1.4-4, (SVN revision 833)
 Geospatial Data Abstraction Library extensions to R successfully loaded
 Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
 Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rgdal/gdal
 GDAL binary built with GEOS: FALSE 
 Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
 Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/rgdal/proj
 Linking to sp version: 1.3-1<jupyter_text>call `readOGR()` function to read Guerry.shp file and this function will returns an `sp` object<jupyter_code>guerry_sp <- readOGR(guerry_path)<jupyter_output>OGR data source with driver: ESRI Shapefile 
Source: "/Library/Frameworks/R.framework/Versions/3.5/Resources/library/rgeoda/extdata/Guerry.shp", layer: "Guerry"
with 85 features
It has 26 fields
Integer64 fields read as strings:  dept Crm_prs Crm_prp Litercy Donatns Infants Suicids MainCty Wealth Commerc Clergy Crm_prn Infntcd Dntn_cl Lottery Desertn Instrct Prsttts Area<jupyter_text>You can simply call plot() function to plot this map:<jupyter_code>plot(guerry_sp)<jupyter_output><empty_output><jupyter_text>Create rgeoda object from spEverything above should be very familiar with you, right? Then, the fun part of spatial data analysis is going to start:Create a rgeoda object from the sp object guerry_sp you just created.<jupyter_code>library(rgeoda)

guerry_gda <- sp_to_geoda(guerry_sp)<jupyter_output>Loading required package: wkb<jupyter_text>That’s it! It should be very fast: rgeoda uses `wkb`, which is a binary representation of geometries, to exchange data between sp and rgeoda in memory.  ESDA with rgeodaNow, with the rgeoda object `guerry_gda`, you can call GeoDa’s spatial analysis functions easily. For example, we want to examine the local Moran of variable “crm_prs” (Population per Crime against persons):<jupyter_code>queen_w <- guerry_gda$CreateContiguityWeights()
crm_lisa <- guerry_gda$LISA(queen_w, as.numeric(paste(guerry_sp$Crm_prs)))
crm_lisa_clusters <- crm_lisa$GetClusterIndicators()<jupyter_output><empty_output><jupyter_text>Now, with the LISA results, we can do exploratory spatial data analysis by generating a LISA cluster map:<jupyter_code>lisa_colors <- c("#eeeeee","#FF0000","#0000FF","#a7adf9", "#f4ada8", "#999999")
lisa_labels <- c("Not Sig", "High-High", "Low-Low", "Low-High","High-Low", "Neighborless")

plot(guerry_sp, 
     col=sapply(crm_lisa_clusters, function(x){return(lisa_colors[[x+1]])}), 
     border = "#333333", lwd=0.2)
title(main = "Local Moran Map")
legend('bottomleft', legend = lisa_labels, fill = lisa_colors, border = "#eeeeee")<jupyter_output><empty_output><jupyter_text>From the above code, you can see that we still use `sp` object to do plotting. The values of cluster indicators from rgeoda’s `LISA` object are used to make the LISA map.This emphasis that rgeoda is a API focused library, which only provides the core functions of spatial data analysis for easy integration.You can easily append the lisa results to original sp object by manipulating the data.frame object inside the sp object.If you check the values of the cluster indicators, you will see they are integer numbers 0 (not significant), 1 (high-high cluster), 2 (low-low cluster), 3 (low-high cluster), 4 (high-low cluster), 5 (neighborless/island), 6 (undefined), which are excatly the same with GeoDa software when you save LISA results to a table:<jupyter_code>crm_lisa_clusters<jupyter_output><empty_output><jupyter_text>We also specify what color is associated with which cluster value to replace the `lisa_colors` and `lisa_labels`, which are defined in `sf_geoda.R`:'''lisa_colors <- c("eeeeee","FF0000","0000FF","a7adf9", "f4ada8", "999999")'''We then create an array of hex color, each observation is set to a hex color according to its cluster indicator value.<jupyter_code>sapply(crm_lisa_clusters, function(x){return(lisa_colors[[x+1]])})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08394976776191296, 'fraction_numerical': 0.01875107517632892, 'mean_word_length': 5.962874251497006, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '455418', 'license': 'MIT', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1759, 'n_words': 696, 'path': '2. ESDA with rgeoda and sp.ipynb', 'repo_name': 'lixun910/rgeoda_tutorial', 'size': '5813'}