k-nearest neighbours from distance matrix in spdep

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

k-nearest neighbours from distance matrix in spdep

Juan Antonio Rodriguez Perez
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: k-nearest neighbours from distance matrix in spdep

Juan Antonio Rodriguez Perez
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: [hidden email]
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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.