第1講 空間的相関の検定

0 概要

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)


Checking rgeos availability: TRUE

Attaching package: ‘maptools’

The following object is masked from ‘package:sp’:

    nowrapSpatialLines

Loading required package: Matrix

0.1 空間データの扱い方

複数のファイルから構成されるシェープファイルを取り扱う。Rに取り込む方法はいくつかあるが、ここでは主にmaptoolsパッケージのreadShapeSpatial()を使う。
Rのパッケージにはサンプルデータが付属している場合が多く、今回もそのサンプルデータを使う。
しかし、自分の研究の際には自分で作らなければならず、その際にはGISソフトが役に立つ(特にArcGISは空間統計のコマンドも豊富であり、そもそもRに取り込む必要すらない)。

0.2 投影法と座標系

地図には投影法と座標系が指定されている。これらを正しく設定しないと正確な結果が得られない。
詳細は各自で学んでもらいたい(社会基盤学科の空間情報学Ⅰを履修するとよい)。
例えば、緯度経度で表現されている地図とメートルで定義されている地図を混同したり、日本なのにアメリカの座標系を指定したりする間違いは致命的である。

0.3 座標系の変換  

CRSクラスで座標系を指定する。
例えば、

pj <- CRS("+proj=longlat +ellps=clrk66")

の場合、投影法を投影法を地球座標系に、準拠楕円体をClark1866にしている  

1 データの準備と空間データ

データは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")


2 面域データの自己相関分析

2.1 空間重み行列

2.2 空間自己相関指標

空間的自己相関を検定する。
a 大域的自己相関
・MoranのI統計量
・Gearyのc統計量
b 局所的自己相関
・ローカルMoran


In [5]:
nc.nb <- poly2nb(nc)

In [6]:
nc.nb


Out[6]:
Neighbour list object:
Number of regions: 100 
Number of nonzero links: 490 
Percentage nonzero weights: 4.9 
Average number of links: 4.9 

In [7]:
nb2listw(nc.nb, style = "B")


Out[7]:
Characteristics of weights list object:
Neighbour list object:
Number of regions: 100 
Number of nonzero links: 490 
Percentage nonzero weights: 4.9 
Average number of links: 4.9 

Weights style: B 
Weights constants summary:
    n    nn  S0  S1    S2
B 100 10000 490 980 10696

In [8]:
nc.c <- coordinates(nc)
nc.tri.nb <- tri2nb(nc.c)


     PLEASE NOTE:  The components "delsgs" and "summary" of the
 object returned by deldir() are now DATA FRAMES rather than
 matrices (as they were prior to release 0.0-18).
 See help("deldir").
 
     PLEASE NOTE: The process that deldir() uses for determining
 duplicated points has changed from that used in version
 0.0-9 of this package (and previously). See help("deldir").



In [9]:
nc.knn.nb <- knn2nb(knearneigh(nc.c, k = 3))

2.3 大域的空間自己相関

MoranのI統計量

$$ I = \frac{n}{\sum^n_{i=1}\sum^n_{j=1}\omega_{ij}}\frac{\sum^n_{i=1}\sum^n_{j=1}(x_i-\bar{x})}{\sum^n_{i=1}(x_i-\bar{x})^2} $$


・正の相関がある場合は正の値を、負の相関がある場合は負の値をとる。
・相関がない場合は$\frac{-1}{(n-1}$に近づく。


In [57]:
library(spdep)
s <- nc@data$SID74/nc@data$BIR74 * 1e+05
moran.test(s, nb2listw(nc.knn.nb, style = "B"))


Out[57]:
	Moran I test under randomisation

data:  s  
weights: nb2listw(nc.knn.nb, style = "B")  

Moran I statistic standard deviate = 3.0285, p-value = 0.001229
alternative hypothesis: greater
sample estimates:
Moran I statistic       Expectation          Variance 
      0.217763140      -0.010101010       0.005660983 

In [61]:
library(spdep)
s <- nc@data$SID74/nc@data$BIR74 * 1e+05
moran.test(s, nb2listw(nc.knn.nb, style = "W"))


Out[61]:
	Moran I test under randomisation

data:  s  
weights: nb2listw(nc.knn.nb, style = "W")  

Moran I statistic standard deviate = 3.0285, p-value = 0.001229
alternative hypothesis: greater
sample estimates:
Moran I statistic       Expectation          Variance 
      0.217763140      -0.010101010       0.005660983 

Gearyのc統計量

$$ c = \frac{(n-1)\sum_{i}\sum_{j}\omega_{ij}(z_i-z_j)^2}{2(\sum + i\sum_{j})\sum_{k}(z_i-z_j)^2} $$


・小さい値のc統計量は高い空間自己相関が存在することを意味する。


In [59]:
geary.test(s, nb2listw(nc.nb, style = "B"))


Out[59]:
	Geary C test under randomisation

data:  s 
weights: nb2listw(nc.nb, style = "B") 

Geary C statistic standard deviate = 3.0989, p-value = 0.0009711
alternative hypothesis: Expectation greater than statistic
sample estimates:
Geary C statistic       Expectation          Variance 
       0.67796679        1.00000000        0.01079878 

2.4 局所的空間自己相関

以上は対象地域全体に空間クラスターがあるかないかを調べたものであり、局所的に見たものではない。
局所的に空間自己相関を検討する検定をローカル検定という。


In [81]:
Imo <- localmoran(s, nb2listw(nc.nb, style = "B"))
head(Imo)


Out[81]:
IiE.IiVar.IiZ.IiPr(z > 0)
37001-0.38296680-0.06060606 5.66778683-0.13540522 0.55385425
37003 2.84892433-0.04040404 3.77639636 1.48681905 0.06853130
37005 1.98692859-0.03030303 2.83039504 1.19903605 0.11525696
37007-3.68378017-0.04040404 3.77639636-1.87484437 0.96959293
37009 1.89322430-0.03030303 2.83039504 1.14333852 0.12644903
37011 3.20602844-0.05050505 4.72219363 1.49859284 0.06698965

In [82]:
Imo


Out[82]:
IiE.IiVar.IiZ.IiPr(z > 0)
37001-0.382966801848665 -0.06060606060606065.66778682869206 -0.1354052152546 0.553854249533791
370032.84892432860899 -0.04040404040404043.7763963621534 1.48681904844877 0.0685313045740228
370051.98692858785486 -0.03030303030303032.83039503766888 1.19903604695948 0.115256964735824
37007-3.68378017211173 -0.04040404040404043.7763963621534 -1.87484437261694 0.969592931644662
370091.89322429735843 -0.03030303030303032.83039503766888 1.14333851522905 0.126449034831958
370113.20602843693518 -0.05050505050505054.72219362582779 1.4985928394519 0.0669896457383451
370130.198471883719379 -0.06060606060606065.66778682869206 0.108823750323568 0.456671142827968
3701512.4345610474011 -0.05050505050505054.72219362582779 5.74538254452669 4.5856700971329e-09
370174.61162512818584 -0.05050505050505054.72219362582779 2.14542086833277 0.0159596060348958
370190.37984070941892 -0.03030303030303032.83039503766888 0.243788132178134 0.403697456110082
37021-0.111832428987364 -0.07070707070707076.61317597074621 -0.01599206900167190.506379640546492
37023-0.120651989180639 -0.07070707070707076.61317597074621 -0.01942165651344410.507747632866774
370251.83770097827275 -0.05050505050505054.72219362582779 0.868915380434343 0.192446708173889
370271.24711083321748 -0.06060606060606065.66778682869206 0.549296687982829 0.291400929958938
370291.95055307121547 -0.03030303030303032.83039503766888 1.17741455576095 0.119515041475525
370310.00426863812985899-0.03030303030303032.83039503766888 0.0205492895716257 0.491802596485834
37033-0.0938573160503155-0.04040404040404043.7763963621534 -0.02750651303092020.510972127416632
370351.23878151768802 -0.06060606060606065.66778682869206 0.5457980213715 0.29260239176193
370370.656606707725882 -0.08080808080808087.55836105199024 0.268224241657371 0.394263358148998
370390.245848576304655 -0.03030303030303032.83039503766888 0.164143635152199 0.43480903587717
370410.590041331082962 -0.02020202020202021.88418965237425 0.444570610555476 0.328315045121399
370431.78944430688842 -0.02020202020202021.88418965237425 1.31835204894902 0.0936929123755267
370450.00340994092459055-0.05050505050505054.72219362582779 0.0248106216033663 0.490103009426499
370475.74234576484268 -0.04040404040404043.7763963621534 2.9757443477396 0.00146139202001315
370490.104472416254327 -0.06060606060606065.66778682869206 0.0693399779607779 0.472359502244147
37051-0.412944372698801 -0.06060606060606065.66778682869206 -0.147997069393308 0.558827459470306
370530.128700236270941 -0.02020202020202021.88418965237425 0.108477326191069 0.456808532723004
370551.771844899589 -0.02020202020202021.88418965237425 1.30553064051902 0.0958561103443767
370571.08895055294237 -0.07070707070707076.61317597074621 0.450946216913649 0.326014156246392
370592.74481438098047 -0.05050505050505054.72219362582779 1.28635117684532 0.0991602646840807
371412.29632563531932 -0.07070707070707076.61317597074621 0.920447916976011 0.178669369159058
37143-0.0248995420768238-0.03030303030303032.83039503766888 0.003211816186214280.49871867292942
37145-0.125238303980873 -0.04040404040404043.7763963621534 -0.04365485086426640.517410235664311
371471.39865676934264 -0.07070707070707076.61317597074621 0.571379044493247 0.283871365435947
37149-0.128728274870228 -0.02020202020202021.88418965237425 -0.07906285778869990.531508686905448
371510.218817364187366 -0.06060606060606065.66778682869206 0.117369717030328 0.453283540245078
37153-2.86899772671113 -0.05050505050505054.72219362582779 -1.29701504956121 0.902686995663892
371556.79411370849719 -0.0505050505050505 4.72219362582779 3.14975930712186 0.000817025125947567
37157-1.6631103856305 -0.05050505050505054.72219362582779 -0.742089346663067 0.770983397984292
371592.15493421581272 -0.06060606060606065.66778682869206 0.9306211013082 0.17602479763881
37161-0.989077670897321 -0.06060606060606065.66778682869206 -0.389997546738044 0.651730819496687
37163-0.798447357472305 -0.07070707070707076.61317597074621 -0.282990188241664 0.611407820818274
371652.31206265549338 -0.04040404040404043.7763963621534 1.21055548127138 0.113032907534924
371670.110164462812706 -0.06060606060606065.66778682869206 0.0717308794908638 0.471408040586209
371690.438058320585319 -0.04040404040404043.7763963621534 0.24621187398224 0.40275911216497
371711.30563544794619 -0.05050505050505054.72219362582779 0.624069258955243 0.266291050741691
37173-5.36881128007866 -0.04040404040404043.7763963621534 -2.74194427563342 0.996946164891859
37175-0.489801606947026 -0.04040404040404043.7763963621534 -0.231255425803636 0.59144181341102
37177-0.800755330858467 -0.02020202020202021.88418965237425 -0.568643740499487 0.715201032694654
37179-2.61713455621286 -0.04040404040404043.7763963621534 -1.3259593664435 0.907573421514651
37181-0.156401655211464 -0.03030303030303032.83039503766888 -0.07495262089700220.529873795577879
371830.842503093071965 -0.07070707070707076.61317597074621 0.355112285044346 0.361252736771228
371855.4055275872634 -0.05050505050505054.72219362582779 2.51075920892882 0.00602359201329821
37187-2.21299959273126 -0.05050505050505054.72219362582779 -0.995137574611991 0.840165321006992
371892.2797768933169 -0.04040404040404043.7763963621534 1.19394155579565 0.116250415277219
371910.683671333556939 -0.06060606060606065.66778682869206 0.312628145652291 0.377281596049444
371933.06636474511197 -0.08080808080808087.55836105199024 1.14473978244364 0.126158483296112
371951.5142865493752 -0.06060606060606065.66778682869206 0.661521846721723 0.254138854481343
371972.4374674728929 -0.05050505050505054.72219362582779 1.14491615783996 0.126121945012251
371991.50334887768882 -0.04040404040404043.7763963621534 0.794399580655095 0.213481424119051

In [83]:
sum(Imo)/980


Out[83]:
0.66510506345095

In [84]:
moran.plot(s, nb2listw(nc.nb, style = "B"))



In [85]:
Imo <- localmoran(s, nb2listw(nc.nb, style = "W"))
head(Imo)


Out[85]:
IiE.IiVar.IiZ.IiPr(z > 0)
37001-0.06382780-0.01010101 0.14885059-0.13925654 0.55537629
37003 0.71223108-0.01010101 0.22774355 1.51360957 0.06506245
37005 0.66230953-0.01010101 0.30663651 1.21429044 0.11231842
37007-0.92094504-0.01010101 0.22774355-1.90862660 0.97184486
37009 0.63107477-0.01010101 0.30663651 1.15788432 0.12345562
37011 0.64120569-0.01010101 0.18040778 1.53340868 0.06258760

In [86]:
Imo


Out[86]:
IiE.IiVar.IiZ.IiPr(z > 0)
37001-0.0638278003081108-0.01010101010101010.148850591836207 -0.139256543870286 0.555376285909253
370030.712231082152249 -0.01010101010101010.227743553279536 1.51360956764699 0.0650624465351529
370050.662309529284954 -0.01010101010101010.306636514722864 1.21429044304786 0.112318416904353
37007-0.920945043027932 -0.01010101010101010.227743553279536 -1.90862659662778 0.971844860798221
370090.631074765786144 -0.01010101010101010.306636514722864 1.15788431526452 0.12345562454285
370110.641205687387037 -0.01010101010101010.180407776413539 1.53340867987309 0.0625875957173564
370130.0330786472865632 -0.01010101010101010.148850591836207 0.11191902270949 0.45544380671168
370152.48691220948021 -0.0101010101010101 0.180407776413539 5.87886130978097 2.06549148031222e-09
370170.922325025637168 -0.01010101010101010.180407776413539 2.19526404696125 0.0140723311693836
370190.126613569806307 -0.01010101010101010.306636514722864 0.246889657557058 0.402496810342584
37021-0.0159760612839092-0.01010101010101010.126309745709542 -0.01653077535541770.506594524871901
37023-0.017235998454377 -0.01010101010101010.126309745709542 -0.02007589141969450.508008583935282
370250.367540195654549 -0.01010101010101010.180407776413539 0.889102330770891 0.186974043204829
370270.207851805536246 -0.01010101010101010.148850591836207 0.56492032588298 0.286063978906562
370290.650184357071824 -0.01010101010101010.306636514722864 1.19239387855891 0.116553419710725
370310.00142287937661966-0.01010101010101010.306636514722864 0.0208107220808944 0.491698322304182
37033-0.0234643290125789-0.01010101010101010.227743553279536 -0.02800214413424770.511169779476584
370350.206463586281337 -0.01010101010101010.148850591836207 0.561322146746881 0.287288973287488
370370.0820758384657352 -0.01010101010101010.109404111114543 0.2786795079085 0.390245395028698
370390.081949525434885 -0.01010101010101010.306636514722864 0.16623190600306 0.433987225693146
370410.295020665541481 -0.01010101010101010.464422437609521 0.447730277125304 0.327173933914552
370430.894722153444208 -0.01010101010101010.464422437609521 1.32772188311579 0.0921350007809871
370450.000681988184918111-0.0101010101010101 0.180407776413539 0.0253870307651833 0.489873127862695
370471.43558644121067 -0.01010101010101010.227743553279536 3.02936333799958 0.00122534875964479
370490.0174120693757211 -0.01010101010101010.148850591836207 0.0713122139697769 0.47157463731856
37051-0.0688240621164668-0.01010101010101010.148850591836207 -0.152206547937545 0.560487985184326
370530.0643501181354704 -0.01010101010101010.464422437609521 0.109248299739505 0.456502775942439
370550.8859224497945 -0.01010101010101010.464422437609521 1.31480934995862 0.0942869921575541
370570.155564364706053 -0.01010101010101010.126309745709542 0.466136721170704 0.32055882005664
370590.548962876196093 -0.01010101010101010.180407776413539 1.3162361436753 0.0940473979885052
371410.328046519331332 -0.01010101010101010.126309745709542 0.951453982614873 0.17068698399112
37143-0.00829984735894127-0.0101010101010101 0.306636514722864 0.00325267760684613 0.498702371666247
37145-0.0313095759952183-0.01010101010101010.227743553279536 -0.04444145372720050.517723740511295
371470.199808109906091 -0.01010101010101010.126309745709542 0.590626430283886 0.277385375867781
37149-0.064364137435114 -0.01010101010101010.464422437609521 -0.07962477587941180.531732155262824
371510.0364695606978943 -0.01010101010101010.148850591836207 0.120708062225998 0.451961136480614
37153-0.573799545342226 -0.01010101010101010.180407776413539 -1.32714776326477 0.907770095960001
371551.35882274169944 -0.0101010101010101 0.180407776413539 3.22293563261533 0.000634420314329447
37157-0.332622077126099 -0.01010101010101010.180407776413539 -0.759329829595801 0.776172361119566
371590.359155702635453 -0.01010101010101010.148850591836207 0.957090744084444 0.169260727505043
37161-0.164846278482887 -0.01010101010101010.148850591836207 -0.401090241424695 0.655823157358853
37163-0.114063908210329 -0.01010101010101010.126309745709542 -0.292522951790743 0.615056593034638
371650.578015663873346 -0.01010101010101010.227743553279536 1.23236809518385 0.108905806666237
371670.0183607438021177 -0.01010101010101010.148850591836207 0.0737711198781491 0.470596253694489
371690.10951458014633 -0.01010101010101010.227743553279536 0.250648287373388 0.401043022589162
371710.261127089589239 -0.01010101010101010.180407776413539 0.638567857346721 0.261552048480432
37173-1.34220282001967 -0.01010101010101010.227743553279536 -2.79135049680973 0.997375570021386
37175-0.122450401736757 -0.01010101010101010.227743553279536 -0.235422343715502 0.593059527210813
37177-0.400377665429234 -0.01010101010101010.464422437609521 -0.572685223616765 0.716571077940092
37179-0.654283639053214 -0.01010101010101010.227743553279536 -1.34985140623128 0.911468174209112
37181-0.052133885070488 -0.01010101010101010.306636514722864 -0.07590618436152350.530253131675834
371830.120357584724566 -0.01010101010101010.126309745709542 0.367074542349925 0.356781708037874
371851.08110551745268 -0.01010101010101010.180407776413539 2.56909005747743 0.00509829773962624
37187-0.442599918546252 -0.01010101010101010.180407776413539 -1.01825696373672 0.845722072598722
371890.569944223329226 -0.01010101010101010.227743553279536 1.21545480867298 0.112096337402316
371910.11394522225949 -0.01010101010101010.148850591836207 0.321520223561961 0.373908094494419
371930.383295593138996 -0.01010101010101010.109404111114543 1.18936124968968 0.117148771263836
371950.252381091562534 -0.01010101010101010.148850591836207 0.680337503219078 0.248145391590455
371970.487493494578581 -0.01010101010101010.180407776413539 1.17151525613912 0.120695864584109
371990.375837219422204 -0.01010101010101010.227743553279536 0.80871361385154 0.209339947796737

In [87]:
sum(Imo)/length(Imo)


Out[87]:
0.271061087463771

In [88]:
moran.plot(s, nb2listw(nc.nb, style = "B"))


参考

nb2listw

Details

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]:
nb2listw {spdep}R Documentation

Spatial weights for neighbours lists

Description

The nb2listw function supplements a neighbours list with spatial weights for the chosen coding scheme. The can.be.simmed helper function checks whether a spatial weights object is similar to symmetric and can be so transformed to yield real eigenvalues or for Cholesky decomposition.

Usage

nb2listw(neighbours, glist=NULL, style="W", zero.policy=NULL)
can.be.simmed(listw)

Arguments

neighbours

an object of class nb

glist

list of general weights corresponding to neighbours

style

style can take values “W”, “B”, “C”, “U”, “minmax” and “S”

zero.policy

default NULL, use global option value; if FALSE stop with error for any empty neighbour sets, if TRUE permit the weights list to be formed with zero-length weights vectors

listw

a spatial weights object

Details

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).

If zero policy is set to TRUE, weights vectors of zero length are inserted for regions without neighbour in the neighbours list. These will in turn generate lag values of zero, equivalent to the sum of products of the zero row t(rep(0, length=length(neighbours))) %*% x, for arbitraty numerical vector x of length length(neighbours). The spatially lagged value of x for the zero-neighbour region will then be zero, which may (or may not) be a sensible choice.

If the sum of the glist vector for one or more observations is zero, a warning message is issued. The consequence for later operations will be the same as if no-neighbour observations were present and the zero.policy argument set to true.

The “minmax” style is based on Kelejian and Prucha (2010), and divides the weights by the minimum of the maximum row sums and maximum column sums of the input weights. It is similar to the C and U styles; it is also available in Stata.

Value

A listw object with the following members:

style

one of W, B, C, U, S, minmax as above

neighbours

the input neighbours list

weights

the weights for the neighbours and chosen style, with attributes set to report the type of relationships (binary or general, if general the form of the glist argument), and style as above

Author(s)

Roger Bivand Roger.Bivand@nhh.no

References

Tiefelsdorf, M., Griffith, D. A., Boots, B. 1999 A variance-stabilizing coding scheme for spatial link matrices, Environment and Planning A, 31, pp. 165–180; Kelejian, H. H., and I. R. Prucha. 2010. Specification and estimation of spatial autoregressive models with autoregressive and heteroskedastic disturbances. Journal of Econometrics, 157: pp. 53–67.

See Also

summary.nb, read.gal

Examples

example(columbus)
coords <- coordinates(columbus)
cards <- card(col.gal.nb)
col.w <- nb2listw(col.gal.nb)
plot(cards, unlist(lapply(col.w$weights, sum)),xlim=c(0,10),
ylim=c(0,10), xlab="number of links", ylab="row sums of weights")
col.b <- nb2listw(col.gal.nb, style="B")
points(cards, unlist(lapply(col.b$weights, sum)), col="red")
col.c <- nb2listw(col.gal.nb, style="C")
points(cards, unlist(lapply(col.c$weights, sum)), col="green")
col.u <- nb2listw(col.gal.nb, style="U")
points(cards, unlist(lapply(col.u$weights, sum)), col="orange")
col.s <- nb2listw(col.gal.nb, style="S")
points(cards, unlist(lapply(col.s$weights, sum)), col="blue")
legend(x=c(0, 1), y=c(7, 9), legend=c("W", "B", "C", "U", "S"),
col=c("black", "red", "green", "orange", "blue"), pch=rep(1,5))
summary(nb2listw(col.gal.nb, style="minmax"))
dlist <- nbdists(col.gal.nb, coords)
dlist <- lapply(dlist, function(x) 1/x)
col.w.d <- nb2listw(col.gal.nb, glist=dlist)
summary(unlist(col.w$weights))
summary(unlist(col.w.d$weights))
# introducing other conditions into weights - only earlier sales count
# see http://sal.uiuc.edu/pipermail/openspace/2005-October/000610.html
data(baltimore)
set.seed(211)
dates <- sample(1:500, nrow(baltimore), replace=TRUE)
nb_15nn <- knn2nb(knearneigh(cbind(baltimore$X, baltimore$Y), k=15))
glist <- vector(mode="list", length=length(nb_15nn))
for (i in seq(along=nb_15nn))
  glist[[i]] <- ifelse(dates[i] > dates[nb_15nn[[i]]], 1, 0)
listw_15nn_dates <- nb2listw(nb_15nn, glist=glist, style="B")
which(lag(listw_15nn_dates, baltimore$PRICE) == 0.0)
which(sapply(glist, sum) == 0)
ex <- which(sapply(glist, sum) == 0)[1]
dates[ex]
dates[nb_15nn[[ex]]]

[Package spdep version 0.6-4 ]

In [33]:
help(knn2nb)


Out[33]:
knn2nb {spdep}R Documentation

Neighbours list from knn object

Description

The function converts a knn object returned by knearneigh into a neighbours list of class nb with a list of integer vectors containing neighbour region number ids.

Usage

knn2nb(knn, row.names = NULL, sym = FALSE)

Arguments

knn

A knn object returned by knearneigh

row.names

character vector of region ids to be added to the neighbours list as attribute region.id, default seq(1, nrow(x))

sym

force the output neighbours list to symmetry

Value

The function returns an object of class nb with a list of integer vectors containing neighbour region number ids. See card for details of “nb” objects.

Author(s)

Roger Bivand Roger.Bivand@nhh.no

See Also

knearneigh, card

Examples

example(columbus)
coords <- coordinates(columbus)
col.knn <- knearneigh(coords, k=4)
plot(columbus, border="grey")
plot(knn2nb(col.knn), coords, add=TRUE)
title(main="K nearest neighbours, k = 4")

[Package spdep version 0.6-4 ]

In [36]:
nc.knn.nb(1)


Error in eval(expr, envir, enclos):  関数 "nc.knn.nb" を見つけることができませんでした 

In [ ]: