空間モデリング(09年度春学期第03回演習)

演習の目的

  • R言語を使って,空間重み付け行列やMoran’s I,Geary’s Cなどの空間的自己相関指標を計算する.
  • 授業で提示した例題をRで計算する。

空間重み付け行列(1)

  • 空間隣接行列(隣接関係があれば1,なければ0と定義)
> neigh <- matrix(ncol=5,nrow=5,
+ c(0,1,1,0,1,
+   1,0,1,0,1,
+   1,1,0,1,1,
+   0,0,1,0,1,
+   1,1,1,1,0))
> neigh
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    1    1    0    1
[2,]    1    0    1    0    1
[3,]    1    1    0    1    1
[4,]    0    0    1    0    1
[5,]    1    1    1    1    0
  • 隣接行列から重み付け行列を計算
> neigh.w <- matrix(ncol=5,nrow=5,0)
> neigh.w
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    0    0
[2,]    0    0    0    0    0
[3,]    0    0    0    0    0
[4,]    0    0    0    0    0
[5,]    0    0    0    0    0
> for(i in 1:nrow(neigh)){
+ neigh.w[i,] <- neigh[i,] / sum(neigh[i,])}
> neigh.w
          [,1]      [,2]      [,3] [,4]      [,5]
[1,] 0.0000000 0.3333333 0.3333333 0.00 0.3333333
[2,] 0.3333333 0.0000000 0.3333333 0.00 0.3333333
[3,] 0.2500000 0.2500000 0.0000000 0.25 0.2500000
[4,] 0.0000000 0.0000000 0.5000000 0.00 0.5000000
[5,] 0.2500000 0.2500000 0.2500000 0.25 0.0000000

空間重み付け行列(2)

  • 地点間の距離から重み付け行列を計算する
> x <- c(1,3,4,5,3)
> y <- c(3,4,3,1,1)
> id <- c(1,2,3,4,5)
> pnt <-data.frame(t(rbind(x,y)),row.names=id)
> pnt
  x y
1 1 3
2 3 4
3 4 3
4 5 1
5 3 1
> pnt$x[2]
[1] 3
> dist <- matrix(nrow=nrow(pnt),ncol=nrow(pnt),0)
> for(i in 1:nrow(pnt)){
+ for(j in 1:nrow(pnt)){
+ dist[i,j] <- sqrt((pnt$x[i]-pnt$x[j])^2+(pnt$y[i]-pnt$y[j])^2)
+ }}
> dist
         [,1]     [,2]     [,3]     [,4]     [,5]
[1,] 0.000000 2.236068 3.000000 4.472136 2.828427
[2,] 2.236068 0.000000 1.414214 3.605551 3.000000
[3,] 3.000000 1.414214 0.000000 2.236068 2.236068
[4,] 4.472136 3.605551 2.236068 0.000000 2.000000
[5,] 2.828427 3.000000 2.236068 2.000000 0.000000
> dist.w <- dist * neigh
> dist.w
         [,1]     [,2]     [,3]     [,4]     [,5]
[1,] 0.000000 2.236068 3.000000 0.000000 2.828427
[2,] 2.236068 0.000000 1.414214 0.000000 3.000000
[3,] 3.000000 1.414214 0.000000 2.236068 2.236068
[4,] 0.000000 0.000000 2.236068 0.000000 2.000000
[5,] 2.828427 3.000000 2.236068 2.000000 0.000000
> for(i in 1:nrow(neigh)){
+ dist.w[i,] <- neigh[i,] / sum(neigh[i,])}
> dist.w
          [,1]      [,2]      [,3] [,4]      [,5]
[1,] 0.0000000 0.3333333 0.3333333 0.00 0.3333333
[2,] 0.3333333 0.0000000 0.3333333 0.00 0.3333333
[3,] 0.2500000 0.2500000 0.0000000 0.25 0.2500000
[4,] 0.0000000 0.0000000 0.5000000 0.00 0.5000000
[5,] 0.2500000 0.2500000 0.2500000 0.25 0.0000000

Moran's Iの計算

  • 空間隣接行列(Rook's case)
> N=9
> neigh.rook <- matrix(ncol=N,nrow=N,c(
+ 0,1,0,1,0,0,0,0,0,
+ 1,0,1,0,1,0,0,0,0,
+ 0,1,0,0,0,1,0,0,0,
+ 1,0,0,0,1,0,1,0,0,
+ 0,1,0,1,0,1,0,1,0,
+ 0,0,1,0,1,0,0,0,1,
+ 0,0,0,1,0,0,0,1,0,
+ 0,0,0,0,1,0,1,0,1,
+ 0,0,0,0,0,1,0,1,0))
> neigh.rook
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    0    1    0    1    0    0    0    0    0
 [2,]    1    0    1    0    1    0    0    0    0
 [3,]    0    1    0    0    0    1    0    0    0
 [4,]    1    0    0    0    1    0    1    0    0
 [5,]    0    1    0    1    0    1    0    1    0
 [6,]    0    0    1    0    1    0    0    0    1
 [7,]    0    0    0    1    0    0    0    1    0
 [8,]    0    0    0    0    1    0    1    0    1
 [9,]    0    0    0    0    0    1    0    1    0
  • 空間重み付け行列
> neigh.rook.w <- neigh.rook
> for(i in 1:N){
> neigh.rook.w[i,] <- neigh.rook[i,] / sum(neigh.rook[i,])}
  • Moran's Iの計算
> X <- c(1,2,3,2,3,4,3,4,5)
> X.mean <- mean(X)
# Moran's Iの分子の一部
> moran1 <- matrix(ncol=N,nrow=N,0)
> for(i in 1:N){
+ for(j in 1:N){
+ moran1[i,j] <- neigh.rook.w[i,j] * (X[i] - X.mean) * (X[j] - X.mean)}}
> moran1
# Moran's I
> MoranI <- N * sum(moran1) / {sum(neigh.rook.w) * sum((X-X.mean)^2)}
> MoranI
[1] 0.5555556

トップ   編集 凍結解除 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2009-04-26 (日) 15:34:16 (3517d)