Giuseppe Vittucci
0.2
2011-09-04
Moran's I statistic of spatial autocorrelation
This package calculate Moran's I statistic to detect spatial autocorrelation in a cross-section. The null hypothesis of the
test is: the series does not exhibit any spatial autocorrelation.
The moran function prints Moran's I, the expected value (E(I)), the standard deviation (sd(I)), and the z-score, with the related
(1-tail) p-value. It returns the scalar value of the Moran's I.
Data requirement: series; spatial weights matrix.
The spatial weights matrix W must be square and its order must match the number of observations in the sample. The function does not assume a symmetric matrix and does not perform any standardization.
The parameters of the function are:
X - the series to be tested
W - the spatial weights matrix
The formulas are taken from Sokal, R.R., N.L. Oden, & B.A. Thomson (1998), Local spatial autocorrelation in a biological model, Geographical Analysis 30: 331-354.
For any suggestion, bug, etc. write to:
giuseppe.vittucci@unimib.it
Series to test
Spatial weights matrix
```
# This function calculate Moran's I for spatial autocorrelation
# z-score and p-value under the null of no spatial correlation
# The standard deviation (sd(I)) is computed under the assumption of total randomization. Under this assumption the index follows an asymptotically normal distribution
# Formulas taken from Sokal, R.R., N.L. Oden, & B.A. Thomson (1998), Local spatial autocorrelation in a biological model, Geographical Analysis 30: 331-354.
#
# X - series to test
# W - spatial weights matrix (the test is computed for the more general hypothesis of asymmetric non-normalized matrix)
# check the data
i=rows(W)
j=cols(W)
if (i != j)
funcerr "The spatial weights matrix must be square"
endif
if (i != $nobs)
funcerr "The number of spatial weights does not match the number of observations"
endif
# number of non-missing observations
scalar n = sum(ok(X))
matrix x = misszero(X - mean(X))
scalar xss = x'*x
s0 = sumc(sumr(W))
# Moran's I
m = n/s0*(x'*W*x)/xss
# Expected value - E(I)
e = -1/(n-1)
#Standard deviation - Sd(I)
s1 = sumc(sumr((W + W').^2))/2
s2 = sumc((sumr(W) + sumr(W')).^2)
s3 = n*sumc(x.^4)/xss^2
s = sqrt((n*((n^2-3*n+3)*s1-n*s2+3*s0^2)-s3*((n^2-n)*s1-2*n*s2+6*s0^2))/((n-1)*(n-2)*(n-3)*s0^2)-1/(n-1)^2)
# z-score
z = (m-e)/s
#p-value
p = pvalue(N,z)
printf "Moran's I: %g, E(I) = %g, sd(I) = %g, z = %g, p-value = %g (1-tail test)\n", m, e, s, z, p
return m
```

include moran.gfn
open http://www.ecn.wfu.edu/pub/gretldata/columbusswm.gdt
matrix W = { dataset }
matrix WS = W ./ sumr(W) # row-standardize matrix
open http://www.ecn.wfu.edu/pub/gretldata/columbusdata.gdt --preserve
moran(crime,WS)