2

I am using the nn2() function within the RANN package to find nearest neighbors, within a specific radius, of points identified by latitudes and longitudes.

What unit is that radius measured in?

I have looked in the RANN and ANN documentation but have not found the answer. An example using a subset of my coordinates in copied below.

------------------------ Example ------------------------------

    library("RANN")
    > #Data contains longitude and latitudes of points
    >   long<-c(-0.12541369, -0.06148541, -0.10533759, -0.06067756, -0.13908847)
    >   lat<-c(51.51735, 51.49540, 51.53120, 51.54869, 51.51602)
    >   
    >   data<-cbind(long,lat)
    >   
    >   #Setting radius
    >   #Here is my question -- what is the unit of radius?
    >   radius<-.05
    >   
    >   #Identifying neighbors
    >   res <- nn2(data, k=nrow(data), searchtype="radius", radius = radius)
    >   
    >   #Counts of neighbors
    >   count <- cbind(data,rowSums(res$nn.idx > 0) - 1)
    >   
    >   #View output
    >   colnames(count)[3]<-"nn_count"
    >   head(count)
        long      lat nn_count
    [1,] -0.12541369 51.51735        2
    [2,] -0.06148541 51.49540        0
    [3,] -0.10533759 51.53120        3
    [4,] -0.06067756 51.54869        1
    [5,] -0.13908847 51.51602        2
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
JKF123
  • 21
  • 3

1 Answers1

3

Not sure if you still need that answer, but others using nn2 might appreciate this:

The radius is the distance, based on the decimal, between your lon/lat coordinates. Chetan S. was kind enough to summarize the distances per decimal for coordinates here! Below the text you can find the extracted table.

You specified r <- 0.05 and looking at the table we can see that values at the second decimal specify a location with an accuracy of up to 1.11km. In you specific case this means you were looking for neighboring observations within a radius of 5 x 1.11km = 5.55km.

You can verify this by using the distHaversine from the geosphere package to calculate the distance to the point that is the furthest away from your center - if you don't specify the maximum number of nearest neighbors to compute you should find that this distance is approximately 5.55km.

decimal places degrees distance ------- ------- -------- 0 1 111 km 1 0.1 11.1 km 2 0.01 1.11 km 3 0.001 111 m 4 0.0001 11.1 m 5 0.00001 1.11 m 6 0.000001 11.1 cm 7 0.0000001 1.11 cm 8 0.00000001 1.11 mm

jbukoski
  • 1,297
  • 1
  • 13
  • 26
Jonathan
  • 31
  • 2