knearneigh               package:spdep               R Documentation

_K _n_e_a_r_e_s_t _n_e_i_g_h_b_o_u_r_s _f_o_r _s_p_a_t_i_a_l _w_e_i_g_h_t_s

_D_e_s_c_r_i_p_t_i_o_n:

     The function returns a matrix with the indices of regions
     belonging to the set of the k nearest neighbours of each other. If
     longlat = TRUE, Great Circle distances are used.

_U_s_a_g_e:

     knearneigh(x, k=1, longlat = NULL)

_A_r_g_u_m_e_n_t_s:

       x: matrix of region point coordinates or a SpatialPoints object

       k: number of nearest neighbours to be returned

 longlat: TRUE if point coordinates are longitude-latitude decimal
          degrees, in which case distances are measured in kilometers;
          if x is a SpatialPoints object, the value is taken from the
          object itself

_D_e_t_a_i_l_s:

     The underlying C code is based on the 'knn' function in the class
     package in the VR bundle.

_V_a_l_u_e:

     A list of class 'knn' 

      nn: integer matrix of region number ids

      np: number of input points

       k: input required k

dimension: number of columns of x

       x: input coordinates

_A_u_t_h_o_r(_s):

     Roger Bivand Roger.Bivand@nhh.no

_S_e_e _A_l_s_o:

     'knn', 'dnearneigh', 'knn2nb'

_E_x_a_m_p_l_e_s:

     example(columbus)
     coords <- coordinates(columbus)
     col.knn <- knearneigh(coords, k=4)
     plot(columbus, border="grey")
     plot(knn2nb(col.knn), coords, add=TRUE)
     title(main="K nearest neighbours, k = 4")
     data(state)
     us48.fipsno <- read.geoda(system.file("etc/weights/us48.txt",
      package="spdep")[1])
     if (as.numeric(paste(version$major, version$minor, sep="")) < 19) {
      m50.48 <- match(us48.fipsno$"State.name", state.name)
     } else {
      m50.48 <- match(us48.fipsno$"State_name", state.name)
     }
     xy <- as.matrix(as.data.frame(state.center))[m50.48,]
     llk4.nb <- knn2nb(knearneigh(xy, k=4, longlat=FALSE))
     gck4.nb <- knn2nb(knearneigh(xy, k=4, longlat=TRUE))
     plot(llk4.nb, xy)
     plot(diffnb(llk4.nb, gck4.nb), xy, add=TRUE, col="red", lty=2)
     title(main="Differences between Euclidean and Great Circle k=4 neighbours")
     summary(llk4.nb, xy, longlat=TRUE)
     summary(gck4.nb, xy, longlat=TRUE)

     xy1 <- SpatialPoints((as.data.frame(state.center))[m50.48,],
       proj4string=CRS("+proj=longlat"))
     gck4a.nb <- knn2nb(knearneigh(xy1, k=4))
     summary(gck4a.nb, xy1)

