>Hello List,
>I'm a very new user to the R system. I'm only beginning to learn the
>basics, but so far I've been able to do little more than try a few
>examples, and of course begin reading the documentation.
>My primary motivation for exploring R is the availability of tools like the
>'spdep' package for calculating spatial statistics such as
Geary's C and
>Moran's I, which I would like to use in an analysis for my thesis.
>However, I am not really sure how to get started.
>As a simple example, if I have a table with columns containing an ID
>field, X & Y coordinates, and an observation value, what steps should I
>follow to pre-process this data in order to use the moran() function?
I've
>been able to duplicate the example in the help documentation (e.g.,
>finzi.psych.upenn.edu/R/library/spdep/html/moran.html), but without
>understanding what the commands really do, I'm unable to proceed much
>further. How might I figure out the data that gets loaded when I run
>'data(oldcol)' for example?
>Is there anyone that might be able to give me some tips, or point me in
>the right direction?
>Thanks in advance for any suggestions.
>Mike
You may want to use this function (spdep required) or download the package
pgirmess from lbe.univ-fcomte.fr/telechar/div.html ( I must however
think to update it to the last version 1.1.3 ASAP) which includes it.
"correlog" <-
function(coords,z,method=c("Moran"),nbclass=NULL){
x<-require(spdep);if (!x) stop("Package spdep required")
coords<-as.matrix(coords)
matdist<-dist(coords)
if (is.null(nbclass)) nbclass<-nclass.Sturges(matdist)
etendue<-range(dist(coords))
breaks1<-seq(etendue[1],etendue[2],l=nbclass+1)
breaks2<-breaks1+0.000001
breaks<-cbind(breaks1[1:length(breaks1)-1],breaks2[2:length(breaks2)])
x<-NULL;N=NULL;p=NULL
for(i in 1:length(breaks[,1])){
z1<-z
nb1<-dnearneigh(coords, breaks[i,1],breaks[i,2])
zero<-which(card(nb1)==0)
if (length(zero)>0){
nb1<-dnearneigh(coords[-zero,], breaks[i,1],breaks[i,2])
z1<-z[-zero]
}
xt<-switch(pmatch(method,c("Moran","Geary"),nomatch=""),
"1"=try(moran.test(z1, nb2listw(nb1,
style="W")),silent=T),
"2"=try(geary.test(z1, nb2listw(nb1,
style="W")),silent=T),
stop("Method must be 'Moran' or
'Geary'"))
if(inherits(xt,"try-error")) {stop("Bad selection of
class breaks,
try another one...")}
else
x<-c(x,xt$estimate[1]);p<-c(p,xt$p.value);N<-c(N,sum(card(nb1)))
meth<-names(xt[[3]][1])
}
names(x)<-NULL
res<-cbind(dist.class=rowMeans(breaks),coef=x,p.value=p,n=N)
attributes(res)<-c(attributes(res),list(Method=meth))
class(res)<-c("correlog","matrix")
res
}
"plot.correlog"<-function (x,type,xlab,ylab,main,...) {
if (!inherits(x, "correlog")) stop("Object must be of class
'correlog'")
if (missing(main)) main<-paste(attributes(res)$Method," =
f(distance
classes)",sep="")
if (missing(type)) type<-"b"
if (missing(ylab)) ylab<-attributes(res)$Method
if (missing(xlab)) xlab<-"distance classes"
plot(x[,1:2,drop=F],type=type,xlab=xlab,ylab=ylab,main=main,xaxt="n",...)
inc<-(x[2,1]-x[1,1])/2
breaks <- pretty(c(x[1,1]-inc,x[length(x[,1]),1]+inc), n =
length(x[,1]), min.n = 2)
axis(1,at=breaks,...)
points(x[x[,3]<0.05,1:2,drop=F],pch=19,col="red",cex=2)
}
"print.correlog"<-function (x) {
if (!inherits(x, "correlog")) stop("Object must be of class
'correlog'")
cat(attributes(x)$Method,"\n")
attributes(x)<-attributes(x)[1:2]
print(x[,,drop=F])
}
[[alternative HTML version deleted]]