Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 :
• 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"))
• Even when the values are binary 0/1, the issue of what to do with no-
neighbor observations arises
???
???
Menghitung Indeks Moran (1)
#compute Moran's Index pm <- matrix(yiyj, ncol=n)
pmw <- pm * wm
n <- length(p) pmw
y <- p$value spmw <- sum(pmw)
ybar <- mean(y) spmw
x smw <- sum(wm)
#####1st method sw <- spmw / smw
dy <- y - ybar vr <- n / sum(dy^2)
g <- expand.grid(dy, dy) MI <- vr * sw
yiyj <- g[,1] * g[,2] MI
EI <- -1/(n-1)
#####2nd method 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
#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.sided” → HA: I ≠ I0
“greater” → HA: I > I0
Diagram Pencar Moran
n <- length(p) reg <- lm(ams[,2] ~ ams[,1])
ms <- cbind(id=rep(1:n, each=n), abline(reg, lwd=2)
y=rep(y, each=n), abline(h=mean(ams[,2]), lt=2)
value=as.vector(wm * y))
abline(v=ybar, lt=2)
ms <- ms[ms[,3] > 0, ]
coefficients(reg)[2]
ams <- aggregate(ms[,2:3], list(ms[,1]),
FUN=mean) rwm <- mat2listw(wm, style='W')
ams <- ams[,-1] # Checking if rows add up to 1
colnames(ams) <- c('y', 'spatially lagged y') mat <- listw2mat(rwm)
head(ams) apply(mat, 1, sum)[1:15]
plot(ams) moran.plot(y, rwm)
Moran Scatter Plot
rwm <- mat2listw(wm, style='W')
moran.plot(y, rwm)
Moran Scatter Plot
Latihan
• Input data berikut:
kemiskinan<-read.csv("http://bit.ly/dataKemiskinan",sep=',',header=T)