Announcement

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

  • The preferred number of children in one household

    Hi, I need to know how many children that are preferred by mom in one household. May anyone suggest the command or the code for this problem?
    Let's say if mom have son preference, and in one household, there are 2 boys and 1 girl. So the preferred number of children in one household is equal to 2.
    If mom have daughter preference, and there are 2 boys and 1 girl in one household, so the preferred number of children in one household is equal to 1.
    hhid is household id, and pid is individual id.
    Thank you!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double pid str8 hhid float sonpref double sex
    10 "0021800" 0 0
    9 "0030100" 1 1
    9 "0030500" 0 0
    3 "0031041" 0 1
    3 "0031731" 1 0
    9 "0032500" 0 0
    7 "0032500" 0 0
    3 "0032700" 0 1
    5 "0032900" 0 1
    6 "0032900" 0 0
    5 "0043022" 0 0
    4 "0051341" 0 1
    3 "0051500" 0 1
    7 "0051600" 0 0
    4 "0060431" 1 1
    3 "0060431" 1 0
    3 "0061231" 1 0
    8 "0061700" 0 1
    6 "0071300" 0 1
    4 "0072200" 0 1
    9 "0072500" 1 1
    6 "0080100" 0 1
    3 "0081731" 0 0
    5 "0082500" 1 0
    4 "0082800" 1 1
    7 "0082800" 0 0
    4 "0082931" 0 0
    3 "0082931" 0 0
    11 "0090700" 1 0
    7 "0090900" 1 0
    5 "0090900" 1 1
    6 "0091400" 1 1
    5 "0091400" 1 1
    8 "0091400" 1 0
    7 "0091400" 1 0
    10 "0091531" 0 1
    9 "0092300" 1 0
    3 "0092441" 1 0
    8 "0092600" 1 1
    15 "0102600" 1 0
    14 "0102600" 1 0
    16 "0102600" 1 1
    5 "0102600" 1 0
    6 "0102700" 0 1
    7 "0102700" 0 0
    8 "0102700" 0 0
    9 "0102700" 0 0
    4 "0104331" 0 0
    4 "0111100" 1 0
    5 "0111100" 1 1
    2 "0112632" 0 0
    3 "0112632" 0 1
    3 "0120200" 1 0
    5 "0120900" 1 0
    3 "0121432" 1 1
    4 "0121432" 1 1
    7 "0141800" 0 0
    3 "0141900" 1 0
    3 "0150431" 0 0
    6 "0150600" 0 0
    7 "0150700" 0 1
    8 "0150700" 0 1
    5 "0171000" 1 1
    4 "0180431" 0 1
    4 "0180800" 1 0
    3 "0180800" 1 1
    6 "0180931" 1 0
    10 "0191000" 0 0
    3 "0191011" 1 0
    4 "0191011" 1 0
    3 "0201031" 1 0
    10 "0201900" 0 1
    8 "0202000" 0 1
    8 "0210600" 0 1
    6 "0210600" 0 0
    7 "0210600" 0 0
    3 "0211131" 1 1
    4 "0211131" 1 1
    3 "0220531" 0 1
    7 "0222000" 0 0
    3 "0231141" 1 1
    4 "0242300" 1 1
    6 "0242300" 1 1
    5 "0242841" 1 1
    3 "0250131" 0 1
    3 "0251031" 0 0
    3 "0251331" 1 0
    10 "0251500" 0 1
    4 "0260221" 1 0
    6 "0270300" 1 0
    7 "0270300" 1 0
    15 "0270600" 1 1
    4 "0270613" 0 1
    5 "0270831" 0 1
    10 "0271500" 1 1
    3 "0272941" 1 0
    9 "0280100" 1 1
    4 "0281041" 1 0
    3 "0291100" 1 0
    3 "0291400" 0 1
    end
    sex : 0 = girl | 1 = boy
    sonpref : 0 = mother preferred girl | 1 = mother preferred boy
    pid : id for individual
    hhid : id for household
    Last edited by Zia Thahira; 14 May 2022, 12:35.

  • #2
    Your question implies that preference is invariant within households. I find one inconsistency.

    Code:
    bys hhid (sonpref): gen varying= sonpref[1]!= sonpref[_N]
    l hhid pid sonpref if varying
    Res.:

    Code:
    . l pid hhid sonpref if varying
    
         +-------------------------+
         | pid      hhid   sonpref |
         |-------------------------|
     25. |   7   0082800         0 |
     26. |   4   0082800         1 |
         +-------------------------+
    This suggests that you want to extract the preference of one household member, e.g., the mother. If we ignore this one inconsistency, you can get what you want as follows:

    Code:
    bys hhid: egen wanted= total(sonpref&sex|!sonpref&!sex)

    Res.:

    Code:
    . l hhid sonpref sex wanted, sepby(hhid)
    
         +----------------------------------+
         |    hhid   sonpref   sex   wanted |
         |----------------------------------|
      1. | 0021800         0     0        1 |
         |----------------------------------|
      2. | 0030100         1     1        1 |
         |----------------------------------|
      3. | 0030500         0     0        1 |
         |----------------------------------|
      4. | 0031041         0     1        0 |
         |----------------------------------|
      5. | 0031731         1     0        0 |
         |----------------------------------|
      6. | 0032500         0     0        2 |
      7. | 0032500         0     0        2 |
         |----------------------------------|
      8. | 0032700         0     1        0 |
         |----------------------------------|
      9. | 0032900         0     1        1 |
     10. | 0032900         0     0        1 |
         |----------------------------------|
     11. | 0043022         0     0        1 |
         |----------------------------------|
     12. | 0051341         0     1        0 |
         |----------------------------------|
     13. | 0051500         0     1        0 |
         |----------------------------------|
     14. | 0051600         0     0        1 |
         |----------------------------------|
     15. | 0060431         1     1        1 |
     16. | 0060431         1     0        1 |
         |----------------------------------|
     17. | 0061231         1     0        0 |
         |----------------------------------|
     18. | 0061700         0     1        0 |
         |----------------------------------|
     19. | 0071300         0     1        0 |
         |----------------------------------|
     20. | 0072200         0     1        0 |
         |----------------------------------|
     21. | 0072500         1     1        1 |
         |----------------------------------|
     22. | 0080100         0     1        0 |
         |----------------------------------|
     23. | 0081731         0     0        1 |
         |----------------------------------|
     24. | 0082500         1     0        0 |
         |----------------------------------|
     25. | 0082800         0     0        1 |
     26. | 0082800         1     1        1 |
         |----------------------------------|
     27. | 0082931         0     0        2 |
     28. | 0082931         0     0        2 |
         |----------------------------------|
     29. | 0090700         1     0        0 |
         |----------------------------------|
     30. | 0090900         1     0        1 |
     31. | 0090900         1     1        1 |
         |----------------------------------|
     32. | 0091400         1     0        2 |
     33. | 0091400         1     0        2 |
     34. | 0091400         1     1        2 |
     35. | 0091400         1     1        2 |
         |----------------------------------|
     36. | 0091531         0     1        0 |
         |----------------------------------|
     37. | 0092300         1     0        0 |
         |----------------------------------|
     38. | 0092441         1     0        0 |
         |----------------------------------|
     39. | 0092600         1     1        1 |
         |----------------------------------|
     40. | 0102600         1     0        1 |
     41. | 0102600         1     0        1 |
     42. | 0102600         1     1        1 |
     43. | 0102600         1     0        1 |
         |----------------------------------|
     44. | 0102700         0     0        3 |
     45. | 0102700         0     1        3 |
     46. | 0102700         0     0        3 |
     47. | 0102700         0     0        3 |
         |----------------------------------|
     48. | 0104331         0     0        1 |
         |----------------------------------|
     49. | 0111100         1     1        1 |
     50. | 0111100         1     0        1 |
         |----------------------------------|
     51. | 0112632         0     0        1 |
     52. | 0112632         0     1        1 |
         |----------------------------------|
     53. | 0120200         1     0        0 |
         |----------------------------------|
     54. | 0120900         1     0        0 |
         |----------------------------------|
     55. | 0121432         1     1        2 |
     56. | 0121432         1     1        2 |
         |----------------------------------|
     57. | 0141800         0     0        1 |
         |----------------------------------|
     58. | 0141900         1     0        0 |
         |----------------------------------|
     59. | 0150431         0     0        1 |
         |----------------------------------|
     60. | 0150600         0     0        1 |
         |----------------------------------|
     61. | 0150700         0     1        0 |
     62. | 0150700         0     1        0 |
         |----------------------------------|
     63. | 0171000         1     1        1 |
         |----------------------------------|
     64. | 0180431         0     1        0 |
         |----------------------------------|
     65. | 0180800         1     0        1 |
     66. | 0180800         1     1        1 |
         |----------------------------------|
     67. | 0180931         1     0        0 |
         |----------------------------------|
     68. | 0191000         0     0        1 |
         |----------------------------------|
     69. | 0191011         1     0        0 |
     70. | 0191011         1     0        0 |
         |----------------------------------|
     71. | 0201031         1     0        0 |
         |----------------------------------|
     72. | 0201900         0     1        0 |
         |----------------------------------|
     73. | 0202000         0     1        0 |
         |----------------------------------|
     74. | 0210600         0     1        2 |
     75. | 0210600         0     0        2 |
     76. | 0210600         0     0        2 |
         |----------------------------------|
     77. | 0211131         1     1        2 |
     78. | 0211131         1     1        2 |
         |----------------------------------|
     79. | 0220531         0     1        0 |
         |----------------------------------|
     80. | 0222000         0     0        1 |
         |----------------------------------|
     81. | 0231141         1     1        1 |
         |----------------------------------|
     82. | 0242300         1     1        2 |
     83. | 0242300         1     1        2 |
         |----------------------------------|
     84. | 0242841         1     1        1 |
         |----------------------------------|
     85. | 0250131         0     1        0 |
         |----------------------------------|
     86. | 0251031         0     0        1 |
         |----------------------------------|
     87. | 0251331         1     0        0 |
         |----------------------------------|
     88. | 0251500         0     1        0 |
         |----------------------------------|
     89. | 0260221         1     0        0 |
         |----------------------------------|
     90. | 0270300         1     0        0 |
     91. | 0270300         1     0        0 |
         |----------------------------------|
     92. | 0270600         1     1        1 |
         |----------------------------------|
     93. | 0270613         0     1        0 |
         |----------------------------------|
     94. | 0270831         0     1        0 |
         |----------------------------------|
     95. | 0271500         1     1        1 |
         |----------------------------------|
     96. | 0272941         1     0        0 |
         |----------------------------------|
     97. | 0280100         1     1        1 |
         |----------------------------------|
     98. | 0281041         1     0        0 |
         |----------------------------------|
     99. | 0291100         1     0        0 |
         |----------------------------------|
    100. | 0291400         0     1        0 |
         +----------------------------------+
    Last edited by Andrew Musau; 14 May 2022, 15:43.

    Comment


    • #3
      Thanks alot Andrew, this is really helpfull!

      I've already got the preference of mother's and there are about 500 kids that not match mother's preference (total number of preferred children in one hosehold is 0). So how to group those unwanted kids by their sex? In simple way, I want to know how many unwanted daughters and unwanted sons among those 500 children.
      Last edited by Zia Thahira; 14 May 2022, 19:52.

      Comment


      • #4
        This is the total number of kids less preferred. Their gender is the negation of the variable "sonpref".

        Code:
        encode hhid, g(HHID)
        bys HHID: egen preferred= total(sonpref&sex|!sonpref&!sex)
        bys HHID: gen wanted= _N-preferred
        *Number of girls if preferred=0 in a household
        tabstat wanted if !preferred & sonpref, by(HHID) stats(count)
        *Number of boys if preferred=0 in a household
        tabstat wanted if !preferred & !sonpref, by(HHID) stats(count)

        (total number of preferred children in one hosehold is 0)

        Your restriction only considers households where total preferred is equal to 0. However, even in households where this number is positive, there may be kids of a gender different to the preferred gender. For example, hhid=0091400 has four kids, two of the preferred gender and two of the opposite gender. To relax this constraint, delete "!preferred" in the code above.


        Res.:

        Code:
        . *Number of girls if preferred=0 in a household
        
        .
        . tabstat wanted if !preferred & sonpref, by(HHID) stats(count)
        
        Summary for variables: wanted
             by categories of: HHID
        
           HHID |         N
        --------+----------
        0031731 |         1
        0061231 |         1
        0082500 |         1
        0090700 |         1
        0092300 |         1
        0092441 |         1
        0120200 |         1
        0120900 |         1
        0141900 |         1
        0180931 |         1
        0191011 |         2
        0201031 |         1
        0251331 |         1
        0260221 |         1
        0270300 |         2
        0272941 |         1
        0281041 |         1
        0291100 |         1
        --------+----------
          Total |        20
        -------------------
        
        .
        . *Number of boys if preferred=0 in a household
        
        .
        . tabstat wanted if !preferred & !sonpref, by(HHID) stats(count)
        
        Summary for variables: wanted
             by categories of: HHID
        
           HHID |         N
        --------+----------
        0031041 |         1
        0032700 |         1
        0051341 |         1
        0051500 |         1
        0061700 |         1
        0071300 |         1
        0072200 |         1
        0080100 |         1
        0091531 |         1
        0150700 |         2
        0180431 |         1
        0201900 |         1
        0202000 |         1
        0220531 |         1
        0250131 |         1
        0251500 |         1
        0270613 |         1
        0270831 |         1
        0291400 |         1
        --------+----------
          Total |        20
        -------------------
        
        .
        Last edited by Andrew Musau; 15 May 2022, 03:34.

        Comment


        • #5
          The quantity sought seems to be the number of children of the preferred sex, which isn't (without further information), the preferred number of children.

          Comment

          Working...
          X