Skip to content

Nearest Neighbors

6 messages · olemissrebs1123, Jeff Newmiller, David L Carlson +1 more

#
I was wondering if there is a way in R to find k nearest neighbors of various
orders, say order 2, 3, or 4. In otherwords neighbors of neighbors of
neighbors. You get the idea. I know that I can use knearneigh(matrix.data,
k) but this only gives me the k nearest neighbors and not of a particular
order. 


Thanks in advance. 



--
View this message in context: http://r.789695.n4.nabble.com/Nearest-Neighbors-tp4637618.html
Sent from the R help mailing list archive at Nabble.com.
#
Isn't the obvious solution to subtract the set for level n-1 from the set for level n?
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
olemissrebs1123 <adrian.wilsonphd at gmail.com> wrote:

            
#
Look at function get.knn in package FNN:
knns is a list with two components, a matrix of indices to the nearest
neighbors and a matrix of distances to the nearest neighbors.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
4 days later
#
On Tue, Jul 24, 2012 at 09:26:49AM -0700, olemissrebs1123 wrote:
Hi.

If i understand correctly, then this may be achieved by several iterations.
In the first iteration, the nearest neighbours are found. The next iteration
starts from them a finds their nearest neighbours. These are neighbours of
order 2. A general iteration finds neighbours of order n by looking for neighbours
of the points found in the previous step, which are the neighours of order n-1.

Hope this helps.

Petr Savicky.
#
Actually if you are looking for neighbors of neighbors you only need the
nearest neighbor for each point. The problem is that the nearest neighbor of
the nearest neighbor of point 1 is often point 1. Did you want the nearest
neighbor not counting any point twice? Sounds more like a traveling salesman
problem or nearest link cluster analysis.
nn2 nn3 nn4
 [1,] 53   1  53   1
 [2,] 34   2  34   2
 [3,] 67   3  67   3
 [4,] 35  60  46  60
 [5,] 82   5  82   5
 [6,] 10   6  10   6
 [7,] 48   7  48   7
 [8,] 40   8  40   8
 [9,] 25   9  25   9
[10,]  6  10   6  10

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352