I sent this to Roger Bivand earlier. Posting it here for
posterity, and in case other people have more to add:
---------------------------------------------------------------------------
Hi Roger,
I'm taking a spatial econometrics class, and we've been making some
LISA maps. We noticed that the p-values from the spdep package seem to
be a lot different than the results from Geoda or from Python's PySAL
package.
I looked through the function definitions and noticed that Python, and
I think Geoda, use simulations to estimate the p-value, while your
localmoran function uses an equation.
I attached the graphs from Geoda and from R, and my R code and the
original dataset. [R code is added below.] Is this kind of
difference normal?
Is there a reason to prefer spdep's results over the Geoda results (or
vice versa)?
Thanks for any help,
Will
---------------------------------------------------------------------------
Here's his response:
---------------------------------------------------------------------------
Please *do* write to the R-sig-geo list rather than to me directly -
others can answer your question as well, perhaps better, and in a more
timely way than I can. In addition, threads in the list can be
searched in the archives, so others can avoid the same problem later.
The only reliable test is localmoran.exact(), followed by
localmoran.sad(), for reasons given in the references in their help
pages.? If you test more than one observation, remember to adjust the
p-value for multiple comparisons. Sampling _i (all but i)
overrepresents non-neighbours of i for a test on i. The formulae used
are from the original paper and most often are similar to the
permutation bootstrap values.
Crucially, almost all use of local Moran's I (and local Geary and
Getis) fall foul of the autocorrelation being related to a
mis-specified mean model (omitted explanatory variables and/or omitted
adjustment for global autocorrelation). Look at the references to
localmoran.sad and localmoran.exact for more details, and at the
relevant parts of Waller & Gotway (2004), Schabenberger & Gotway
(2005), and in Bivand et al. (2008, 2013, ASDAR-book, ch. 9).
LISA maps are extremely misleading when the mean model is
mis-specified, and when the p-values are not adjusted for multiple
comparisons. They look simple, but because adjustment for multiple
comparisons is a subjective choice, you can almost choose the map you
want.
Roger
---------------------------------------------------------------------------
And here's my R code:
---------------------------------------------------------------------------
# make a LISA cluster map
library(maptools)
library(spdep)
setwd('/home/will/classes/polisci_610/1/')
# get the data
nat <- readShapeSpatial('NAT.SHP',)
nat.data<-data.frame(nat)
attach(nat.data)
# nearest neighbors (nnb)
coords<-coordinates(nat)
IDs<-row.names(as(nat, 'data.frame'))
nat_10nnb<-knn2nb(knearneigh(coords, k0010), row.names=IDs)
# 'W' identifies row standardized weights
nat_10nnb_w <- nb2listw(nat_10nnb,)
# can preset list_w object; if do this, remember to change if needed
list_w <- nat_10nnb_w
# LISA values
lisa <- localmoran(HR60, list_w, zero.policy = T)
# centers the variable of interest around its mean
cDV <- HR60 - mean(HR60)
# centers the local Moran's around the mean
mI <- lisa[, 1]
C_mI <- mI - mean(mI) # but we don't want to center it! Only the sign
# matters.
quadrant <- vector(mode='numeric',length=nrow(lisa))
quadrant[cDV>0 & mI>0] <- 1
quadrant[cDV <0 & mI>0] <- 2?????
quadrant[cDV>0 & mI<0] <- 3
quadrant[cDV <0 & mI<0] <- 4
# set a statistical significance level for the local Moran's
signif <- 0.05
# places non-significant Moran's in the category '5'
quadrant[lisa[, 5]> signif] <- 5
# map
png(file='Lisa_map_R.png', width = 680, res = 100)
colors <- c('red', 'blue', 'lightpink', 'skyblue2', rgb(.95, .95, .95))
par(mar=c(0,0,1,0)) # sets margin parameters for plot space
plot(nat, border='grey', col=colors[quadrant],
??? ? main = 'LISA Cluster Map, 1960 Homicides')
legend('bottomright',legend=c('high-high','low-low','high-low','low-high'),
?????? fill=colors,bty='n',cex=0.7,y.intersp=1,x.intersp=1)
dev.off()
---------------------------------------------------------------------------
Dear members of list,
I calculated a local moran index ('localmoran()') for my data (which come
from a raster grid) with a queen spatial neighbor structure for construct
the weights matrix.
I would like to plot in a map the value of local moran index for each zone,
however, I am not sure how to extract spatial information for each one of
the values obtained of local moran index.
My question is: if I am well in assume that the result of localmoran()
function is in the same order that neighbor list, thus, I can assign
coordinates of neighbors to each row of result matrix? something like:
#Calculating neighbors
kneig=knearneigh(cbind(data$x,data$y),k=8)
neig=knn2nb(neig)
#Calculating weights for neighbors
neiglist=nb2listw(neig,style='W')
#Spatial autocorrelation
lmt=localmoran(data$z,neiglist)
#Data for plot local moran index
data_map=cbind(lmt,data$x,data$y)
Thanks in advance and best regards to all.
Dear members of list,
I calculated a local moran index ('localmoran()') for my data (which come
from a raster grid) with a queen spatial neighbor structure for construct
the weights matrix.
I would like to plot in a map the value of local moran index for each zone,
however, I am not sure how to extract spatial information for each one of
the values obtained of local moran index.
My question is: if I am well in assume that the result of localmoran()
function is in the same order that neighbor list, thus, I can assign
coordinates of neighbors to each row of result matrix? something like:
#Calculating neighbors
kneig=knearneigh(cbind(data$x,data$y),k=8)
neig=knn2nb(neig)
#Calculating weights for neighbors
neiglist=nb2listw(neig,style='W')
#Spatial autocorrelation
lmt=localmoran(data$z,neiglist)
#Data for plot local moran index
data_map=cbind(lmt,data$x,data$y)
Thanks in advance and best regards to all.
x w
posterity, and in case other people have more to add:
---------------------------------------------------------------------------
Hi Roger,
I'm taking a spatial econometrics class, and we've been making some
LISA maps. We noticed that the p-values from the spdep package seem to
be a lot different than the results from Geoda or from Python's PySAL
package.
I looked through the function definitions and noticed that Python, and
I think Geoda, use simulations to estimate the p-value, while your
localmoran function uses an equation.
I attached the graphs from Geoda and from R, and my R code and the
original dataset. [R code is added below.] Is this kind of
difference normal?
Is there a reason to prefer spdep's results over the Geoda results (or
vice versa)?
Thanks for any help,
Will
---------------------------------------------------------------------------
Here's his response:
---------------------------------------------------------------------------
Please *do* write to the R-sig-geo list rather than to me directly -
others can answer your question as well, perhaps better, and in a more
timely way than I can. In addition, threads in the list can be
searched in the archives, so others can avoid the same problem later.
The only reliable test is localmoran.exact(), followed by
localmoran.sad(), for reasons given in the references in their help
pages.? If you test more than one observation, remember to adjust the
p-value for multiple comparisons. Sampling _i (all but i)
overrepresents non-neighbours of i for a test on i. The formulae used
are from the original paper and most often are similar to the
permutation bootstrap values.
Crucially, almost all use of local Moran's I (and local Geary and
Getis) fall foul of the autocorrelation being related to a
mis-specified mean model (omitted explanatory variables and/or omitted
adjustment for global autocorrelation). Look at the references to
localmoran.sad and localmoran.exact for more details, and at the
relevant parts of Waller & Gotway (2004), Schabenberger & Gotway
(2005), and in Bivand et al. (2008, 2013, ASDAR-book, ch. 9).
Raster Local Moran P Value Fund
LISA maps are extremely misleading when the mean model is
mis-specified, and when the p-values are not adjusted for multiple
comparisons. They look simple, but because adjustment for multiple
comparisons is a subjective choice, you can almost choose the map you
want.
Roger
---------------------------------------------------------------------------
And here's my R code:
---------------------------------------------------------------------------
# make a LISA cluster map
library(maptools)
library(spdep)
setwd('/home/will/classes/polisci_610/1/')
# get the data
nat <- readShapeSpatial('NAT.SHP',)
nat.data<-data.frame(nat)
attach(nat.data)
# nearest neighbors (nnb)
coords<-coordinates(nat)
IDs<-row.names(as(nat, 'data.frame'))
nat_10nnb<-knn2nb(knearneigh(coords, k0010), row.names=IDs)
# 'W' identifies row standardized weights
nat_10nnb_w <- nb2listw(nat_10nnb,)
# can preset list_w object; if do this, remember to change if needed
list_w <- nat_10nnb_w
# LISA values
lisa <- localmoran(HR60, list_w, zero.policy = T)
# centers the variable of interest around its mean
cDV <- HR60 - mean(HR60)
# centers the local Moran's around the mean
mI <- lisa[, 1]
C_mI <- mI - mean(mI) # but we don't want to center it! Only the sign
# matters.
quadrant <- vector(mode='numeric',length=nrow(lisa))
quadrant[cDV>0 & mI>0] <- 1
quadrant[cDV <0 & mI>0] <- 2?????
quadrant[cDV>0 & mI<0] <- 3
quadrant[cDV <0 & mI<0] <- 4
# set a statistical significance level for the local Moran's
signif <- 0.05
# places non-significant Moran's in the category '5'
quadrant[lisa[, 5]> signif] <- 5
# map
png(file='Lisa_map_R.png', width = 680, res = 100)
colors <- c('red', 'blue', 'lightpink', 'skyblue2', rgb(.95, .95, .95))
par(mar=c(0,0,1,0)) # sets margin parameters for plot space
plot(nat, border='grey', col=colors[quadrant],
??? ? main = 'LISA Cluster Map, 1960 Homicides')
legend('bottomright',legend=c('high-high','low-low','high-low','low-high'),
?????? fill=colors,bty='n',cex=0.7,y.intersp=1,x.intersp=1)
dev.off()
---------------------------------------------------------------------------
Dear members of list,
I calculated a local moran index ('localmoran()') for my data (which come
from a raster grid) with a queen spatial neighbor structure for construct
the weights matrix.
I would like to plot in a map the value of local moran index for each zone,
however, I am not sure how to extract spatial information for each one of
the values obtained of local moran index.
My question is: if I am well in assume that the result of localmoran()
function is in the same order that neighbor list, thus, I can assign
coordinates of neighbors to each row of result matrix? something like:
#Calculating neighbors
kneig=knearneigh(cbind(data$x,data$y),k=8)
neig=knn2nb(neig)
#Calculating weights for neighbors
neiglist=nb2listw(neig,style='W')
#Spatial autocorrelation
lmt=localmoran(data$z,neiglist)
#Data for plot local moran index
data_map=cbind(lmt,data$x,data$y)
Thanks in advance and best regards to all.
Dear members of list,
I calculated a local moran index ('localmoran()') for my data (which come
from a raster grid) with a queen spatial neighbor structure for construct
the weights matrix.
I would like to plot in a map the value of local moran index for each zone,
however, I am not sure how to extract spatial information for each one of
the values obtained of local moran index.
My question is: if I am well in assume that the result of localmoran()
function is in the same order that neighbor list, thus, I can assign
coordinates of neighbors to each row of result matrix? something like:
#Calculating neighbors
kneig=knearneigh(cbind(data$x,data$y),k=8)
neig=knn2nb(neig)
Raster Local Moran P Value List
#Calculating weights for neighbors
neiglist=nb2listw(neig,style='W')
#Spatial autocorrelation
lmt=localmoran(data$z,neiglist)
#Data for plot local moran index
data_map=cbind(lmt,data$x,data$y)
Thanks in advance and best regards to all.
I want to test the correlation in the values between 2 spatial raster data sets (that perfectly overlap).
I could just do:
but both raster datasets are spatial autocorrelated.
Instead, I am using:
from the
SpatialPack
library. This is based on Dutilleul's test that modifies that effective sample size based on the degree of autocorrelation.However, the modified test does not change the estimated correlation coefficient, only the p-value.
How do I also correct the estimated correlation coefficient for the extent of autocorrelation?
tsutsumetsutsume
1 Answer
This is more a stats than a programming question.
I do not think you can 'correct the correlation coefficient for autocorrelation'. The correlation coefficient is what it is. It is not affected by 'oversampling'.
No 'inflation' when using the same values twice
The p-value is affected
You can adjust the p-value for oversampling. However, a question with raster data is whether you should indeed consider these as a sample. That depends on context, but raster data often represent the entire population (with some local averaging given that cells are discreet). If there is no uncertainty due to (a small) sample size, presenting a p-value is not meaningful.
For example, you mistakenly delete files from D drive on Windows 8 and then empty recycle bin, and then you can run the data recovery software to deep can D drive so as to search every single. Undelete Files after Emptying Recycle Bin on Windows 8If you want to after emptying Recycle Bin on Windows 8, you can run iCare Data Recovery to deep scan the hard disk partition where data got deleted.
Robert HijmansRobert Hijmans Delete files from Recycle Bin by mistake on Windows 8Probably, you mistakenly click 'Empty Recycle Bin' option after which you erase everything stored in the Recycle Bin. Or you mistakenly deleted selected files in Recycle Bin when you attempted to restore them.You may empty Recycle Bin due to some other reasons, but you should remember not to write new data to the hard drive where data got deleted in order to undelete files after emptying Recycle Bin on Windows 8.
15.4k11 gold badge2525 silver badges3131 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Not the answer you're looking for? Browse other questions tagged rgeospatialcorrelationrasterspatialpack or ask your own question.
Spatial autocorrelation
Compute Moran's I or Geary's C measures of global spatial autocorrelation in a RasterLayer, or compute the the local Moran or Geary index (Anselin, 1995).
- Keywords
- spatial
Usage
Arguments
RasterLayer
Spatial weights defined by or a rectangular matrix with odd length (3, 5, ..) sides (as in
focal
)Details
The default setting uses a 3x3 neighborhood to compute 'Queen's case' indices. You can use a filter (weights matrix) to do other things, such as 'Rook's case', or different lags.
Value
A single value (Moran's I or Geary's C) or a RasterLayer (Local Moran or Geary values)
References
Moran, P.A.P., 1950. Notes on continuous stochastic phenomena. Biometrika 37:17-23
Geary, R.C., 1954. The contiguity ratio and statistical mapping. The Incorporated Statistician 5: 115-145
Anselin, L., 1995. Local indicators of spatial association-LISA. Geographical Analysis 27:93-115
See Also
The spdep package for additional and more general approaches for computing indices of spatial autocorrelation
Aliases
- Geary
- Moran
- MoranLocal
- GearyLocal
Examples
library(raster)
# NOT RUN {r <- raster(nrows=10, ncols=10)r[] <- 1:ncell(r)Moran(r)# Rook's casef <- matrix(c(0,1,0,1,0,1,0,1,0), nrow=3)Moran(r, f)Geary(r)x1 <- MoranLocal(r)# Rook's casex2 <- MoranLocal(r, w=f)# }
Raster Local Moran P Values
Documentation reproduced from package raster, version 2.6-7, License: GPL (>= 3)Community examples
API documentation