maptoolsパッケージに含まれる米国ノースカロライナ州の乳幼児突然死症候群(SIDS)のデータを用いて分析を行う。
必要なライブラリーとデータの読み込み。
In [2]:
library(sp)
library(maptools)
library(spdep)
pj <- CRS("+proj=longlat +ellps=clrk66")
f <- system.file("shapes/sids.shp", package = "maptools")
nc <- readShapePoly(f, IDvar ="FIPSNO", proj4string = pj)
データはSpatialPolygonsDataFrameクラスで与えられている。
In [3]:
plot(nc)
coordinates()でポリゴンの幾何中心を求めてプロットする。
asp = 1は縦横比を1に揃える引数である。
xlab = "Longitude", ylab = "Latitude"はそれぞれx軸、y軸のラベルを意味している。
In [4]:
plot(coordinates(nc), asp = 1, xlab = "Longitude", ylab = "Latitude")
空間的自己相関を検定する。
a 大域的自己相関
・MoranのI統計量
・Gearyのc統計量
b 局所的自己相関
・ローカルMoran
In [5]:
nc.nb <- poly2nb(nc)
In [6]:
nc.nb
Out[6]:
In [7]:
nb2listw(nc.nb, style = "B")
Out[7]:
In [8]:
nc.c <- coordinates(nc)
nc.tri.nb <- tri2nb(nc.c)
In [9]:
nc.knn.nb <- knn2nb(knearneigh(nc.c, k = 3))
In [57]:
library(spdep)
s <- nc@data$SID74/nc@data$BIR74 * 1e+05
moran.test(s, nb2listw(nc.knn.nb, style = "B"))
Out[57]:
In [61]:
library(spdep)
s <- nc@data$SID74/nc@data$BIR74 * 1e+05
moran.test(s, nb2listw(nc.knn.nb, style = "W"))
Out[61]:
In [59]:
geary.test(s, nb2listw(nc.nb, style = "B"))
Out[59]:
以上は対象地域全体に空間クラスターがあるかないかを調べたものであり、局所的に見たものではない。
局所的に空間自己相関を検討する検定をローカル検定という。
In [81]:
Imo <- localmoran(s, nb2listw(nc.nb, style = "B"))
head(Imo)
Out[81]:
In [82]:
Imo
Out[82]:
In [83]:
sum(Imo)/980
Out[83]:
In [84]:
moran.plot(s, nb2listw(nc.nb, style = "B"))
In [85]:
Imo <- localmoran(s, nb2listw(nc.nb, style = "W"))
head(Imo)
Out[85]:
In [86]:
Imo
Out[86]:
In [87]:
sum(Imo)/length(Imo)
Out[87]:
In [88]:
moran.plot(s, nb2listw(nc.nb, style = "B"))
Starting from a binary neighbours list, in which regions are either listed as neighbours or are absent (thus not in the set of neighbours for some definition), the function adds a weights list with values given by the coding scheme style chosen. B is the basic binary coding, W is row standardised (sums over all links to n), C is globally standardised (sums over all links to n), U is equal to C divided by the number of neighbours (sums over all links to unity), while S is the variance-stabilizing coding scheme proposed by Tiefelsdorf et al. 1999, p. 167-168 (sums over all links to n).
In [30]:
help(nb2listw)
Out[30]:
In [33]:
help(knn2nb)
Out[33]:
In [36]:
nc.knn.nb(1)
In [ ]: