Suggested citation:

Mendez C. (2020). Spatial regression analysis in R. R Studio/RPubs. Available at https://rpubs.com/quarcs-lab/tutorial-spatial-regression

This work is licensed under the Creative Commons Attribution-Non Commercial-Share Alike 4.0 International License.

Acknowledgment:

Material adapted from multiple sources, in particular BurkeyAcademy’s GIS & Spatial Econometrics Project

Import shapefiles into R

Import neighbor relationship from

`.gal`

filesCreate neighbor relationships in R from shape files

Create neighbor relationships in R from shape latitude and longitude

Understand the difference between Great Circle and Euclidean distances

Export neighbor relationships as weight matrices to plain text files

Test for spatial dependence via the Moran’s I test

Evaluate the four simplest models of spatial regression

All necessary files can be downloaded from BurkeyAcademy’s GIS & Spatial Econometrics Project

If you are a member of the QuaRCS lab, you can run this tutorial in R Studio Cloud and access the files in the following Github Repository

Let us use the `readOGR`

function from the `rgdal`

library to import the `.shp`

file

```
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/carlos/Github/QuaRCS-lab/tutorial-spatial-regression", layer: "NCVACO"
## with 234 features
## It has 49 fields
## Integer64 fields read as strings: FIPS qtystores PCI COUNTMXBV DC GA KY MD SC TN WV VA COUNTBKGR TOTALPOP POP18OV LABFORCE HHOLDS POP25OV POP16OV
```

Note that the file is imported as a `SpatialPolygonsDataFrame`

object

`.gal`

fileLet us use the `read.gal`

function from the `rgdal`

library to import the `.gal`

weights matrix created in GeoDa

```
## Neighbour list object:
## Number of regions: 234
## Number of nonzero links: 1132
## Percentage nonzero weights: 2.067
## Average number of links: 4.838
## Link number distribution:
##
## 1 2 3 4 5 6 7 8 9 11
## 16 26 22 32 36 49 37 9 6 1
## 16 least connected regions:
## 51515 51530 51595 51660 51690 51720 51820 51131 51520 51540 51580 51600 51678 51790 51840 51001 with 1 link
## 1 most connected region:
## 51041 with 11 links
```

- Is the is the neighbor relationship symmetric?

`## [1] TRUE`

- For queen contiguity

- Alternatively, you can create a Rook contiguity relationship as

```
## Neighbour list object:
## Number of regions: 234
## Number of nonzero links: 1132
## Percentage nonzero weights: 2.067
## Average number of links: 4.838
## Link number distribution:
##
## 1 2 3 4 5 6 7 8 9 11
## 16 26 22 32 36 49 37 9 6 1
## 16 least connected regions:
## 51515 51530 51595 51660 51690 51720 51820 51131 51520 51540 51580 51600 51678 51790 51840 51001 with 1 link
## 1 most connected region:
## 51041 with 11 links
```

- Are the relationships symmetric?

`## [1] TRUE`

- Import table

- Identify coordinates

Recognize that latitude and longitude are handled using **great circle distances**

Fail to recognize that latitude and longitude are handled using great circle distances. Latitude and longitude should not be used to compute **Euclidean distances**

Plot the right neighbor relationship