Distance matrix for Spatial Filtering {spdep}

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

Distance matrix for Spatial Filtering {spdep}

Erica Cseko Nolasco
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:

> ###creating the distance matrix

> library(fossil)

> setwd("G:/database/mapas/registros")

> coo=read.table("coordinates16.txt",sep=" ")

> coor1=as.data.frame(coo)

> length(coor1[,1]

[1] 4922

> matriz3=earth.dist(coor1, dist=T)

> 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*

>nb=knearneigh(coo,4921,longlat=T) ##second try to create nb, 4921 is the
required number for my project

*R session aborted. R encoutered a fatal error. The session was terminated*

>nb=knearneigh(coo,1000,longlat=T) ##second try to create nb

*R session aborted. R encoutered a fatal error. The session was terminated*

> nb=knearneigh(coo,500,longlat=T) ###tentativa de criar nb

*Error in knearneigh(coo, 500, longlat = T) : too many ties in knearneigh*

>nb=knearneigh(coo,400,longlat=T) #Large knn (5 elements,7.6Mb), I believe
I need the same number of elements as my distance matrix (12110581)

> nb2listw(nb,glist=matriz3,style="W")

*Error in nb2listw(nb, glist = matriz3, style = "W") : *
  *Not a neighbours list*

Any ideas would help a lot! Thanks in advance

*Erica Csekö Nolasco*
Mestranda em Modelagem em Ciências da Terra e do Ambiente
http://lattes.cnpq.br/2117508819823917
Laboratório de Ornitologia - Sala 03, LABIO
Universidade Estadual de Feira de Santana
Avenida Transnordestina s/n, Novo Horizonte
Feira de Santana - BA, Brasil CEP 44.036-900.
Tel/Fax: 55(75)3224-8295

Graduate Student in Modeling of Environmental and Earth Sciences
http://lattes.cnpq.br/2117508819823917
Ornithology Lab - 03 room, LABIO
Universidade Estadual de Feira de Santana
Transnordestina Ave, Novo Horizonte
Feira de Santana - BA, Brazil 44.036-900.
55(75)3224-8295

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Distance matrix for Spatial Filtering {spdep}

Roger Bivand
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*

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Roger Bivand
Department of Economics
NHH Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway