Subsetting NaN values in localG()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Subsetting NaN values in localG()

dab98
Hi,

I'm currently trying to calculate local Getis-Ord Gi* statistics for a 169x315 cell matrix of temperature values, below is the code I currently have (diffc is the data vector I am removing NaN values from, and I am moving said values to diffD; -999 represents NaN values; id contains ID values for cells I want to use in the calculation, which I already know to contain 25064 values):

counter = 1
diffD = array(0,25064)
id = array(0,25064)
for(i in 1:53235){
        if(diffc[i]!=-999){
                diffD[counter] = diffc[i]
                id[counter] = i
                counter = counter+1
        }
}               ##Isolates values I want to use in localG calculation

neigh = cell2nb(169,315,type=='queen')
neigh2 = subset.nb(neigh,(1:length(neigh) %in% id)
mylist = nb2listw(neigh2,style="B")
stats = localG(diffD,mylist)


Unfortunately, when I get to the last line of the code, I receive the following error:

"Error in matrix(0, nrow = nrow(x), ncol = ncol(x)) :
  invalid 'ncol' value (too large or NA)"


I can't figure out what it is referring to, as I have verified that there are no NA values and "ncol" should only be 1, as diffD and mylist are the same size (25064 data regions). My data works when I don't remove the cells with values of -999, however it returns some ridiculous Z-values (as expected). All I can think of is that I'm either using subset.nb() incorrectly or subset.nb() isn't returning a useable nb object in localG(). I'm basically trying to mimic ArcGIS' "Hot Spot Analysis" to locate cold and hot spots spatially in this code.

Thanks,
Dan
Reply | Threaded
Open this post in threaded view
|

Re: Subsetting NaN values in localG()

dab98
Still have not solved this problem with package 'spdep'.  It appears localG() cannot handle either SpatialPoints or a matrix of point coordinates that are not in a rectangular matrix when plotted. It cannot take NaN values and, after I successfully used dnearnigh() to create an "nb" object for ONLY Northeast land points and converted to a 'listw' object, it still returned the same error. See below ("points" is a 2-column matrix of lat-long point coordinates; diffD is my data matrix):

     > neigh2 = dnearneigh(points,0,5,longlat=TRUE)
     > mylist = nb2listw(neigh2,style="B")
     > stats = localG(diffD,mylist)
     Error in matrix(0, nrow = nrow(x), ncol = ncol(x)) :
       invalid 'ncol' value (too large or NA)


Again, if anyone thinks this analysis is incorrect, please let me know.  If not, I'll just abandon R for this portion of the project, as it is taking far too long to use this this package with Getis-Ord statistics, which should run fairly easily.
Reply | Threaded
Open this post in threaded view
|

Re: Subsetting NaN values in localG()

Roger Bivand
Writing to R-sig-geo would have been a good idea if you required speedier response. That response would have suggested that your code is not at all helpful, not using available classes in R for handling spatial data. You could have read your Arc GRID into a SpatialGridDataFrame, and made everything simpler. For an arbitrary longlat grid at 0.5 degrees spacing and a 100km neighbour threshold:

diffc <- rep(as.numeric(NA), 53235)
diffc[sample(53235, 25064, FALSE)] <- rnorm(25064)
summary(diffc)
library(sp)grd <- GridTopology(c(0.25, -79.75), c(0.5,0.5), c(169, 315))
SGDF <- SpatialGridDataFrame(grd, proj4string=CRS("+proj=longlat"), data=data.frame(diffc))
# SGDF could be read in from Arc with readGDAL() in rgdal
SPixDF <- as(SGDF, "SpatialPixelsDataFrame")
library(spdep)
dnb <- dnearneigh(SPixDF, 0, 100)
dnb
summary(SPixDF)
names(SPixDF)
lw <- nb2listw(dnb, style="B", zero.policy=TRUE)
out <- localG(SPixDF$diffc,  lw, zero.policy=TRUE)
summary(out)

As you see, none of your assertions about localG() or dnearneigh() hold in an unqualified fashion (localG stops on NAs, a quite logical design choice), you have simply not considered your data representation adequately. Did you read the Spatial Task View on CRAN carefully?

Further, so-called "hot-spots" are most likely a very poor idea, as the heat will be only in the eye of the beholder. You should be very careful with ad-hoc approaches of this kind.



dab98 wrote
Still have not solved this problem with package 'spdep'.  It appears localG() cannot handle either SpatialPoints or a matrix of point coordinates that are not in a rectangular matrix when plotted. It cannot take NaN values and, after I successfully used dnearnigh() to create an "nb" object for ONLY Northeast land points and converted to a 'listw' object, it still returned the same error. See below ("points" is a 2-column matrix of lat-long point coordinates; diffD is my data matrix):

     > neigh2 = dnearneigh(points,0,5,longlat=TRUE)
     > mylist = nb2listw(neigh2,style="B")
     > stats = localG(diffD,mylist)
     Error in matrix(0, nrow = nrow(x), ncol = ncol(x)) :
       invalid 'ncol' value (too large or NA)


Again, if anyone thinks this analysis is incorrect, please let me know.  If not, I'll just abandon R for this portion of the project, as it is taking far too long to use this this package with Getis-Ord statistics, which should run fairly easily.
Roger Bivand
Department of Economics
NHH Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway