[R] Finding pairs with least magnitude difference from mean

rex.dwyer at syngenta.com rex.dwyer at syngenta.com
Tue Mar 1 20:11:45 CET 2011


No, that's not what I meant, but maybe I didn't understand the question.
What I suggested would involve sorting y, not x: "sort the *distances*".
If you want to minimize the sd of a subset of numbers, you sort the numbers and find a subset that is clumped together.
If the numbers are a function of pairs, you compute the function for all pairs of numbers, and find a subset that's clumped together.
Anyway, it's an idea, not a theorem, so proof is left as an exercise for the esteemed reader.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Hans W Borchers
Sent: Monday, February 28, 2011 2:17 PM
To: r-help at stat.math.ethz.ch
Subject: Re: [R] Finding pairs with least magnitude difference from mean

 <rex.dwyer <at> syngenta.com> writes:

> 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

You probably mean something like the following:

    x <- rnorm(10)
    y <- outer(x, x, "+") - (2 * mean(x))

    o <- order(x)
    sd(c(y[o[1],o[10]], y[o[2],o[9]], y[o[3],o[8]], y[o[4],o[7]], y[o[5],o[6]]))

This seems reasonable, though you would have to supply a more stringent
argument. I did two tests and it works alright.

--Hans Werner

______________________________________________
R-help at 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. 


More information about the R-help mailing list