R: ドロネー三角網とか近隣点とか [統計]
この本をよみながら いろいろためしてみるテスト。
library(spdep) set.seed(123) n <- 24 x <- rnorm(n, 0, 1) y <- rnorm(n, 0, 1) xy <- cbind(x, y) s <- SpatialPoints(xy) coords <- coordinates(xy) plot(xy) text(x, y, 1:n, pos = 1)
データを生成して、プロットしてみる。
t <- tri2nb(xy) plot(t, coords) text(x, y, 1:n, pos = 1)
ドロネー三角網を作成してプロット。
どの点が連結しているか、またその距離を表示
> lapply(1:n, function(i) t[[i]]) [[1]] [1] 2 14 15 17 22 23 24 [[2]] [1] 1 5 8 18 19 22 23 [[3]] [1] 6 11 16 [[4]] [1] 7 10 12 13 14 15 [[5]] [1] 2 17 19 22 [[6]] [1] 3 11 16 20 [[7]] [1] 4 11 13 14 16 17 [[8]] [1] 2 9 15 18 21 23 [[9]] [1] 8 10 15 21 [[10]] [1] 4 9 12 15 20 21 [[11]] [1] 3 6 7 12 13 16 20 [[12]] [1] 4 10 11 13 20 [[13]] [1] 4 7 11 12 [[14]] [1] 1 4 7 15 17 [[15]] [1] 1 4 8 9 10 14 23 24 [[16]] [1] 3 6 7 11 17 19 [[17]] [1] 1 5 7 14 16 19 22 [[18]] [1] 2 8 20 21 [[19]] [1] 2 5 16 17 [[20]] [1] 6 10 11 12 18 21 [[21]] [1] 8 9 10 18 20 [[22]] [1] 1 2 5 17 [[23]] [1] 1 2 8 15 24 [[24]] [1] 1 15 23 > lapply(1:n, function(i) dist(xy[c(i, t[[i]]), ])[1:length(t[[i]])]) [[1]] [1] 1.1118481 0.8761086 0.3191103 1.0606167 0.6044665 0.5158194 0.2311910 [[2]] [1] 1.1118481 0.6558425 1.7342421 2.2807896 1.0223725 0.5637168 1.5104651 [[3]] [1] 0.4444397 0.3350187 1.2394475 [[4]] [1] 0.4764420 0.8897806 0.6084476 0.5191432 0.2190012 0.7767259 [[5]] [1] 0.6558425 0.5766009 0.5860520 0.3475877 [[6]] [1] 0.4444397 0.6541335 1.6358645 2.3715393 [[7]] [1] 0.4764420 0.8593829 0.1409318 0.6009780 1.5522282 1.1217794 [[8]] [1] 1.7342421 1.3232136 0.7093037 0.7069488 1.5159196 0.2622439 [[9]] [1] 1.3232136 0.2417887 1.2082124 0.4929557 [[10]] [1] 0.8897806 0.2417887 0.8274654 1.1892111 1.2911075 0.7041818 [[11]] [1] 0.3350187 0.6541335 0.8593829 0.8744326 0.8657272 1.3272936 2.1667482 [[12]] [1] 0.6084476 0.8274654 0.8744326 0.1408109 1.6984010 [[13]] [1] 0.5191432 0.1409318 0.8657272 0.1408109 [[14]] [1] 0.8761086 0.2190012 0.6009780 0.7097992 0.7418414 [[15]] [1] 0.3191103 0.7767259 0.7093037 1.2082124 1.1892111 0.7097992 0.4800494 0.2361535 [[16]] [1] 1.239448 1.635864 1.552228 1.327294 1.326811 1.400545 [[17]] [1] 1.0606167 0.5766009 1.1217794 0.7418414 1.3268108 0.6058885 0.8342265 [[18]] [1] 2.2807896 0.7069488 2.8073193 1.6770640 [[19]] [1] 1.0223725 0.5860520 1.4005454 0.6058885 [[20]] [1] 2.371539 1.291108 2.166748 1.698401 2.807319 1.130298 [[21]] [1] 1.5159196 0.4929557 0.7041818 1.6770640 1.1302977 [[22]] [1] 0.6044665 0.5637168 0.3475877 0.8342265 [[23]] [1] 0.5158194 1.5104651 0.2622439 0.4800494 0.3038798 [[24]] [1] 0.2311910 0.2361535 0.3038798
つづいて、近隣点をもとめてみる。
set.seed(123) n.x <- 5 n.y <- 5 g.xy <- expand.grid(1:n.x, 1:n.y) + rnorm(n.x * n.y * 2, 0, 0.1) g.coords <- coordinates(g.xy) g.pnt <- SpatialPoints(g.xy) g.nb <- knn2nb(knearneigh(g.pnt, k = 4)) plot(g.nb, g.coords)
タグ:R
2012-02-13 20:51
nice!(0)
コメント(0)
トラックバック(0)
コメント 0