[R] Distance matrix for Spatial Filtering {spdep}
Roger Bivand
roger.bivand at nhh.no
Sun Nov 9 14:43:45 CET 2014
Erica Cseko Nolasco <ecnolasco <at> gmail.com> writes:
>
> Hi all,
>
> I´m working on a species distribution modeling, and I want to build
> eigenvectors to represent my spatial varuable. I´m trying to use
> SpatialFiltering(spdep) for it, but I´m having problems to create the nb
> object. My weights would be a truncated pairwise distance matrix that I was
> pretty able to build. However, I´m going out resources to create a nb
> object. Or it doesn´t match the matrix or the R-Studio crashes. Here is the
> code I´m trying:
You have not provided a reproducible example, it usually helps. Also always
run code that fails in R-Studio outside it, as this frees memory and aids
debugging (nothing between you and R). In addition, we know nothing of your
platform, I guess Windows (G: isn't common elsewhere). Your posting is HTML
which is discouraged.
>
....
> > matriz3=earth.dist(coor1, dist=T)
This function takes a matrix of geographical coordinates, and returns a
triangular matrix of distances (n*(n-1)), hence the length:
>
> > length(matriz3)
>
> [1] 12110581
>
> > ###truncating the distance matrix
>
> > matriz3[matriz3>=230]=230
> > ###creating the nb object to link to the distance matrix
>
> > library(spdep)
>
> >coord=read.table("coordinates16.txt",sep=" ", header=T)
>
> > coo=cbind(coord[,1],coord[,2])
>
> > length(coo[,1])
> [1] 4922
>
> >nb=dnearneigh(coo,0, 10000, longlat=T) ###large nb (4922 elements,92.9Mb)
>
> > nb2listw(nb,glist=matriz3,style="W")
>
> *Error in nb2listw(nb, glist = matriz3, style = "W") : glist wrong length*
>
The documentation of nb2listw says that the glist argument is a list, not a
triangular matrix. In addition, it is highly unlikely that you expect the
spatial autocorrelation to increase in distance. The examples for the
nb2listw function show how to construct truncated inverse distance weights
using km distances, here with a cutoff at 230km and using geographical
coordinates:
nb <- dnearneigh(coo, 0, 230, longlat=TRUE)
dists <- nbdists(nb, coo, longlat=TRUE)
glist <- lapply(dists, function(x) 1/x)
lw <- nb2listw(nb, glist, style="W")
The use of functions to create nb objects is described in vignette("nb") in
the spdep package.
...
>
> *R session aborted. R encoutered a fatal error. The session was terminated*
>
Such things have happened before with R-Studio as a front end when Windows
platforms have run out of memory, but do not repeat outside R-Studio
(reported but no action taken that I know of).
> > nb=knearneigh(coo,500,longlat=T) ###tentativa de criar nb
>
> *Error in knearneigh(coo, 500, longlat = T) : too many ties in knearneigh*
>
Almost certainly your choice of k=500 is erroneous, and also suggests that
you are flailing around without a clear grasp of what you should be doing.
1) until the problem is resolved, drop R-Studio; once resolved outside
R-Studio, you may go back to using it, but beware of memory problems.
2) construct the list of weights object as shown (maybe modify the threshold
if too many observations have no neighbour)
3) think carefully about the use of SpatialFiltering - is your response
continuous? You have about 5000 observations, so you will be operation on
5000x5000 matrices in order to construct the Moran eigenvectors, and will be
doing a brute force search for combinations of these eigenvectors to add to
your model. Are you sure that the model you are starting from is
well-specified (included variables and their functional forms)? If not, you
risk including many eigenvectors that simply mop up other misspecifications.
This search will be very time-consuming.
Consider using the R-sig-geo list, which may be more appropriate for this
kind of question.
Roger
...
>
> Any ideas would help a lot! Thanks in advance
>
> *Erica Csekö Nolasco*
More information about the R-help
mailing list