[R] Finding pairs with least magnitude difference from mean

Hans W Borchers hwborchers at googlemail.com
Sat Feb 26 12:43:23 CET 2011


> I have what I think is some kind of linear programming question.
> Basically, what I want to figure out is if I have a vector of numbers,
> 
> > x <- rnorm(10)
> > x
>  [1] -0.44305959 -0.26707077  0.07121266  0.44123714 -1.10323616
> -0.19712807  0.20679494 -0.98629992  0.97191659 -0.77561593
> 
> > mean(x)
> [1] -0.2081249
> 
> Using each number only once, I want to find the set of five pairs
> where the magnitude of the differences between the mean(x) and each
> pairs sum is least.
> 
> > y <- outer(x, x, "+") - (2 * mean(x))
> 
> With this matrix, if I put together a combination of pairs which uses
> each number only once, the sum of the corresponding numbers is 0.
> 
> For example, compare the SD between this set of 5 pairs
> > sd(c(y[10,1], y[9,2], y[8,3], y[7,4], y[6,5]))
> [1] 1.007960
> 
> versus this hand-selected, possibly lowest SD combination of pairs
> > sd(c(y[3,1], y[6,2], y[10,4], y[9,5], y[8,7]))
> [1] 0.2367030

Your selection is not bad, as only about 0.4% of all possible distinct
combinations have a smaller value -- the minimum is 0.1770076, for example
[10 7 9 5 8 4 6 2 3 1].

(1) combinat() from the 'combinations' package seems slow, try instead the
    permutations() function from 'e1071'. 

(2) Yes, except your vector is getting much larger in which case brute force
    is no longer feasible.

(3) This is not a linear programming, but a combinatorial optimization task.
    You could try optim() with the SANN method, or some mixed-integer linear
    program (e.g., lpSolve, Rglpk, Rsymphony) by intelligently using binary
    variables to define the sets.

This does not mean that some specialized approach might not be more
appropriate.

--Hans Werner

> I believe that if I could test all the various five pair combinations,
> the combination with the lowest SD of values from the table would give
> me my answer.  I believe I have 3 questions regarding my problem.
> 
> 1) How can I find all the 5 pair combinations of my 10 numbers so that
> I can perform a brute force test of each set of combinations?  I
> believe there are 45 different pairs (i.e. choose(10,2)). I found
> combinations from the {Combinations} package but I can't figure out
> how to get it to provide pairs.
> 
> 2) Will my brute force strategy of testing the SD of each of these 5
> pair combinations actually give me the answer I'm searching for?
> 
> 3) Is there a better way of doing this?  Probably something to do with
> real linear programming, rather than this method I've concocted.
> 
> Thanks for any help you can provide regarding my question.
> 
> Best regards,
> 
> James
>



More information about the R-help mailing list