[R] k-nearest neighbours from distance matrix in spdep

Juan Antonio Rodriguez Perez ju@n@rodr|guez @end|ng |rom cn@g@crg@eu
Mon Feb 10 14:28:04 CET 2020


Dear all,

I got a message from the spdep package developer and suggested me to open a GitHub issue for the problem. Please follow the discussion at:


https://github.com/r-spatial/spdep/issues/38


Cheers,

J


________________________________
De: Juan Antonio Rodriguez Perez
Enviado: venres 07 febreiro 2020 17:01:25
Para: r-help using r-project.org
Asunto: k-nearest neighbours from distance matrix in spdep


Dear all,


I am using the spdep package to compute Local Moran Index.

My problem is that I am using 3D coordinates (x,y,z), and I would like to compute the k-nearest neighbours (k=10) for each point in my 3D space. I have already done this in 2D, by doing the following:


>neighs_k <- knn2nb(knearneigh(as.matrix(full),
                                        k = 10))
> neighs_mat_k <- nb2listw(neighs_k
                         style = "W",
                         zero.policy = TRUE)

And then I can easily proceed using the neighs_mat_k object.

However, when using x,y,z coordinates I can't run the knearneigh() function on it. I tried converting my data to a distance matrix and using mat2listw() function like this:

>D <- as.matrix(dist(full, diag=FALSE, upper=FALSE))
>test1 <- mat2listw(D)

...but now I don't know how to retrieve the k-nearest weights from my test1 object (which would correspond to k-nearest neighbours) without changing the class of test1, which is:

> class(test1)
[1] "listw" "nb"
## and contains...:
> ls(test1)
[1] "neighbours" "style"      "weights"


How should I do this? Is this even the right way to proceed?

Thanks all in advance!!
Best wishes,

Juan

	[[alternative HTML version deleted]]



More information about the R-help mailing list