Announcement

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

  • question about tuples @Nick Cox

    @Nick Cox
    Dear Nick,now I have 5 variables ,x1 x2 x3 x4 x5.
    I want each tuple contains only one of x1 x2 x3 x4.
    Code:
    global m "x1 x2 x3 x4 x5"
    The reslut I want to get is
    x1,
    x2,
    x3,
    x4,
    x1 x5,
    x2 x5,
    x3 x5,
    x4 x5

    how can I set the conditional( ) option?
    I try to write the cond() like this below,
    Code:
    global m "x1 x2 x3 x4 x5"
    tuples $m,cond(!(1&2) !(2&3) !(1&4) !(1&3) !(2&4) !(3&4)) display
    
    tuple1: x5
    tuple2: x4
    tuple3: x3
    tuple4: x2
    tuple5: x1
    tuple6: x4 x5
    tuple7: x3 x5
    tuple8: x2 x5
    tuple9: x1 x5
    But this code is a little complicated.
    Do you have another easier method?

    Many thanks in advance.
    Best regards.

    Raymond Zhang
    Stata 17.0,MP

  • #2
    For this particular problem, I would not use tuples at all. I would simply

    Code:
    foreach x in x1 x2 x3 x4 {
        do_something_with `x'
        do_something_with `x' x5
    }

    If you insist on using tuples, alternatives incliude

    Code:
    tuples x1 x2 x3 x4 x5 , conditionals(!(1&(2|3|4)|2&(3|4)|3&4))
    is possible. I find your code easier to read.*


    * Also, in the current version of tuples, the results we obtain with my code differ between the Mata-based and the Python-based method. I am working on a fix (for the Mata-based method).

    Comment


    • #3
      dear Daniel, the code above is just an easiest example.what I want is
      Code:
       global m x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18
      I want each tuple contains only one of x1 x2 x3 x4,so I think the foreach command may not work.I have to use tuples.
      Best regards.

      Raymond Zhang
      Stata 17.0,MP

      Comment


      • #4
        Then, perhaps something along the lines

        Code:
        tuples x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18
        
        foreach x in x1 x2 x3 x4 {
            forvalues i = 1/`ntuples' {
                [i]do_something_with `tuple`i'' `x'
            }
        }
        I cannot think of a convenient (and better readble) way to set up the conditionals() option. Note that tuples has a max() option that might be useful for approaches that built on my suggestion.

        Comment


        • #5
          dear Daniel,thanks a lot for your help.
          it helps me a lot.
          Best regards.

          Raymond Zhang
          Stata 17.0,MP

          Comment

          Working...
          X