Skip to content
Prev 166343 / 398502 Next

troubles performing Moran.I test

<Barbara.Spillmann <at> agrar.uni-giessen.de> writes:
Using the ape package which is written for a different application area is 
not necessarily a good idea - the web page you refer to also has basic
blunders, mainly calculating and using planar distances when spherical
were called for. You repeat this in your data. Distances should be measured
by Great Circle between geographical coordinates.
...
The function takes a number of short cuts that are not obvious without reading
the code, or possibly reading Gittleman & Kot - it performs a hidden row
standardisation of the weights.

If we start from ape's Moran.I() example:

set.seed(1)
tr <- rtree(30)
x <- rnorm(30)
w <- 1/cophenetic(tr)
diag(w) <- 0
Moran.I(x, w)

we can recreate the same results using:

library(spdep)
# convert w to a row standardised general weights object
lw <- mat2listw(w)
lwW <- nb2listw(lw$neighbours, glist=lw$weights, style="W")
moran.test(x, lwW, alternative="two.sided")

Note that spdep provides functions for calculating Great Circle distances, 
see dnearneigh() and nbdist().
Inserting an NA into x:

is.na(x[5]) <- TRUE
Moran.I(x, w, na.rm=TRUE)

and

xc <- complete.cases(x)
wc <- w[xc, xc]
lwc <- mat2listw(wc)
lwWc <- nb2listw(lwc$neighbours, glist=lwc$weights, style="W")
moran.test(x[xc], lwWc, alternative="two.sided")

agree in the coefficient but not beyond that (ei is not for the correct n 
in Moran.I()). By the way, Moran's I really doesn't make sense for missing 
data. There is a provision in moran.test() for subsetting, but not for the 
general weights matrix you are using.
Since it isn't NA in your variable, it must be something else, so use 
debug(Moran.I) to see which of obs and/or ei are NAs in the line you quote.

Have you had the opportunity to review the Spatial task view on CRAN?
It is not impossible that it might shed some light on your problem, possibly
more than the authority you cite, including a link to the R-sig-geo list. 

Roger Bivand