# Compute Moran's I or some other estimate of spatial correlation in R

I'm trying to do a little spatial analysis--just some simple spatial correlations. I have a data frame consisting of latitude and longitudinal points, a value to model, and a neighborhood indicator. To whit:

d1 <- data.frame(long  =  runif(n=1000, 10, 10.1),
lat = runif(n=1000, 10, 10.1),
val = rnorm(1000))

d1$neighb <- ifelse(d1$long  >= 10.05 &
d1$lat >= 10.05, "B", ifelse(d1$long  >= 10.05 &
d1$lat < 10.05, "D", ifelse(d1$long  < 10.05 &
d1$lat >= 10.05, "A", ifelse(d1$long  < 10.05 &
d1\$lat   < 10.05,
"C",NA))))


which results in the following

library(ggplot2)
ggplot(d1, aes(y = long, x = lat, color = val)) + geom_point() +facet_wrap(~neighb)


which produces

With apologies for asking such a rudimentary question--how do I estimate the degree of spatial correlation for the val variable?

Thanks!

-
There's no clear connection between your example and Moran's I: to calculate it, you need to specify a neighborhood for every location in the dataset. – whuber Nov 23 '12 at 21:07
Thanks @whuber . I'm obviously missing something-- every observation does have a value on the neighb variable, as indicated by the plot. Why doesn't this coarse categorization (the 1000 locations to one of four neighborhoods, letters A:D) suffice? – tom Nov 23 '12 at 21:13
Your example does not define neighborhoods in the sense needed for Moran's I. A neighborhood for this purpose is the assignment of a definite set of locations to each existing location. For instance, the neighborhood of a location might be all locations lying withing a distance of 0.005 of it. In addition, you need to supply weights for all those neighbors; typically, a weight depends on their distances and decreases with distance. In the question it appears you merely have divided the locations into four subgroups; that won't do at all. – whuber Nov 23 '12 at 21:15
Hmmm...ok. My real data is obviously a lot more convoluted than this, but it shares the same basic organization (in that a very large number of points were assigned to a smaller number of voting precincts by spatial intersection.) Is there some way to estimate these boundaries and weights from data of this sort? – tom Nov 23 '12 at 21:30
Well you have individuals nested within some higher level unit right? so all you need is the neighbor structure for the higher level units, not the individuals, if you assume they are independent within the higher level units, which is usually the assumption. If you want to model the spatial correlation of individuals within blocks that's different and you have a 2 level hierarchical model with spatial correlation at both levels. Which way are you wanting to do it? and are you wanting a covariance model or a CAR model or some other spatial model specification? – Corey Sparks Nov 24 '12 at 0:39