Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Making permutations of dyads

 From Robert Picard To "statalist@hsphsun2.harvard.edu" Subject Re: st: Making permutations of dyads Date Thu, 19 Dec 2013 17:50:31 -0500

```Here are a few more ways to do this. Scott's solution is equivalent to
using -joinby-.

For time bands (I assume this means a running window of 5 minutes),
then -cross- can be used to create all pairwise combinations and drop
those that do not fall within the time constraint. This is a brute
force approach that may quickly become unmanageable if there are too
many observations.

If there are only a few dyads expected to fall within the window of
time, then a loop approach may be more efficient.

Robert

* --------------------------- begin example -------------
clear
input id   str4 time  block
1     "8:00"  1
2     "8:02"  1
3     "8:02"  1
4     "8:04"  1
5     "8:06"  2
6     "8:07"  2
7     "8:09"  2
end

* to be able to do time arithmetic using milliseconds
gen double dtime = clock("2013.12.19"+time,"YMDhm")
format %tc dtime
tempfile f
save "`f'"

* the -joinby- approach
rename id id2
joinby block using "`f'"
keep if id < id2
sort id id2
list id id2, noobs sepby(id)

* the -cross- approach with "time bands"
use "`f'", clear
rename * *2
cross using "`f'"
keep if id < id2 & inrange(dtime2-dtime,0,5*60000-1)
sort id id2
list id id2, noobs sepby(id)

* loop over forward observations until no more cases
* are found within the window of time.
use "`f'", clear
isid dtime id, sort
local i 0
local more 1
while `more' {
local ++i
gen did`i' = id[_n+`i'] if ///
inrange(dtime[_n+`i']-dtime,0,5*60000-1)
count if !mi(did`i')
local more = r(N)
}
reshape long did, i(id) j(n)
drop if mi(did)
sort id did
list id did, noobs sepby(id)
* --------------------------- end example ----------------

On Thu, Dec 19, 2013 at 4:17 PM, Scott Merryman
<scott.merryman@gmail.com> wrote:
> info).
>
>  clear
> input id   str4 time  block
>  1     "8:00"  1
>  2     "8:02"  1
>  3     "8:02"  1
>  4     "8:04"  1
>  5     "8:06"  2
>  6     "8:07"  2
>  7     "8:09"  2
>  end
> bys block: generate dim = _N
> expand dim
> sort block id
> by block id: generate column = _n
> by block :generate id_d = id[_n+column*dim]
> drop if mi(id_d)
> drop dim column
> gen dyad = string(id)+ string(id_d)
> order id id_d
> l
>
>
> Scott
>
>
> On Thu, Dec 19, 2013 at 2:10 PM, Douglas Levy
> <douglas_levy@post.harvard.edu> wrote:
>> Actually, to correct my initial post, I'd be interested in
>> *combinations* not *permutations*. So to restate and revise:
>> ID    time  5m_block
>> 1     8:00  1
>> 2     8:02  1
>> 3     8:02  1
>> 4     8:04  1
>> 5     8:06  2
>> 6     8:07  2
>> 7     8:09  2
>>
>> For 5 minute time blocks,  I'd want to output a dataset that looks like this:
>> 12
>> 13
>> 14
>> 23
>> 24
>> 34
>> 56
>> 57
>> 67
>>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```