Statalist The Stata Listserver


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

st: RE: powerset of a list


From   Jesper Kjr Hansen <kjaer.hansen@oncable.dk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: powerset of a list
Date   Sat, 23 Sep 2006 15:37:15 +0200

Carole,

Carole Wilson wrote:
> 
> Does anyone know of stata code to generate a powerset of a 
> list in stata?
> 
> I will be using the elements of the subsets in different 
> calls to a routine.
> So, given {1, 2, 3}, I would like stata to return: {1}, {2}, {3}, {1,
> 2}, {1, 3}, {2, 3}, {1, 2, 3} (return of the null set is optional)
> possibly returning the subsets in a variable.
> In a simplified version of my code, I will be generating variables
> based on these subsets and their complements e.g.:
> s1=x1*(1-x2)*(1-x3)
> s2=x2*(1-x1)*(1-x3)
> s3=x3*(1-x1)*(1-x2)
> s4=x1*x2*(1-x3)
> s5=x1*x3*(1-x2)
> s6=x2*x3*(1-x1)
> s7=x1*x2*x3
> 
> I'll also be generating variables from subsets that contain 
> specific elements:
> r1=x1*(x1+x2)*(x1+x3)*(x1+x2+x3)  <==subsets that contain 1
> r2=x2*(x1+x2)*(x2+x3)*(x1+x2+x3)  <==subsets that contain 2
> r3=x3*(x1+x3)*(x2+x3)*(x1+x2+x3)  <==subsets that contain 3
> 
> My goal, of course, is to generalize these operations to n 
> elements of a list
> 
> Thank you,
> Carole Wilson

Maybe you can adapt the program _ffdtcomb, which I made a while
ago, for your needs?

 type http://www.nat.sdu.dk/users/nat-sdu/jkha01/ffdt/_ffdtcomb.ado

It takes a string of elements, e.g. a varlist, and returns all
possible combinations of the elements. The combinations are returned
in r().

The syntax is:

 _ffdtcomb , n(integer) Elist(str) [k(integer 0)]

Example:

. _ffdtcomb , n(3) elist(Hello cruel world)

. return list

macros:
              r(cmax) : "7"
                r(v1) : "Hello"
                r(v2) : "cruel"
                r(v3) : "world"
                r(v4) : "Hello cruel"
                r(v5) : "Hello world"
                r(v6) : "cruel world"
                r(v7) : "Hello cruel world"


-- Jesper K. Hansen

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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