Announcement

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

  • how to randomly assign the observations to different groups with some requirements?

    I want to randomly assign the observations to 12 groups, trying the most posibility to make sure that the same major is in the same group and distinct provinces are in the same group. How to realize this?
    Each group has 6 observations except only one group has 5 observations.
    Thanks a ton!
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id province) str45 major
     1 19 "social science"
     2 20 "social science"
     3  7 "accounting"    
     4 20 "social science"
     5 20 "social science"
     6  9 "social science"
     7  6 "economics"    
     8  3 "accounting"    
     9  5 "world trade"  
    10 10 "finance"      
    11 19 "social science"
    12 11 "accounting"    
    13  6 "world trade"  
    14 18 "business"      
    15 17 "world trade"  
    16  9 "social science"
    17  6 "financial tech"
    18  4 "finance"      
    19 18 "economics"    
    20  4 "finance"      
    21  5 "financial tech"
    22  6 "finance"      
    23  1 "economics"    
    24  4 "finance"      
    25 11 "accounting"    
    26  9 "social science"
    27 16 "social science"
    28  9 "social science"
    29  9 "social science"
    30  1 "economics"    
    31  9 "social science"
    32 20 "social science"
    33  5 "finance"      
    34 13 "business"      
    35 18 "finance"      
    36 20 "social science"
    37  3 "finance"      
    38  1 "finance"      
    39 21 "business"      
    40 14 "social science"
    41 12 "financial tech"
    42  9 "social science"
    43 13 "economics"    
    44 16 "social science"
    45  5 "economics"    
    46  2 "financial tech"
    47  1 "economics"    
    48 14 "social science"
    49 15 "financial tech"
    50 18 "economics"    
    51  9 "social science"
    52 14 "social science"
    53 14 "social science"
    54 15 "financial tech"
    55  6 "finance"      
    56  9 "social science"
    57  5 "economics"    
    58 20 "social science"
    59 10 "world trade"  
    60 14 "social science"
    61  8 "financial tech"
    62 13 "financial tech"
    63 14 "social science"
    64 19 "social science"
    65  4 "financial tech"
    66  2 "economics"    
    67  9 "social science"
    68  9 "social science"
    69  6 "business"      
    70 20 "social science"
    71 15 "financial tech"
    end
    Last edited by Fred Lee; 17 Aug 2022, 03:55.

  • #2
    I'm having trouble understanding what you want. Do you mean that all individuals who have the same combination of major and province should be assigned to the same random group? Also, what does "group" refer to when you write: "Each group has 6 observations except only one group has 5 observations." How would the "6" apply to the example data? I think you will need to describe this differently and show a different or perhaps more elaborate data example.

    Comment


    • #3
      Thanks Mike! Sorry for the unclarity. Since the number of observations is 71, therefore 11 groups* 6 and 1group *5, totally 12 groups.
      I hope the same major are in the same group while the same provinces are not in the same group, that is to say each group consists of individuals from different provinces. I know there must be some observations from the same provinces are in the same group due to the overlap, but try best to make the provinces in the same group distinct.

      Comment


      • #4
        I find myself even more confused now about what you want. You could either wait for a response from one of the people here who is better at understanding questions than I am, or find a colleague to help you rewrite your question and post it again.

        Comment


        • #5
          Fred, ambiguous algorithms won't lead to clear solutions. Part of your request has no precise definition, like "trying the most possibility" and "try best".

          For example, there are 7 observations containing "social science" and province 20. How would you arrange the 7 observations? Assigned into 7 different groups -- succeed in avoiding to have the same province in a same group but fail to include the same major in a same group. Any other solution would be a trade-off between "assigning the same province in different groups" and "assigning the same major in the same group", and there is no definite criteria for the trade-off.

          Comment


          • #6
            Thanks so much Fei! The only requirement that must be met is that 12 groups with 11 groups*6 and 1 group*5. Since there is a trade-off between "assigning the same province in different groups" and "assigning the same major in the same group", can we meet only one of them? Thanks again!

            Comment


            • #7
              You have 29 observations of "social science" and a group cannot be larger than 6, so you will fail "assigning the same major in the same group."

              So your problem reduces to assigning the 71 observations to 12 groups such that a given province appears either 0 or 1 time in each group.

              I would attack this with a pencil and paper.

              Comment


              • #8
                Originally posted by William Lisowski View Post

                So your problem reduces to assigning the 71 observations to 12 groups such that a given province appears either 0 or 1 time in each group.
                Exactly! Thanks so much in advance, William!

                Comment


                • #9
                  HI William Lisowski, have you solved this? Thanks a ton!

                  Comment


                  • #10
                    I'm sorry, in suggesting that if it were my problem I would attack it with pencil and paper rather than with Stata, I did not mean to leave the impression that I would undertake that pencil-and-paper task for this problem.


                    Comment


                    • #11
                      Here is what I would have done with pencil and paper - gone through the provinces and assigned each observation to a successive group. Either it meets your needs, or you can use this to start your paper-and-pencil work to obtain a result that better meets your needs.
                      Code:
                      --------------------------------------------------------------------
                               |                            group                         
                               |  1   2   3   4   5   6   7   8   9   10   11   12   Total
                      ---------+----------------------------------------------------------
                      province |                                                          
                        1      |  1   1   1   1                                          4
                        2      |                  1   1                                  2
                        3      |                          1   1                          2
                        4      |                                  1    1    1    1       4
                        5      |  1   1   1   1   1                                      5
                        6      |                      1   1   1   1    1    1            6
                        7      |                                                 1       1
                        8      |  1                                                      1
                        9      |      1   1   1   1   1   1   1   1    1    1    1      11
                        10     |  1   1                                                  2
                        11     |          1   1                                          2
                        12     |                  1                                      1
                        13     |                      1   1   1                          3
                        14     |  1   1                           1    1    1    1       6
                        15     |          1   1   1                                      3
                        16     |                      1   1                              2
                        17     |                              1                          1
                        18     |                                  1    1    1    1       4
                        19     |  1   1   1                                              3
                        20     |              1   1   1   1   1   1    1                 7
                        21     |                                            1            1
                        Total  |  6   6   6   6   6   6   6   6   6    6    6    5      71
                      --------------------------------------------------------------------
                      
                      ----------------------------------------------------------------------------
                                       |                            group                         
                                       |  1   2   3   4   5   6   7   8   9   10   11   12   Total
                      -----------------+----------------------------------------------------------
                      major            |                                                          
                        accounting     |          1   1               1                  1       4
                        business       |                      1       1   1         1            4
                        economics      |  1   1   2   1       2       1        1         1      10
                        finance        |  1   1       1           1       2    1    2    1      10
                        financial tech |  1       1   1   4       1            1    1           10
                        social science |  2   3   2   2   2   3   3   2   3    3    2    2      29
                        world trade    |  1   1                   1   1                          4
                        Total          |  6   6   6   6   6   6   6   6   6    6    6    5      71
                      ----------------------------------------------------------------------------
                      
                        +----------------------------------------+
                        | group   province            major   id |
                        |----------------------------------------|
                        |     1          1        economics   30 |
                        |     1          5      world trade    9 |
                        |     1          8   financial tech   61 |
                        |     1         10          finance   10 |
                        |     1         14   social science   53 |
                        |     1         19   social science   64 |
                        |----------------------------------------|
                        |     2          1        economics   23 |
                        |     2          5          finance   33 |
                        |     2          9   social science   51 |
                        |     2         10      world trade   59 |
                        |     2         14   social science   48 |
                        |     2         19   social science    1 |
                        |----------------------------------------|
                        |     3          1        economics   47 |
                        |     3          5        economics   45 |
                        |     3          9   social science   56 |
                        |     3         11       accounting   25 |
                        |     3         15   financial tech   49 |
                        |     3         19   social science   11 |
                        |----------------------------------------|
                        |     4          1          finance   38 |
                        |     4          5        economics   57 |
                        |     4          9   social science   28 |
                        |     4         11       accounting   12 |
                        |     4         15   financial tech   54 |
                        |     4         20   social science   70 |
                        |----------------------------------------|
                        |     5          2   financial tech   46 |
                        |     5          5   financial tech   21 |
                        |     5          9   social science   26 |
                        |     5         12   financial tech   41 |
                        |     5         15   financial tech   71 |
                        |     5         20   social science   36 |
                        |----------------------------------------|
                        |     6          2        economics   66 |
                        |     6          6        economics    7 |
                        |     6          9   social science   67 |
                        |     6         13         business   34 |
                        |     6         16   social science   27 |
                        |     6         20   social science    2 |
                        |----------------------------------------|
                        |     7          3          finance   37 |
                        |     7          6      world trade   13 |
                        |     7          9   social science   16 |
                        |     7         13   financial tech   62 |
                        |     7         16   social science   44 |
                        |     7         20   social science    5 |
                        |----------------------------------------|
                        |     8          3       accounting    8 |
                        |     8          6         business   69 |
                        |     8          9   social science   31 |
                        |     8         13        economics   43 |
                        |     8         17      world trade   15 |
                        |     8         20   social science    4 |
                        |----------------------------------------|
                        |     9          4          finance   18 |
                        |     9          6          finance   22 |
                        |     9          9   social science    6 |
                        |     9         14   social science   52 |
                        |     9         18         business   14 |
                        |     9         20   social science   58 |
                        |----------------------------------------|
                        |    10          4          finance   20 |
                        |    10          6   financial tech   17 |
                        |    10          9   social science   42 |
                        |    10         14   social science   63 |
                        |    10         18        economics   19 |
                        |    10         20   social science   32 |
                        |----------------------------------------|
                        |    11          4   financial tech   65 |
                        |    11          6          finance   55 |
                        |    11          9   social science   29 |
                        |    11         14   social science   40 |
                        |    11         18          finance   35 |
                        |    11         21         business   39 |
                        |----------------------------------------|
                        |    12          4          finance   24 |
                        |    12          7       accounting    3 |
                        |    12          9   social science   68 |
                        |    12         14   social science   60 |
                        |    12         18        economics   50 |
                        +----------------------------------------+

                      Comment


                      • #12
                        Great, thanks William, this is absolutely I want. You ideas inspire me. If this can be done through Stata random function, that would be perfect. Still, you solved my problem. Thanks!

                        Comment

                        Working...
                        X