Dear list,
I’m an undergraduate research assistant trying to create a single function
that can calculate Moran’s I using the “ape” package.  The example that I
will use comes from online data about ozone levels. Here are the commands
that I have been using:
> library(ape)
>
ozone<-read.table("http://www.ats.ucla.edu/stat/r/faq/ozone.csv",
sep=",", header=T)
> head(ozone, n=10)
   Station     Av8top      Lat          Lon          county
1       60      7.225806   34.13583  -117.9236  1
2       69      5.899194   34.17611  -118.3153  1
3       72      4.052885   33.82361  -118.1875  1
4       74      7.181452   34.19944  -118.5347  1
5       75      6.076613   34.06694  -117.7514  2
6       84      3.157258   33.92917  -118.2097  2
7       85      5.201613   34.01500  -118.0597  2
8       87      4.717742   34.06722  -118.2264  2
9       88      6.532258   34.08333  -118.1069  2
10      89     7.540323   34.38750  -118.5347  2
   >attach(ozone)
> Moran<- function(x)
+ {
+ distmatrix<- as.matrix(dist(cbind(Lon, Lat)))
+ invdistmatrix<- 1/distmatrix
+ diag(invdistmatrix)<-0
+ Moran.I(Av8top, invdistmatrix)
+ print(Moran.I)
+ }
> Moran.I(ozone)
I either receive an error saying that the matrix is not weighted, or I
receive a significant amount of output that does not include a calculation
for Moran’s I.  Do you have any suggestions on how to correctly create a
single function that calculates Moran’s I?
My ultimate goal is to run this function as a loop. I want to create a loop
that produces Moran’s I values for subsets of a very large database.
Specifically, I have points (block centroids) with population
characteristics. These points are nested within approximately 35,000 zip
codes. I want to create Moran’s I across blocks for each of the ~35,000 and
have the output stored in a new file that contains the zip code number and
Moran’s I values.  In the example of the ozone data I show above,  I would
create a loop that produces Moran’s I values for county 1 and county 2 and
then have the output stored in a new file with two records (one for each
county) and values for each county and the corresponding Moran’s I values.
Do you have any advice on creating this loop? Any help would be greatly
appreciated!
	[[alternative HTML version deleted]]