Announcement

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

  • Getting help on randomly assigning value to one observation in a group

    Hi everyone,

    I have a dataset where I am ranking participation in an intervention based on the person's relationship to the household head. In the dataset, I have three priorities, priority1 if you are the hosuehold head, priority2 if you are a child / grandchild / son-daughter in law of the household head and priority3 if you are the spouse. Each household has a parent ID which is in string and I have no issues with priority1 and priority3. The problem is with priority2 where potentially there are more than one child. Right now, my dataset is as follows --

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 hh_current_id str21 relation_with_hhh float(consent_group_meeting meeting_priority1 meeting_priority2)
    "1190073" "Spouse"              1 0 0
    "1190047" "Household Head"      1 1 0
    "1190047" "Spouse"              1 0 0
    "1002009" "Household Head"      1 1 0
    "1002009" "Spouse"              1 0 0
    "1002009" "Child"               1 0 1
    "1190026" "Household Head"      1 1 0
    "1190026" "Spouse"              1 0 0
    "1002007" "Household Head"      1 1 0
    "1002007" "Spouse"              1 0 0
    "1190040" "Household Head"      1 1 0
    "1190040" "Spouse"              1 0 0
    "1002003" "Spouse"              1 0 0
    "1189014" "Household Head"      1 1 0
    "1190038" "Household Head"      1 1 0
    "1190038" "Spouse"              1 0 0
    "1002011" "Household Head"      1 1 0
    "1002011" "Spouse"              1 0 0
    "1189023" "Spouse"              1 0 0
    "1189023" "Brother/Sister"      1 0 1
    "1189023" "Child"               1 0 1
    "1189023" "Son/daughter-in-law" 1 0 1
    "1189023" "Child"               1 0 1
    "1002016" "Household Head"      1 1 0
    "1002016" "Spouse"              1 0 0
    "1002016" "Child"               1 0 1
    "1172053" "Household Head"      1 1 0
    "1172053" "Spouse"              1 0 0
    "1172053" "Child"               1 0 1
    "1172053" "Son/daughter-in-law" 1 0 1
    "1173005" "Household Head"      1 1 0
    "1173005" "Spouse"              1 0 0
    "1002017" "Household Head"      1 1 0
    "1002017" "Spouse"              1 0 0
    "1189037" "Household Head"      1 1 0
    "1189037" "Spouse"              1 0 0
    "1189037" "Child"               1 0 1
    "1189037" "Son/daughter-in-law" 1 0 1
    "1189037" "Child"               1 0 1
    "1172067" "Household Head"      1 1 0
    "1172067" "Spouse"              1 0 0
    "1174027" "Household Head"      1 1 0
    "1174027" "Child"               1 0 1
    "1173016" "Household Head"      1 1 0
    "1173016" "Spouse"              1 0 0
    "1189029" "Household Head"      1 1 0
    "1189029" "Spouse"              1 0 0
    "1189029" "Child"               1 0 1
    "1189029" "Child"               1 0 1
    "1189029" "Son/daughter-in-law" 1 0 1
    "1172070" "Household Head"      1 1 0
    "1172070" "Spouse"              1 0 0
    "1002002" "Spouse"              1 0 0
    "1002002" "Child"               1 0 1
    "1002002" "Son/daughter-in-law" 1 0 1
    "1002002" "Child"               1 0 1
    "1189073" "Household Head"      1 1 0
    "1189073" "Spouse"              1 0 0
    "1189073" "Child"               1 0 1
    "1181053" "Spouse"              1 0 0
    "1002019" "Household Head"      1 1 0
    "1173013" "Household Head"      1 1 0
    "1173013" "Spouse"              1 0 0
    "1172009" "Household Head"      1 1 0
    "1172009" "Spouse"              1 0 0
    "1172009" "Brother/Sister"      1 0 1
    "1174024" "Household Head"      1 1 0
    "1174024" "Spouse"              1 0 0
    "1189030" "Spouse"              1 0 0
    "1189030" "Son/daughter-in-law" 1 0 1
    "1173003" "Household Head"      1 1 0
    "1173003" "Spouse"              1 0 0
    "1014055" "Household Head"      1 1 0
    "1014055" "Spouse"              1 0 0
    "1181069" "Household Head"      1 1 0
    "1181069" "Spouse"              1 0 0
    "1173001" "Household Head"      1 1 0
    "1177002" "Spouse"              1 0 0
    "1009003" "Spouse"              1 0 0
    "1009003" "Child"               1 0 1
    "1009003" "Son/daughter-in-law" 1 0 1
    "1002010" "Household Head"      1 1 0
    "1181072" "Household Head"      1 1 0
    "1181072" "Spouse"              1 0 0
    "1173017" "Household Head"      1 1 0
    "1173017" "Spouse"              1 0 0
    "1173017" "Child"               1 0 1
    "1172046" "Household Head"      1 1 0
    "1172046" "Spouse"              1 0 0
    "1172046" "Child"               1 0 1
    "1172046" "Son/daughter-in-law" 1 0 1
    "1172046" "Child"               1 0 1
    "1014051" "Household Head"      1 1 0
    "1014051" "Child"               1 0 1
    "1014051" "Son/daughter-in-law" 1 0 1
    "1174026" "Household Head"      1 1 0
    "1174026" "Spouse"              1 0 0
    "1174026" "Child"               1 0 1
    "1172020" "Household Head"      1 1 0
    "1172020" "Child"               1 0 1
    end
    What I want to do is for the variable meeting_priority2 I need a code that randomly assigns a value of 1 or 0 if there are more than observations per unique household_id. I have been thinking of a way to do this but cannot work with the grouped data. Any help will be appreciated.

    Regards,
    Ibrahim

  • #2
    This will get you a count of hh members:
    Code:
    egen members = count(relation_with_hhh) , by(hh_current_id)
    What do you want the random 0/1 to apply to? The household? Members of it?

    Comment


    • #3
      Hi George,

      I already have the count of hh members. I want the 0/1 to apply to the variable meeting_priority2, where certain household_ids have more than one as 1 right now. I want to randomise between 1 and 0 if there are more than two per household_id. Alternatively, rank them as 1,2,3 randomly if there are more than two. I'm not sure if I have been able to explain myself clearly?

      Comment


      • #4
        Number of meeting_priority2 by hh.

        Code:
        egen countmem2 = total(meeting_priority2), by(hh_current_id)
        It's not clear exactly, but this may help get you started.

        HTML Code:
        g dummy = 0
        replace dummy = 1 if runiform()>0.5 & countmem2>1

        Comment


        • #5
          So this is doing something close to what I want. But the problem is, it's reading countmem independent of its grouping within hh_current_id, let me show you the problem -

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str7 hh_current_id str21 relation_with_hhh float(countmem2 meeting_priority2_1)
          "1002009" "Child"               1 0
          "1189023" "Brother/Sister"      4 0
          "1189023" "Child"               4 0
          "1189023" "Son/daughter-in-law" 4 0
          "1189023" "Child"               4 0
          "1002016" "Child"               1 0
          "1172053" "Child"               2 0
          "1172053" "Son/daughter-in-law" 2 0
          "1189037" "Child"               3 0
          "1189037" "Son/daughter-in-law" 3 0
          "1189037" "Child"               3 0
          "1174027" "Child"               1 0
          "1189029" "Child"               3 0
          "1189029" "Child"               3 0
          "1189029" "Son/daughter-in-law" 3 0
          "1002002" "Child"               3 0
          "1002002" "Son/daughter-in-law" 3 0
          "1002002" "Child"               3 0
          "1189073" "Child"               1 0
          "1172009" "Brother/Sister"      1 0
          "1189030" "Son/daughter-in-law" 1 0
          "1009003" "Child"               2 1
          "1009003" "Son/daughter-in-law" 2 1
          "1173017" "Child"               1 0
          "1172046" "Child"               3 0
          "1172046" "Son/daughter-in-law" 3 0
          "1172046" "Child"               3 0
          "1014051" "Child"               2 0
          "1014051" "Son/daughter-in-law" 2 0
          "1174026" "Child"               1 0
          "1172020" "Child"               1 0
          "1181066" "Son/daughter-in-law" 1 0
          "1177050" "Child"               1 0
          "1002033" "Child"               1 0
          "1174006" "Child"               1 0
          "1014054" "Child"               1 0
          "1002001" "Child"               1 0
          "1172050" "Child"               4 0
          "1172050" "Son/daughter-in-law" 4 0
          "1172050" "Child"               4 0
          "1172050" "Son/daughter-in-law" 4 0
          "1009029" "Child"               2 1
          "1009029" "Child"               2 0
          "1174008" "Child"               2 1
          "1174008" "Son/daughter-in-law" 2 0
          "1181010" "Child"               2 1
          "1181010" "Son/daughter-in-law" 2 1
          "1002013" "Child"               1 0
          "1014020" "Child"               2 1
          "1014020" "Son/daughter-in-law" 2 0
          "1172058" "Child"               1 0
          "1002012" "Child"               2 1
          "1002012" "Child"               2 1
          "1002006" "Child"               2 0
          "1002006" "Child"               2 0
          "1181032" "Child"               1 0
          "1009078" "Son/daughter-in-law" 1 0
          "1002008" "Child"               1 0
          "1172080" "Brother/Sister"      2 1
          "1172080" "Brother/Sister"      2 1
          "1009023" "Child"               1 0
          "1174023" "Child"               1 0
          "1174016" "Child"               1 0
          "1174022" "Child"               1 0
          "1190056" "Child"               1 0
          "1009015" "Child"               1 0
          "1191001" "Child"               1 0
          "1181056" "Child"               4 0
          "1181056" "Child"               4 0
          "1181056" "Son/daughter-in-law" 4 0
          "1181056" "Son/daughter-in-law" 4 0
          "1191045" "Child"               3 0
          "1191045" "Son/daughter-in-law" 3 0
          "1191045" "Child"               3 0
          "1191067" "Son/daughter-in-law" 1 0
          "1014033" "Child"               5 0
          "1014033" "Child"               5 0
          "1014033" "Child"               5 0
          "1014033" "Son/daughter-in-law" 5 0
          "1014033" "Child"               5 0
          "1009002" "Child"               2 1
          "1009002" "Child"               2 1
          "1014072" "Child"               2 1
          "1014072" "Son/daughter-in-law" 2 1
          "1030006" "Brother/Sister"      1 0
          "1009052" "Child"               1 0
          "1009017" "Child"               1 0
          "1191008" "Child"               1 0
          "1030057" "Son/daughter-in-law" 2 0
          "1030057" "Grandchild"          2 0
          "1009022" "Child"               1 0
          "1001015" "Child"               4 0
          "1001015" "Child"               4 0
          "1001015" "Child"               4 0
          "1001015" "Child"               4 0
          "1001011" "Child"               1 0
          "1001007" "Child"               3 0
          "1001007" "Child"               3 0
          "1001007" "Child"               3 0
          "1014046" "Brother/Sister"      2 1
          end
          So here it's randomly assigning 1 or 0 to any countmem with 2, but I need it to randomly assign 1 or 0 whenever countmem is 2 within the same hh_current_id.

          Comment


          • #6

            so you always want one of the 2 to be 1 and the other be 0?

            Comment


            • #7
              Yes, that's right. For example, in the above dataset, I have hh_current_id
              "1009002" where both observations are 1 while in
              "1014051" both observations are 0. I need it to be such that one of those variables are going to be 1 and the other 0 for each hh_current_id in order to randomise entry into the intervention.

              Comment


              • #8
                are you doing a simulation, or will just once do?

                Comment


                • #9
                  This is a kluge, but it works.

                  Code:
                  egen countmem2 = total(meeting_priority2), by(hh_current_id)
                  bys hh_current_id: g num2 = _n if countmem==2 & meeting_priority2==1
                  egen num3 = min(num2), by(hh_current_id)
                  g newdum = 0
                  replace newdum = num2==num3 if num2!=.

                  Comment


                  • #10
                    Thank you George, it worked! I am doing this just once for this dataset.

                    Best,
                    Ibrahim

                    Comment


                    • #11
                      I was concerned the order might be an issue, but it doesn't look like the relation_with_hhh variable has any particular ordering. But watch that if your resort, else it's not really random.

                      Comment

                      Working...
                      X