[R] k-nearest neighbours from distance matrix in spdep
Juan Antonio Rodriguez Perez
ju@n@rodr|guez @end|ng |rom cn@g@crg@eu
Fri Feb 7 17:01:25 CET 2020
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