James,
It seems the 2*mean(x) term is irrelevant if you are seeking to minimize sd.  
Then you want to sort the distances from smallest to largest.  Then it seems 
clear that your five values will be adjacent in the list, since if you have a 
set of five adjacent values, exchanging any of them for one further away in the 
list will increase the sd.  The only problem I see with this is that you can't 
use a number more than once.  In any case, you need to compute the best five 
pairs beginning at position i in the sorted list, for 1<=i<=choose(n,2), then 
take the max over all i.
There no R in my answer such as you'd notice, but I hope it helps just the same.
Rex

-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Hans W Borchers
Sent: Saturday, February 26, 2011 6:43 AM
To: r-h...@stat.math.ethz.ch
Subject: Re: [R] Finding pairs with least magnitude difference from mean

> 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
>

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




message may contain confidential information. If you are not the designated 
recipient, please notify the sender immediately, and delete the original and 
any copies. Any use of the message by you is prohibited. 
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to