【R】rgeoda
2021年9月9日
1. はじめに
rgeoda
は、libgeoda C++ libraryのWrapperでSpatial Data を扱うパッケージです。
2. インストール
CRANからインストールできます。
install.packages("rgeoda")
3. つかってみる
標準でインストールされるデータを使ってみます。
library(sf) library(rgeoda) guerry_path <- system.file("extdata", "Guerry.shp", package = "rgeoda") guerry <- st_read(guerry_path) guerry
> guerry
Simple feature collection with 85 features and 29 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 47680 ymin: 1703258 xmax: 1031401 ymax: 2677441
Projected CRS: NTF (Paris) / Lambert zone II
First 10 features:
CODE_DE COUNT AVE_ID_ dept Region Dprtmnt Crm_prs Crm_prp Litercy Donatns Infants Suicids MainCty Wealth Commerc
1 01 1 49 1 E Ain 28870 15890 37 5098 33120 35039 2 73 58
2 02 1 812 2 N Aisne 26226 5521 51 8901 14572 12831 2 22 10
3 03 1 1418 3 C Allier 26747 7925 13 10973 17044 114121 2 61 66
4 04 1 1603 4 E Basses-Alpes 12935 7289 46 2733 23018 14238 1 76 49
5 05 1 1802 5 E Hautes-Alpes 17488 8174 69 6962 23076 16171 1 83 65
6 07 1 2249 7 S Ardeche 9474 10263 27 3188 42117 52547 1 84 1
7 08 1 35395 8 N Ardennes 35203 8847 67 6400 16106 26198 2 33 4
8 09 1 2526 9 S Ariege 6173 9597 18 3542 22916 123625 1 72 60
9 10 1 34410 10 E Aube 19602 4086 59 3608 18642 10989 2 14 3
10 11 1 2807 11 S Aude 15647 10431 34 2582 20225 66498 2 17 35
Clergy Crm_prn Infntcd Dntn_cl Lottery Desertn Instrct Prsttts Distanc Area Pop1831 TopCrm TopLit TopWealth
1 11 71 60 69 41 55 46 13 218.372 5762 346.03 1 0 1
2 82 4 82 36 38 82 24 327 65.945 7369 513.00 1 1 0
3 68 46 42 76 66 16 85 34 161.927 7340 298.26 1 0 1
4 5 70 12 37 80 32 29 2 351.399 6925 155.90 0 1 1
5 10 22 23 64 79 35 7 1 320.280 5549 129.10 0 1 1
6 28 76 47 67 70 19 62 1 279.413 5529 340.73 0 0 1
7 50 53 85 49 31 62 9 83 105.694 5229 289.62 1 1 0
8 39 74 28 63 75 22 77 3 385.313 4890 253.12 0 0 1
9 42 77 54 9 28 86 15 207 83.244 6004 246.36 0 1 0
10 15 80 35 27 50 63 48 1 370.949 6139 270.13 0 0 0
geometry
1 MULTIPOLYGON (((801150 2092...
2 MULTIPOLYGON (((729326 2521...
3 MULTIPOLYGON (((710830 2137...
4 MULTIPOLYGON (((882701 1920...
5 MULTIPOLYGON (((886504 1922...
6 MULTIPOLYGON (((747008 1925...
7 MULTIPOLYGON (((818893 2514...
8 MULTIPOLYGON (((509103 1747...
9 MULTIPOLYGON (((775400 2345...
10 MULTIPOLYGON (((626230 1810...
Queen Contiguity Weightsを計算してみます。
w <- queen_weights(guerry) w
> w
Reference class object of class "Weight"
Field "gda_w":
An object of class "p_GeoDaWeight"
Slot "pointer":
<pointer: 0x000001e287506f30>
Field "is_symmetric":
[1] TRUE
Field "sparsity":
[1] 0.05813149
Field "min_neighbors":
[1] 2
Field "max_neighbors":
[1] 8
Field "num_obs":
[1] 85
Field "mean_neighbors":
[1] 4.941176
Field "median_neighbors":
[1] 5
Field "has_isolates":
[1] FALSE
summary(w)
> summary(w)
name value
1 number of observations: 85
2 is symmetric: TRUE
3 sparsity: 0.0581314878892734
4 # min neighbors: 2
5 # max neighbors: 8
6 # mean neighbors: 4.94117647058824
7 # median neighbors: 5
8 has isolates: FALSE
Rook Contiguity Weightsはこちら。
rook_w <- rook_weights(guerry) summary(rook_w)
> summary(rook_w)
name value
1 number of observations: 85
2 is symmetric: TRUE
3 sparsity: 0.0581314878892734
4 # min neighbors: 2
5 # max neighbors: 8
6 # mean neighbors: 4.94117647058824
7 # median neighbors: 5
8 has isolates: FALSE
K-Nearest Neighbor Weightsはこちら。
knn6_w <- knn_weights(guerry, 6) summary(knn6_w)
> summary(knn6_w)
name value
1 number of observations: 85
2 is symmetric: FALSE
3 sparsity: 0.0705882352941176
4 # min neighbors: 6
5 # max neighbors: 6
6 # mean neighbors: 6
7 # median neighbors: 6
8 has isolates: FALSE
Local Indicators of Spatial Association–LISAもできます。以下の関数が提供されています。
- Local Moran: local_moran(), local_moran_eb()
- Local Geary: local_geary(), local_multigeary()
- Local Getis-Ord statistics: local_g() and local_gstar()
- Local Join Count: local_joincount(), local_bijoincount(), local_multijoincount()
- Quantile LISA: local_quantilelisa(), local_multiquantilelisa()
- Local Neighbor Match Test: neighbor_match_test()
crm_prs = guerry["Crm_prs"] lisa <- local_moran(w, crm_prs) lisa
> lisa
Reference class object of class "LISA"
Field "gda_lisa":
An object of class "p_LISA"
Slot "pointer":
<pointer: 0x000001e287594740>
Field "p_vals":
[1] 0.197 0.013 0.023 0.068 0.111 0.045 0.281 0.048 0.183 0.004 0.002 0.052 0.106 0.002 0.266 0.267 0.017 0.401 0.091
[20] 0.123 0.219 0.281 0.206 0.035 0.371 0.301 0.119 0.001 0.011 0.189 0.463 0.024 0.014 0.272 0.021 0.423 0.067 0.426
[39] 0.061 0.458 0.125 0.145 0.433 0.145 0.197 0.011 0.187 0.130 0.017 0.197 0.002 0.210 0.092 0.080 0.471 0.077 0.032
[58] 0.331 0.061 0.037 0.154 0.170 0.309 0.036 0.158 0.205 0.059 0.183 0.003 0.048 0.342 0.100 0.358 0.443 0.290 0.075
[77] 0.034 0.026 0.060 0.005 0.310 0.122 0.064 0.494 0.126
Field "c_vals":
[1] 0 1 1 0 0 2 0 2 0 2 2 0 0 2 0 0 1 0 0 0 0 0 0 2 0 0 0 2 2 0 0 2 1 0 3 0 0 0 0 0 0 0 0 0 0 2 0 0 3 0 1 0 0 0 0 0 1 0
[59] 0 1 0 0 0 2 0 0 0 0 1 1 0 0 0 0 0 0 2 2 0 2 0 0 0 0 0
Field "lisa_vals":
[1] 0.516120231 0.818275138 0.794086560 0.733161543 0.228467334 0.829040272 0.615280448 1.627415446 -0.019525625
[10] 0.687081885 1.707896579 0.821748275 -0.213568315 0.291316827 -0.184833414 -0.047198613 0.249972417 0.054411195
[19] 0.862095943 0.835368441 0.538432168 -0.050446242 -0.681022658 0.678851849 -0.105369231 0.038567922 1.116236578
[28] 1.168138859 0.162030880 0.069037225 0.021522359 1.000255304 0.276308094 0.423159405 -0.101271972 0.015647332
[37] 0.554846307 -0.027584417 0.111200075 -0.082994402 0.281817532 -0.048962443 -0.015839144 0.830286100 -0.160743989
[46] 1.597607483 0.396712769 0.862342357 -0.466396879 0.262343383 1.424738721 -0.379247703 0.388066026 0.316231271
[55] 0.068795845 0.405538158 0.977019076 0.202610060 0.714533504 0.601447598 -0.177594467 0.229608404 0.284505927
[64] 2.247991283 0.532787318 1.039424040 -0.129846379 0.136565215 1.033851456 1.238521154 0.296054062 -0.167632280
[73] -0.041663121 0.036396148 -0.055081083 1.166507105 0.773315084 0.523682291 0.851277285 0.884651013 0.025497212
[82] -0.326497225 -0.306819894 0.001277301 -0.125219240
Field "nn_vals":
[1] 4 6 6 4 3 7 3 3 5 5 7 3 3 6 5 5 6 6 7 3 6 7 2 5 6 6 2 6 6 6 4 4 6 6 5 6 5 4 6 7 5 4 7 6 6 5 8 4 6 6 5 4 5 4 2 6 3 6
[59] 6 2 6 3 3 2 4 2 4 5 7 6 2 3 8 6 5 5 5 6 3 6 4 6 6 6 5
Field "labels":
[1] "Not significant" "High-High" "Low-Low" "Low-High" "High-Low" "Undefined"
[7] "Isolated"
Field "colors":
[1] "#eeeeee" "#FF0000" "#0000FF" "#a7adf9" "#f4ada8" "#464646" "#999999"
4. さいごに
Spatial Dataの解析に使えそうなパッケージです。