[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
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:
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
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:
 "listw" "nb"
## and contains...:
 "neighbours" "style" "weights"
How should I do this? Is this even the right way to proceed?
Thanks all in advance!!
[[alternative HTML version deleted]]
More information about the R-help