+ Reply to Thread
Results 1 to 7 of 7

K-Nearest Neighbor Imputation

  1. #1
    Registered User
    Join Date
    01-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    30

    K-Nearest Neighbor Imputation

    Hi, What I am looking to do is find the 10 (or something set by a variable) nearest neighbors to every empty cell within a range, where the neighbors are in the same column or row (dictated by another variable), and then average those 10 nearest neighbors and insert the value into the original cell.


    Thanks for your help!

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,614

    Re: K-Nearest Neighbor Imputation

    Can you upload example workbook with your inputs and desired output?

  3. #3
    Registered User
    Join Date
    01-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    30

    Re: K-Nearest Neighbor Imputation

    Yep, attached!
    Edit: I should note that because I threw this together all the values are the same and it is a little more confusing. I averaged the ten nearest neighbors in the same column to each blank and inserted that number.
    Attached Files Attached Files
    Last edited by tucanj; 01-12-2013 at 04:20 PM.

  4. #4
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,614

    Re: K-Nearest Neighbor Imputation

    Try run this macro:
    • It will promt for number of neighbours
    • It will promt by rows or columns
    • Desire result is with blue font
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    01-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    30

    Re: K-Nearest Neighbor Imputation

    1
    Quote Originally Posted by zbor View Post
    Try run this macro:
    • It will promt for number of neighbours
    • It will promt by rows or columns
    • Desire result is with blue font
    Thanks for your help! This was great. There were a couple things that could be fixed, which I would appreciate help because I don't fully understand your code:
    a) when calculating the average of the 10 nearest neighbors, I would like it to ignore blank cells and cells without integers.
    b) I think it's restricted to a range of A1:Z100? Because when I run it on a large dataset (22000 rows, A:GH columns) it does not work. It would be great if it could work on all rows and columns.
    c) It seems to me that when calculating the nearest cell for an odd number of neighbors it will take the lowest value. For example, if dNum=1, it will take the closest neighbor with the lowest value. Is it possible to make it select either one of the nearest neighbors at random?

    Thanks again!

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,614

    Re: K-Nearest Neighbor Imputation

    a) For 3 neighbours my code will look:
    Please Login or Register  to view this content.
    For blank1 it will return 3,75 (2 + blank2 + 5 = 7 and average 3.5). It won't look 2 + 5 + 2 (far away third value) and result 3.
    I'm affraid this is over my skills to give you second solution.

    b) Just set range to
    Please Login or Register  to view this content.
    c) yes. It define range from the left, so for one value it will take left (or upper number). I will need more time to make it random. Hopefully tomorrow.
    Or if someone will take care of a) problem he can also solve c)

  7. #7
    Registered User
    Join Date
    01-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2013
    Posts
    30

    Re: K-Nearest Neighbor Imputation

    Ok thanks for your help. If you figure out a solution to a) I'm happy to hear

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1