Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate variable names with all permutations of a list

    Hi,

    I need to generate string variables whose names are the permutations of a list. For example, if the list was a b c, then the variable names would be a b c ab ba ac ca bc cb abc acb and so on. I don't think I can do it with tuples or permin. Any thoughts?

  • #2
    When you say names, do you mean values? So you want to start out with a variable whose values in three observations were "a", "b", "c" and get a dataset like that?

    That's possible with tuples (SSC, as you are asked to explain):

    Code:
    clear
    
    tuples a b c, max(3)
    
    set obs `ntuples'
    
    gen values = ""
    
    qui forval i = 1/`ntuples' {
           replace values = "`tuple`i''" in `i' 
    }
    
    list
    
         +--------+
         | values |
         |--------|
      1. |      c |
      2. |      b |
      3. |      a |
      4. |    b c |
      5. |    a c |
         |--------|
      6. |    a b |
      7. |  a b c |
         +--------+
    For a more complicated example, try feeding the results of levelsof as the argument of tuples.
    .
    Last edited by Nick Cox; 15 Oct 2014, 16:54.

    Comment


    • #3
      Sean, I think you would probably get more helpful advice if you gave more details about your problem. If you haven't already done so, please read the FAQ for some helpful advice on how to ask questions in ways that help us help you.

      From my reading you want all permutations of your list, not all combinations (which is what tuples gets you).

      It's not clear to me, though, why permin won't work for you. (permin is part of the percom package from http://fmwww.bc.edu/RePEc/bocode/p)
      My guess is that it's because you have more than 5 items in your list but you should make details like that clear.

      Permin will give you observations containing all the permutations (assuming your list is 5 items or less). You would need to do some extra work to turn those values into variable names. Since you don't provide any information about what those variables would contain it's hard to give you details about what steps you would take next.

      Comment


      • #4
        Correct, I need permutations rather than combinations. I also need variable names, not values. I'll try to provide more detail, but the first post really says it. I will have a list with roughly 12 single letters (A a B b C c D d...). Those letters are not values in a current variable, though if that's required I can certainly create a variable with those letters as values. I need a variable that is named for each one of the permutations of that list, including single letters, two letters, three letters...12 letters. Each of those variables will eventually be a string.

        Why I need this: I'm using a Stata package called fuzzy, which stores results as clusters of single-letters (for example "AbC aCd BCD"). I'm running a simulation which collects thousands of such sets. I need to know how many times each permutation was represented in the thousands of iterations of fuzzy. I can do this, but right now I'm forced to manually generate each variable and that is tedious and error prone.

        Comment


        • #5
          Hi Sean,

          You might try cvpermute() in Mata.

          For example you could alter the following to suit your needs:

          Code:
          mata:
          
          info = cvpermutesetup((1::4)) /*sets up a column vector to be permuted which is the same length as the number of letters (in this toy example, 4)*/
          
          letter_vec = ("A" \ "a" \ "B" \ "b")  /*where the entries to be permuted would go*/
          
          for (x = 1; x <= factorial(4); x++) { /*change the factorial to be the number of entries in letter_vec*/
          
          if (x==1) permutation_vec = invtokens(letter_vec[cvpermute(info)]') /*syntax to collect the letter permutations into a single entry of a vector*/
          
          else permutation_vec = (permutation_vec \ invtokens(letter_vec[cvpermute(info)]'))
          
          }
          
          permutation_vec /*displays the resulting vector*/
          
          end
          The above code produces (in Mata) a vector including all permutations of the entries of "letter_vec". This code could be adapted to however many letters you actually need and then imported into Stata.

          - joe
          Joseph Nicholas Luchman, Ph.D., PStatĀ® (American Statistical Association)
          ----
          Research Fellow
          Fors Marsh

          ----
          Version 18.0 MP

          Comment

          Working...
          X