date()
## [1] "Wed Feb 12 15:52:27 2014"
Use the Columbus, OH crime data from http://myweb.fsu.edu/jelsner/data/columbus.zip.
require(maptools)
## Loading required package: maptools
## Warning: package 'maptools' was built under R version 3.0.2
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.0.2
## Checking rgeos availability: TRUE
require(rgdal)
## Loading required package: rgdal
## Warning: package 'rgdal' was built under R version 3.0.2
## rgdal: version: 0.8-14, (SVN revision 496)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.10.1, released 2013/08/26
## Path to GDAL shared files: C:/Users/ADVQuant12/Documents/R/win-library/3.0/rgdal/gdal
## GDAL does not use iconv for recoding strings.
## Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
## Path to PROJ.4 shared files: C:/Users/ADVQuant12/Documents/R/win-library/3.0/rgdal/proj
tmp = download.file("http://myweb.fsu.edu/jelsner/data/columbus.zip", "columbus.zip",
mode = "wb")
unzip("columbus.zip")
CC = readShapeSpatial("columbus")
brks = round(quantile(CC$CRIME, probs = seq(0, 1, 0.2)), digits = 1)
ints = findInterval(CC$CRIME, brks, all.inside = TRUE)
cls = rev(heat.colors(20))
spplot(CC, "CRIME", col.regions = cls, colorkey = list(space = "bottom"), sub = "Residential burglaries and vehicle thefts\n per 1000 households")
require(spatstat)
## Loading required package: spatstat
## Loading required package: mgcv
## This is mgcv 1.7-22. For overview type 'help("mgcv-package")'.
## Loading required package: deldir
## Warning: package 'deldir' was built under R version 3.0.2
## deldir 0.1-1
##
## spatstat 1.33-0 (nickname: 'Titanic Deckchair')
## For an introduction to spatstat, type 'beginner'
require(spdep)
## Loading required package: spdep
## Warning: package 'spdep' was built under R version 3.0.2
## Loading required package: Matrix
## Loading required package: lattice
m = length(CC$CRIME)
CC.nb = poly2nb(CC)
CC.wts = nb2listw(CC.nb)
s = Szero(CC.wts)
moran(CC$CRIME, CC.wts, n = m, S0 = s)
## $I
## [1] 0.5002
##
## $K
## [1] 2.226
require(ggplot2)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.0.2
crime = CC$CRIME
wcrime = lag.listw(CC.wts, crime)
df = data.frame(crime = crime, wcrime = wcrime)
ggplot(df, aes(x = crime, y = wcrime)) + geom_point() + geom_smooth(method = lm) +
xlab("Crime Rate") + ylab("Spatial Lag of Crime Rate")
lm(wcrime ~ crime, data = df)
##
## Call:
## lm(formula = wcrime ~ crime, data = df)
##
## Coefficients:
## (Intercept) crime
## 17.5 0.5