ywh19860616 发表于 2012-5-13 16:42 
library(ape)
library(fields)
yc=as.numeric(t(c(40.16304......)
data<-read.csv('moran.csv') # "data.frame"
data=data[,1] #"numeric"
换成"numeric"是为了配合function Moran.I(x, weight)
x : a numeric vector.的要求
data<-read.csv('data1.csv') # "data.frame"
data=data[,1] #"numeric"
p<-nrow(data)
LL<-cbind(xc,yc)
dista<-rdist.earth(LL)
d = seq(50,1000,50)
s<-length(d)
n<-ncol(dista)
WW= array(NA, dim = c(n,n,s))
moranImat=matrix(NA,s,4);
for (p in 1:s){
dw<-dista
for (i in 1:n){
for (j in 1:n) {
if (dw[i,j] <= d[p]) dw[i,j] =0 else
dw[i,j] = 1
} #j
} #i
WW[,,p]=dw
moranImat[p,1]=Moran.I(data,dw)$observed
moranImat[p,2]=Moran.I(data,dw)$expected
moranImat[p,3]=Moran.I(data,dw)$sd
moranImat[p,4]=Moran.I(data,dw)$p.value
} #p
colnames(moranImat) = c("observed","expected","sd", "p.value")
print(moranImat)
observed expected sd p.value
[1,] -0.03562998 -0.03571429 0.003497052 0.98076591
[2,] -0.03505226 -0.03571429 0.004949200 0.89358897
[3,] -0.03505226 -0.03571429 0.004949200 0.89358897
[4,] -0.03815054 -0.03571429 0.007036459 0.72916722
[5,] -0.05173708 -0.03571429 0.011300924 0.15624031
[6,] -0.05570482 -0.03571429 0.014758350 0.17556940
[7,] -0.06194603 -0.03571429 0.018383120 0.15359511
[8,] -0.05337594 -0.03571429 0.019858576 0.37380342
[9,] -0.06442231 -0.03571429 0.022407499 0.20013071
[10,] -0.05243863 -0.03571429 0.026124726 0.52206003
[11,] -0.05588271 -0.03571429 0.029924213 0.50032175
[12,] -0.06776555 -0.03571429 0.033569987 0.33969935
[13,] -0.07897514 -0.03571429 0.036233464 0.23249847
[14,] -0.07869158 -0.03571429 0.039075959 0.27140198
[15,] -0.10539806 -0.03571429 0.042226614 0.09889518
[16,] -0.12439059 -0.03571429 0.045660253 0.05212624
[17,] -0.12607884 -0.03571429 0.050839595 0.07549515
[18,] -0.13276167 -0.03571429 0.054816061 0.07665742
[19,] -0.15463911 -0.03571429 0.059795454 0.04671659
[20,] -0.19021641 -0.03571429 0.064245291 0.01617784