Announcement

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

  • How to use loops to establish the following combinations

    Hi every one,

    Is there anybody know how to establish the following combination by the loops? Each combination will generate a new variable. Thanks

    Best regards
    Chang

    Click image for larger version

Name:	Figure.PNG
Views:	1
Size:	39.3 KB
ID:	1594411





  • #2
    Your question really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you. Show example data. Tell us how precisely you want to create the new variables if you were not using loops. The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output.

    Comment


    • #3
      Thanks William,

      We have a p*1 matrix, with the initial numer as (0, 0, 0)T. Now each time we add 1 to one the elements, untill all three elements are 3. For clarify it more clear, I draft the Figure below. Is there any method to establish the following iteration automatically? Thanks again.

      Best regards
      Chang


      Click image for larger version

Name:	1.png
Views:	1
Size:	4.6 KB
ID:	1594523

      Comment


      • #4
        The updated combinations.
        Last edited by Chang Xu; 22 Feb 2021, 22:14.

        Comment


        • #5
          The updated combinations.

          Click image for larger version

Name:	2.jpg
Views:	1
Size:	90.6 KB
ID:	1594602

          Comment


          • #6
            Your goal remains unclear. In post #1 you implied you wanted to create a new variable for each combination of A, B, and C. The following example code generates all 64 of the ordered 3-tuples (a,b,c) and creates a new variable with a name that reflects the 3-tuple. If it is not what you want, explain clearly how it differs from what you want.
            Code:
            . forvalues a=0/3 {
              2. forvalues b=0/3 {
              3. forvalues c=0/3 {
              4.     generate x`a'`b'`c' = 1
              5. }
              6. }
              7. }
            
            . describe, simple
            x000  x010  x020  x030  x100  x110  x120  x130  x200  x210  x220  x230  x300  x310  x320  x330
            x001  x011  x021  x031  x101  x111  x121  x131  x201  x211  x221  x231  x301  x311  x321  x331
            x002  x012  x022  x032  x102  x112  x122  x132  x202  x212  x222  x232  x302  x312  x322  x332
            x003  x013  x023  x033  x103  x113  x123  x133  x203  x213  x223  x233  x303  x313  x323  x333
            
            .

            Comment


            • #7
              Originally posted by William Lisowski View Post
              Your goal remains unclear. In post #1 you implied you wanted to create a new variable for each combination of A, B, and C. The following example code generates all 64 of the ordered 3-tuples (a,b,c) and creates a new variable with a name that reflects the 3-tuple. If it is not what you want, explain clearly how it differs from what you want.
              Code:
              . forvalues a=0/3 {
              2. forvalues b=0/3 {
              3. forvalues c=0/3 {
              4. generate x`a'`b'`c' = 1
              5. }
              6. }
              7. }
              
              . describe, simple
              x000 x010 x020 x030 x100 x110 x120 x130 x200 x210 x220 x230 x300 x310 x320 x330
              x001 x011 x021 x031 x101 x111 x121 x131 x201 x211 x221 x231 x301 x311 x321 x331
              x002 x012 x022 x032 x102 x112 x122 x132 x202 x212 x222 x232 x302 x312 x322 x332
              x003 x013 x023 x033 x103 x113 x123 x133 x203 x213 x223 x233 x303 x313 x323 x333
              
              .
              Thanks William. The code you provided is exactly what I needed. Thanks so much.

              Best regards
              Chang

              Comment


              • #8
                Originally posted by Chang Xu View Post

                Thanks William. The code you provided is exactly what I needed. Thanks so much.

                Best regards
                Chang
                Can we define "a", "b", "c" as number 1, 2, 3, ...k. And for each k we take 0/3 in the loops?

                Say, some thing like below:

                Code:
                forvalues a=0/3 {
                 forvalues b=0/3 {
                 forvalues c=0/3 {
                  forvalues d=0/3 {
                  .    
                  .                    
                  .
                 
                 generate x`a'`b'`c'`...' = 1
                 }
                 }
                 }
                
                . describe, simple

                Thanks
                Best regards
                Chang
                Last edited by Chang Xu; 23 Feb 2021, 10:50.

                Comment


                • #9
                  Thanks.

                  Comment


                  • #10
                    This will work for k from 1 to 9 and can trivially be extended to more,
                    Code:
                    local K 4
                    forvalues k=1/9 {
                        local v`k' !
                        if `k'<=`K' local v`k' 0 1 2 3
                    }
                    foreach a1 in `v1' {
                    foreach a2 in `v2' {
                    foreach a3 in `v3' {
                    foreach a4 in `v4' {
                    foreach a5 in `v5' {
                    foreach a6 in `v6' {
                    foreach a7 in `v7' {
                    foreach a8 in `v8' {
                    foreach a9 in `v9' {
                    local foo x`a1'`a2'`a3'`a4'`a5'`a6'`a7'`a8'`a9'
                    local foo : subinstr local foo "!" "", all
                    generate `foo' = 1
                    }
                    }
                    }
                    }
                    }
                    }
                    }
                    }
                    }
                    Code:
                    . describe, simple
                    x0000  x0103  x0212  x0321  x1030  x1133  x1302  x2011  x2120  x2223  x2332  x3101  x3210  x3313
                    x0001  x0110  x0213  x0322  x1031  x1200  x1303  x2012  x2121  x2230  x2333  x3102  x3211  x3320
                    x0002  x0111  x0220  x0323  x1032  x1201  x1310  x2013  x2122  x2231  x3000  x3103  x3212  x3321
                    x0003  x0112  x0221  x0330  x1033  x1202  x1311  x2020  x2123  x2232  x3001  x3110  x3213  x3322
                    x0010  x0113  x0222  x0331  x1100  x1203  x1312  x2021  x2130  x2233  x3002  x3111  x3220  x3323
                    x0011  x0120  x0223  x0332  x1101  x1210  x1313  x2022  x2131  x2300  x3003  x3112  x3221  x3330
                    x0012  x0121  x0230  x0333  x1102  x1211  x1320  x2023  x2132  x2301  x3010  x3113  x3222  x3331
                    x0013  x0122  x0231  x1000  x1103  x1212  x1321  x2030  x2133  x2302  x3011  x3120  x3223  x3332
                    x0020  x0123  x0232  x1001  x1110  x1213  x1322  x2031  x2200  x2303  x3012  x3121  x3230  x3333
                    x0021  x0130  x0233  x1002  x1111  x1220  x1323  x2032  x2201  x2310  x3013  x3122  x3231
                    x0022  x0131  x0300  x1003  x1112  x1221  x1330  x2033  x2202  x2311  x3020  x3123  x3232
                    x0023  x0132  x0301  x1010  x1113  x1222  x1331  x2100  x2203  x2312  x3021  x3130  x3233
                    x0030  x0133  x0302  x1011  x1120  x1223  x1332  x2101  x2210  x2313  x3022  x3131  x3300
                    x0031  x0200  x0303  x1012  x1121  x1230  x1333  x2102  x2211  x2320  x3023  x3132  x3301
                    x0032  x0201  x0310  x1013  x1122  x1231  x2000  x2103  x2212  x2321  x3030  x3133  x3302
                    x0033  x0202  x0311  x1020  x1123  x1232  x2001  x2110  x2213  x2322  x3031  x3200  x3303
                    x0100  x0203  x0312  x1021  x1130  x1233  x2002  x2111  x2220  x2323  x3032  x3201  x3310
                    x0101  x0210  x0313  x1022  x1131  x1300  x2003  x2112  x2221  x2330  x3033  x3202  x3311
                    x0102  x0211  x0320  x1023  x1132  x1301  x2010  x2113  x2222  x2331  x3100  x3203  x3312

                    Comment


                    • #11
                      Originally posted by William Lisowski View Post
                      This will work for k from 1 to 9 and can trivially be extended to more,
                      Code:
                      local K 4
                      forvalues k=1/9 {
                      local v`k' !
                      if `k'<=`K' local v`k' 0 1 2 3
                      }
                      foreach a1 in `v1' {
                      foreach a2 in `v2' {
                      foreach a3 in `v3' {
                      foreach a4 in `v4' {
                      foreach a5 in `v5' {
                      foreach a6 in `v6' {
                      foreach a7 in `v7' {
                      foreach a8 in `v8' {
                      foreach a9 in `v9' {
                      local foo x`a1'`a2'`a3'`a4'`a5'`a6'`a7'`a8'`a9'
                      local foo : subinstr local foo "!" "", all
                      generate `foo' = 1
                      }
                      }
                      }
                      }
                      }
                      }
                      }
                      }
                      }
                      Code:
                      . describe, simple
                      x0000 x0103 x0212 x0321 x1030 x1133 x1302 x2011 x2120 x2223 x2332 x3101 x3210 x3313
                      x0001 x0110 x0213 x0322 x1031 x1200 x1303 x2012 x2121 x2230 x2333 x3102 x3211 x3320
                      x0002 x0111 x0220 x0323 x1032 x1201 x1310 x2013 x2122 x2231 x3000 x3103 x3212 x3321
                      x0003 x0112 x0221 x0330 x1033 x1202 x1311 x2020 x2123 x2232 x3001 x3110 x3213 x3322
                      x0010 x0113 x0222 x0331 x1100 x1203 x1312 x2021 x2130 x2233 x3002 x3111 x3220 x3323
                      x0011 x0120 x0223 x0332 x1101 x1210 x1313 x2022 x2131 x2300 x3003 x3112 x3221 x3330
                      x0012 x0121 x0230 x0333 x1102 x1211 x1320 x2023 x2132 x2301 x3010 x3113 x3222 x3331
                      x0013 x0122 x0231 x1000 x1103 x1212 x1321 x2030 x2133 x2302 x3011 x3120 x3223 x3332
                      x0020 x0123 x0232 x1001 x1110 x1213 x1322 x2031 x2200 x2303 x3012 x3121 x3230 x3333
                      x0021 x0130 x0233 x1002 x1111 x1220 x1323 x2032 x2201 x2310 x3013 x3122 x3231
                      x0022 x0131 x0300 x1003 x1112 x1221 x1330 x2033 x2202 x2311 x3020 x3123 x3232
                      x0023 x0132 x0301 x1010 x1113 x1222 x1331 x2100 x2203 x2312 x3021 x3130 x3233
                      x0030 x0133 x0302 x1011 x1120 x1223 x1332 x2101 x2210 x2313 x3022 x3131 x3300
                      x0031 x0200 x0303 x1012 x1121 x1230 x1333 x2102 x2211 x2320 x3023 x3132 x3301
                      x0032 x0201 x0310 x1013 x1122 x1231 x2000 x2103 x2212 x2321 x3030 x3133 x3302
                      x0033 x0202 x0311 x1020 x1123 x1232 x2001 x2110 x2213 x2322 x3031 x3200 x3303
                      x0100 x0203 x0312 x1021 x1130 x1233 x2002 x2111 x2220 x2323 x3032 x3201 x3310
                      x0101 x0210 x0313 x1022 x1131 x1300 x2003 x2112 x2221 x2330 x3033 x3202 x3311
                      x0102 x0211 x0320 x1023 x1132 x1301 x2010 x2113 x2222 x2331 x3100 x3203 x3312
                      Okay, thanks William. It helps a lot!

                      Best regards
                      Chang

                      Comment


                      • #12
                        Originally posted by William Lisowski View Post
                        This will work for k from 1 to 9 and can trivially be extended to more,
                        Code:
                        local K 4
                        forvalues k=1/9 {
                        local v`k' !
                        if `k'<=`K' local v`k' 0 1 2 3
                        }
                        foreach a1 in `v1' {
                        foreach a2 in `v2' {
                        foreach a3 in `v3' {
                        foreach a4 in `v4' {
                        foreach a5 in `v5' {
                        foreach a6 in `v6' {
                        foreach a7 in `v7' {
                        foreach a8 in `v8' {
                        foreach a9 in `v9' {
                        local foo x`a1'`a2'`a3'`a4'`a5'`a6'`a7'`a8'`a9'
                        local foo : subinstr local foo "!" "", all
                        generate `foo' = 1
                        }
                        }
                        }
                        }
                        }
                        }
                        }
                        }
                        }
                        Code:
                        . describe, simple
                        x0000 x0103 x0212 x0321 x1030 x1133 x1302 x2011 x2120 x2223 x2332 x3101 x3210 x3313
                        x0001 x0110 x0213 x0322 x1031 x1200 x1303 x2012 x2121 x2230 x2333 x3102 x3211 x3320
                        x0002 x0111 x0220 x0323 x1032 x1201 x1310 x2013 x2122 x2231 x3000 x3103 x3212 x3321
                        x0003 x0112 x0221 x0330 x1033 x1202 x1311 x2020 x2123 x2232 x3001 x3110 x3213 x3322
                        x0010 x0113 x0222 x0331 x1100 x1203 x1312 x2021 x2130 x2233 x3002 x3111 x3220 x3323
                        x0011 x0120 x0223 x0332 x1101 x1210 x1313 x2022 x2131 x2300 x3003 x3112 x3221 x3330
                        x0012 x0121 x0230 x0333 x1102 x1211 x1320 x2023 x2132 x2301 x3010 x3113 x3222 x3331
                        x0013 x0122 x0231 x1000 x1103 x1212 x1321 x2030 x2133 x2302 x3011 x3120 x3223 x3332
                        x0020 x0123 x0232 x1001 x1110 x1213 x1322 x2031 x2200 x2303 x3012 x3121 x3230 x3333
                        x0021 x0130 x0233 x1002 x1111 x1220 x1323 x2032 x2201 x2310 x3013 x3122 x3231
                        x0022 x0131 x0300 x1003 x1112 x1221 x1330 x2033 x2202 x2311 x3020 x3123 x3232
                        x0023 x0132 x0301 x1010 x1113 x1222 x1331 x2100 x2203 x2312 x3021 x3130 x3233
                        x0030 x0133 x0302 x1011 x1120 x1223 x1332 x2101 x2210 x2313 x3022 x3131 x3300
                        x0031 x0200 x0303 x1012 x1121 x1230 x1333 x2102 x2211 x2320 x3023 x3132 x3301
                        x0032 x0201 x0310 x1013 x1122 x1231 x2000 x2103 x2212 x2321 x3030 x3133 x3302
                        x0033 x0202 x0311 x1020 x1123 x1232 x2001 x2110 x2213 x2322 x3031 x3200 x3303
                        x0100 x0203 x0312 x1021 x1130 x1233 x2002 x2111 x2220 x2323 x3032 x3201 x3310
                        x0101 x0210 x0313 x1022 x1131 x1300 x2003 x2112 x2221 x2330 x3033 x3202 x3311
                        x0102 x0211 x0320 x1023 x1132 x1301 x2010 x2113 x2222 x2331 x3100 x3203 x3312
                        Hi William,

                        Is it possible to generate these variables as observations? As the max variables is 32767, if we set k =8, it will exceed the maximum limits. Thanks

                        Best regards
                        Chang

                        Comment


                        • #13
                          I will repeat what I wrote in post #2, which still holds and which your subsequent responses did not address.
                          Your question really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you. Show example data. Tell us how precisely you want to create the new variables if you were not using loops. The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output.
                          I've now guessed two answers, neither of which was suitable, and you ask for a new answer. At this point it is your turn to answer a few questions.

                          It is not at all clear what the input to this process is to be, what results you want this process to produce, and equally importantly, what you intend use those results for.

                          In post #1 you showed 3 levels, each of which had values 0-5. In post #3 and post #5 you showed 3 levels, each of which had values 0-3. In post #8 you asked for an arbitrary number of levels.
                          • What is the maximum number of levels (the biggest value of k you will want)?
                          • Does each level have the same range of values, or might they vary from level to level?
                          • What is the range of the values: 0-3, 0-5, or do you want to be able to specify an arbitrary maximum?
                          What do you want in the observations this process is to produce?
                          • What variables?
                          • What values?
                          • Show what the first ten observations should be like. Create a ten observation dataset using the Stata Data Editor, and then use the dataex command to present this sample data in your Results window, and then copy and paste from the Results window - copying the code delimiters [CODE] and [/CODE] and everything in between them - into your next post.
                          What are you going to use these results for?
                          • I am concerned that you have set yourself the problem of generating these n-tuples to solve some other problem that can be solved in a more effective way. Kind of like asking for instructions on how to invert a matrix when what you need is the regress command.
                          • Please explain the context within which these results will be used, so that in any event they can be created effectively for that context.

                          Comment


                          • #14
                            Thanks William. I'm conducting a project to test the robustness of the results of meta-analysis, and writting a ado file of the analysis process. This process is done by add 0, 1, 2, 3, ... events to the included studies, and re-do the meta-analysis by each add. Below is the example of the meta-analysis, where there are 4 studies. By this example, for each included studies, we need to add 0, 1,... to n(total events in the arm) events to the each study: first add 0 in the treatment arm to study 1, this is the original meta-analysis (without adding anything); and then add 1 to study 1, re-do the meta-analysis; continue add 2 to study 1 and redo the analysis; after finished the first study, we start add events to the second study. After finish all 4 studies, we start the combinations: for example, add 1 events to study 1 while add 2 to study 2, add three to study 3, add 0 to study 4. And considering all of the combinations.

                            So, in this example, we will have millsions of combinations. To make things simple, we set the the maximum events added as 3 (say, add 0, 1, 2, 3 to each study), so there are 4^4 = 256 combinations, and therefore we need to conduct 256 meta-analyses. And then to see if the significance of all the 256 meta-analyses are robust.

                            -----------------------
                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input byte(id a) float b byte c float d
                            1 15  96  8 101
                            2  3 186  2 193
                            3 31 240 20 247
                            4 12 106  7 116
                            end
                            ------------------

                            The code below is based on your code for this aim. I first generate these combinations as variables, and then reshape it to get the combinations as observations, and futher get the value added for each study. Then we add this values to the treatment arm and do the meta-analyses. This works well when the included studies are small (<= 5 studies), however, when we include 10 studies, there would be 4^10 variables, and is not applicable in Stata. I'm wondering if we can generate these combinations into observations directly.


                            Code:
                            local K 4
                            forvalues k=1/40 {
                                local v`k' !
                                if `k'<=`K' local v`k' 0 1 2 3
                            }
                            foreach a1 in `v1' {
                            foreach a2 in `v2' {
                            foreach a3 in `v3' {
                            foreach a4 in `v4' {
                            foreach a5 in `v5' {
                            foreach a6 in `v6' {
                            foreach a7 in `v7' {
                            foreach a8 in `v8' {
                            foreach a9 in `v9' {
                            foreach a10 in `v10' {
                            foreach a11 in `v11' {
                            foreach a12 in `v12' {
                            foreach a13 in `v13' {
                            foreach a14 in `v14' {
                            foreach a15 in `v15' {
                            foreach a16 in `v16' {
                            foreach a17 in `v17' {
                            foreach a18 in `v18' {
                            foreach a19 in `v19' {
                            foreach a20 in `v20' {
                            foreach a21 in `v21' {
                            foreach a22 in `v22' {
                            foreach a23 in `v23' {
                            foreach a24 in `v24' {
                            foreach a25 in `v25' {
                            foreach a26 in `v26' {
                            foreach a27 in `v27' {
                            foreach a28 in `v28' {
                            foreach a29 in `v29' {
                            foreach a30 in `v30' {
                            foreach a31 in `v31' {
                            foreach a32 in `v32' {
                            foreach a33 in `v33' {
                            foreach a34 in `v34' {
                            foreach a35 in `v35' {
                            foreach a36 in `v36' {
                            foreach a37 in `v37' {
                            foreach a38 in `v38' {
                            foreach a39 in `v39' {
                            foreach a40 in `v40' {
                            
                            
                            local foo x`a1'`a2'`a3'`a4'`a5'`a6'`a7'`a8'`a9'`a10'`a11'`a12'`a13'`a14'`a15'`a16'`a17'`a18'`a19'`a20'`a21'`a22'`a23'`a24'`a25'`a26'`a27'`a28'`a29'`a30'`a31'`a32'`a33'`a34'`a35'`a36'`a37'`a38'`a39'`a40'
                            local foo : subinstr local foo "!" "", all
                            generate `foo' = 1
                            
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            }
                            *
                            
                            set obs 1
                            gen comb= 1
                            reshape long x, i(comb) j(combinations) string
                            drop x comb
                            
                             
                            local i 4
                            
                            forvalues i=1(1)`i' {
                             gen study`i' = real(substr(combinations, `i', 1))
                                }

                            Here is the output of the above code, I listed 5 out of 256 combinations. As we can see, for combination 0000, we add 0 to study 1, 0 to study 2, 0 to study 3, and 0 to study 4 in the "a" cell in above meta-analysis example, this is the orignial meta-anlaysis data; similary, in combination 0001, we add 0 to study 1, study2, and study 3, while 1 to study 1, this will generate a new meta-analysis data. ... the same for other combinations. Each combination have have different data and then we can do the meta-analyses to see if the results are robust.
                            -----------------------
                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input str4 combinations float(study1 study2 study3 study4)
                            "0000" 0 0 0 0
                            "0001" 0 0 0 1
                            "0002" 0 0 0 2
                            "0003" 0 0 0 3
                            "0010" 0 0 1 0
                            end
                            ------------------


                            Now the question is, if we include 8 or more studies, for example, 10 studies,there would be 4^10 variables, and is not applicable in Stata. The possible solution is the generate these combiantions as observations directly, instead of generating variabels while reshape them into observations.

                            This is why I used different levels and numbers in my previous posts. Becuase the number of included studies is variant, and the levels is arbitrary (we now set it as three considering that the combinations are so big).

                            Thank you very much.

                            Best regards
                            Chang
                            Last edited by Chang Xu; 28 Feb 2021, 09:44.

                            Comment


                            • #15
                              By the way, I'm not sure if you are interested in this project, if you are intereted, we are so happy and be hornored to add you as a co-author; And if possible, may I use the code to form a ado. file to facilitate further systematic review authors to test the robuestness of their meta-analysis?

                              Thanks

                              Best regards
                              Chang
                              Last edited by Chang Xu; 28 Feb 2021, 09:51.

                              Comment

                              Working...
                              X