Sei sulla pagina 1di 35

Autokorelasi Spasial

Statistika Spasial | Praktikum ke-4 rrahmaanisa@apps.ipb.ac.id

Autokorelasi temporal

set.seed(0)

 d <- sample(100, 10) d

a <- d[-length(d)]

b <- d[-1]

plot(a, b, xlab='t', ylab='t-1')

cor(a, b)

 d <- sort(d) d a <- d[-length(d)] b <- d[-1]

plot(a, b, xlab='t', ylab='t-1')

acf(d)

Autokorelasi

Spasial

??

Contoh Data Spasial

library(raster)

p <- shapefile(system.file("external/lux.shp", package="raster"))

p <- p[p\$NAME_1=="Diekirch", ]

p\$value <- c(10, 6, 4, 11, 6)

data.frame(p)

par(mai=c(0,0,0,0))

plot(p, col=2:7)

xy <- coordinates(p)

points(xy, cex=6, pch=20, col='white')

text(p, 'ID_2', cex=1.5)

Steps in determining the extent of spatial autocorrelation in your data :

1. Choose a neighborhood criterion

Which areas are linked?

2. Assign weights to the areas that are linked

Create a spatial weights matrix

3. Run statistical test to examine spatial autocorrelation

Step 1:

Choose a neighborhood criterion

Spatial weights matrices

Neighborhoods can be defined in a number of ways

Contiguity (common boundary)

What is a “shared” boundary?

Distance (distance band, K-nearest neighbors)

How many “neighbors” to include, what distance do we use?

General weights (social distance, distance decay)

Contiguity based neighbors

Areas sharing any boundary point (QUEEN) are taken as neighbors, using the poly2nb function, which accepts a SpatialPolygonsDataFrame

> library(spdep)

> w<-poly2nb(p)

If contiguity is defined as areas sharing more than one boundary point (ROOK), the queen= argument is set to FALSE

> w.rook<-poly2nb(p, queen=FALSE)

> coords<-coordinates(p)

> plot(p)

> plot(w, coords, add=T)

Queen contiguity

Rook contiguity

Distance based neighbors

k nearest neighbors

Can also choose the k nearest points as neighbors

> coords<-coordinates(p)

> IDs<-row.names(as(p, "data.frame"))

> p_kn1<-knn2nb(knearneigh(coords, k=1), row.names=IDs)

> p_kn2<-knn2nb(knearneigh(coords, k=2), row.names=IDs)

> p_kn4<-knn2nb(knearneigh(coords, k=4), row.names=IDs)

> plot(p)

> plot(p_kn2, coords, add=T)

k=2
k=1
k=3

k=1

k=2

k=4

Distance based neighbors : Specified distance

Can also assign neighbors based on a specified distance

> dist<-unlist(nbdists(p_kn1, coords))

> summary(dist)

Min. 1st Qu.

Median

Mean 3rd Qu.

Max.

0.07316 0.07316 0.14159 0.11832 0.14159 0.16213

> max_k1<-max(dist)

> p_kd1<-dnearneigh(coords, d1=0, d2=0.75*max_k1, row.names=IDs)

> p_kd2<-dnearneigh(coords, d1=0, d2=1*max_k1, row.names=IDs)

> p_kd3<-dnearneigh(coords, d1=0, d2=1.5*max_k1, row.names=IDs)

OR by raw distance

> p_ran1<-dnearneigh(coords, d1=0, d2=0.16123, row.names=IDs)

dist=1*max_k1

dist=1.5*max_k1

Distance=0.75*max_k1

Distance=1*max_k1

Distance=1.5*max_k1

Step 2:

Assign weights to the areas that are linked

Creating spatial weights matrices using neighborhood lists

Spatial weights matrices

Once our list of neighbors has been created, we assign spatial weights to each relationship

Can be binary or variable

Even when the values are binary 0/1, the issue of what to do with no- neighbor observations arises

Binary weighting will, for a target feature, assign a value of 1 to

neighboring features and 0 to all other features

Used with fixed distance, k nearest neighbors, and contiguity

Row-standardized weights matrix

> p_nbq_w<- nb2listw(w)

> p_nbq_w

Row standardization is used to create proportional weights in cases where

features have an unequal number of

neighbors

Divide each neighbor weight for a

feature by the sum of all neighbor weights

Obs i has 3 neighbors, each has a weight

of 1/3

Obs j has 2 neighbors, each has a weight

of 1/2

Use is you want comparable spatial

parameters across different data sets with different connectivity structures

Binary weights

> w_nbq_wb<-nb2listw(w, style="B")

> w_nbq_wb

Row-standardised weights increase the influence of links from

observations with few neighbours

Binary weights vary the influence of

observations

Those with many neighbours are up-

weighted compared to those with few

Binary vs. row-standardized

A binary weights matrix looks like:

0

0

1

0

1

0

1

1

A row-standardized matrix it looks like:

 0 0 1 1 0 0 1 1 0 1 0 0 0 0 .5 .5 0.5 0.5 0 0 0 0.33 .33 .33

Style Options

 Code Description B basic binary coding W row standardised (sums over all links to n) C globally standardised (sums over all links to n) U equal to C divided by the number of neighbours (sums over all links to unity) S the variance-stabilizing coding scheme

Regions with no neighbors

If you ever get the following error:

Error in nb2listw(filename ): Empty neighbor sets found

You have some regions that have NO neighbors

> p_nbq_w<-nb2listw(p_nbq, zero.policy=T)

Step 3:

Examine spatial autocorrelation

Using spatial weights matrices, run statistical tests of spatial autocorrelation

Spatial autocorrelation

Test for the presence of spatial autocorrelation

Global

Moran’s I

Geary’s C

Local (LISA Local Indicators of Spatial Autocorrelation)

Local Moran’s I and Getis G i *

We’ll just focus on the “industry standard” – Moran’s I

Autokorelasi Spasial

library(spdep) w <- poly2nb(p, row.names=p\$Id) class(w)

summary(w)

str(w)

plot(p, col='gray', border='blue', lwd=2)

plot(w, xy, col='red', lwd=2, add=TRUE)

wm <- nb2mat(w, style='B')

wm

Autokorelasi Spasial

???
???

Menghitung Indeks Moran (1)

#compute Moran's Index

n <- length(p)

y <- p\$value

ybar <- mean(y)

x

#####1st method

dy <- y - ybar

g <- expand.grid(dy, dy)

pm <- matrix(yiyj, ncol=n)

pmw <- pm * wm pmw

spmw <- sum(pmw)

spmw smw <- sum(wm)

sw <- spmw / smw

vr <- n / sum(dy^2) MI <- vr * sw

 yiyj <- g[,1] * g[,2] MI #####2nd method EI <- -1/(n-1) EI

yi <- rep(dy, each=n)

yj <- rep(dy) yiyj <- yi * yj

Menghitung Indeks Moran (2)

#compute moran's using spdep function

ww <- nb2listw(w, style='B')

ww

moran(p\$value, ww, n=length(ww\$neighbours), S0=Szero(ww))

#Note that

Szero(ww)

# is the same as

pmw

Moran’s I in R

> moran.test(p\$value, listw=ww, randomisation=FALSE, alternative=‘less’)

two.sidedH A : I ≠ I 0 “greater” H A : I > I 0

Diagram Pencar Moran

n <- length(p)

ms <- cbind(id=rep(1:n, each=n),

y=rep(y, each=n),

value=as.vector(wm * y))

ms <- ms[ms[,3] > 0, ]

ams <- aggregate(ms[,2:3], list(ms[,1]),

FUN=mean)

ams <- ams[,-1]

colnames(ams) <- c('y', 'spatially lagged y')

head(ams)

plot(ams)

reg <- lm(ams[,2] ~ ams[,1])

abline(reg, lwd=2)

abline(h=mean(ams[,2]), lt=2)

abline(v=ybar, lt=2)

coefficients(reg)[2]

rwm <- mat2listw(wm, style='W')

# Checking if rows add up to 1

mat <- listw2mat(rwm)

apply(mat, 1, sum)[1:15]

moran.plot(y, rwm)

Moran Scatter Plot

rwm <- mat2listw(wm, style='W')

# Checking if rows add up to 1

mat <- listw2mat(rwm)

apply(mat, 1, sum)

moran.plot(y, rwm)

Moran Scatter Plot

Latihan

Input data berikut:

kemiskinan<-read.csv("http://bit.ly/dataKemiskinan",sep=',',header=T)

Input data bobot berikut:

bobot<-read.csv("http://bit.ly/bobot_kemiskinan",sep=',',header=F)

Hitunglah indeks moran untuk data kemiskinan tsb!

Latihan

Mengubah data bobot ke dalam bentuk matriks

bot<-as.matrix(bobot)

Menghitung indeks moran global

w=mat2listw(bot)

moran(kemiskinan\$Y, listw=w, n=112, S0=Szero(w))

Latihan

Menghitung indeks moran lokal

localmoran(kemiskinan\$Y, w)

Membuat Moran scatter plot

moran.plot(kemiskinan\$Y, mat2listw(bot,style='W'),

labels=kemiskinan\$Nama.Kabupaten)