[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Sum over all possible combinations

From   "Sebastian F. Büchte" <>
Subject   Re: st: Sum over all possible combinations
Date   Sat, 21 Jul 2007 09:47:58 +0200

On 7/21/07, J E <> wrote:
Dear statalisters

I am interested in computing the sum of the absolute value of all possible
differences within a group of my sample.

Suppose that the variable "group" denotes the variable indicating the group
to which an individual belongs and Y is an outcome variable. I am interested
in computing

sum of abs (Yi-Yj) for all i and j  belonging to group K

My programming skills are almost none. Can somebody help me out regarding an
efficient way of computing such statistic?

I think your problem may be solved by using -joinby-. I prepared a
little example to demonstrate the way I would do it: The result you
are asking for can then be found in the variable sum_dy

* -- begin example

// begin setting up an example dataset
set obs 10
gen group = _n
tempvar expand
set seed 1234567890
gen `expand' = round(uniform() * 10,1)
expand `expand'
gen y_i = uniform() * 10
// end setting up an example dataset

bys group: gen g_i=_n
tempfile join_file
ren y_i y_k
save `join_file', replace

joinby group using `join_file', unmatched(none)
bys group: egen sum_dy = sum(abs(y_i - y_k))

// cleaning up
bys group g_i: keep if _n==1
drop y_k g_i

* -- end example

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index