Announcement

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

  • Dummy creation

    Hi there,

    Using the following code, i was unable to create dummy variables for this generated interval. I result does not exactly provide dummy variables.

    use poverty, clear
    keep xhpsu xhnum hhsize
    gen hhsize_grp=""

    replace hhsize_grp = "1-2 persons" if hhsize <= 2
    replace hhsize_grp = "3-4 persons" if hhsize >= 3 & hhsize <= 4
    replace hhsize_grp = "5-6 persons" if hhsize >= 5 & hhsize <= 6
    replace hhsize_grp = "7-8 persons" if hhsize >= 7 & hhsize <= 8
    replace hhsize_grp = "9-10 persons" if hhsize >= 9 & hhsize <= 10
    replace hhsize_grp = "11 or more" if hhsize >= 11

    Is there any I could get dummy variables for this? Thanks

  • #2
    You're creating a string variable with 6 categories. That may have uses for tables or graphs, but it's not a dummy or indicator variable, at least as I understand the term.

    If you use i.hhsize as a factor variable in model fitting you can think about further changes, if (for example) the pattern of coefficients suggests a simpler parameterization or you decide that some sizes have too few observations to work well as separate levels.

    Comment


    • #3
      Because you didn't show us your data (see FAQ #12) I made up some data to demonstrate one way to do (there are other ways without using recode). As Nick Cox already said: In most situations numeric variables (properly labeled) are better to use than string variables; additionally, your code does not create indicator variables (also called "dummy variables") -- but again, most often that is not necessary because you could use factor variable notation (see help fvvarlist).

      Note that I am using frequency weights only because I want to create an example data set. Have a look at the help for all commands you don't fully understand using help command :
      Code:
      . clear
      . input hhsize freq
      
              hhsize       freq
        1.    1  50
        2.    2  70
        3.    3  80
        4.    4  85
        5.    5  82
        6.    6  61
        7.    7  40
        8.    8  18
        9.    9   5
       10.   10   3
       11.   11   2
       12.   12   2
       13.   13   1
       14.   .   12
       15. end
      
      . recode hhsize (11/max = 6 "[11+")   ///
      >               ( 9/10  = 5 "[9-10]") ///
      >               ( 7/ 8  = 4 "[7-8]")  ///
      >               ( 5/ 6  = 3 "[5-6]")  ///
      >               ( 3/ 4  = 2 "[3-4]")  ///
      >               ( 1/ 2  = 1 "[1-2]"), gen(hhsize_grp)
      (12 differences between hhsize and hhsize_grp)
      
      . // check the recoding of hhsize:
      . tab2 hhsize hhsize_grp [fw=freq], mi
      
      -> tabulation of hhsize by hhsize_grp  
      
                 |                               RECODE of hhsize
          hhsize |     [1-2]      [3-4]      [5-6]      [7-8]     [9-10]       [11+          . |     Total
      -----------+-----------------------------------------------------------------------------+----------
               1 |        50          0          0          0          0          0          0 |        50
               2 |        70          0          0          0          0          0          0 |        70
               3 |         0         80          0          0          0          0          0 |        80
               4 |         0         85          0          0          0          0          0 |        85
               5 |         0          0         82          0          0          0          0 |        82
               6 |         0          0         61          0          0          0          0 |        61
               7 |         0          0          0         40          0          0          0 |        40
               8 |         0          0          0         18          0          0          0 |        18
               9 |         0          0          0          0          5          0          0 |         5
              10 |         0          0          0          0          3          0          0 |         3
              11 |         0          0          0          0          0          2          0 |         2
              12 |         0          0          0          0          0          2          0 |         2
              13 |         0          0          0          0          0          1          0 |         1
               . |         0          0          0          0          0          0         12 |        12
      -----------+-----------------------------------------------------------------------------+----------
           Total |       120        165        143         58          8          5         12 |       511
      
      . // show percentages of subgroups excluding missings:
      . tab1 hhsize_grp [fw=freq]
      
      -> tabulation of hhsize_grp  
      
        RECODE of |
           hhsize |      Freq.     Percent        Cum.
      ------------+-----------------------------------
            [1-2] |        120       24.05       24.05
            [3-4] |        165       33.07       57.11
            [5-6] |        143       28.66       85.77
            [7-8] |         58       11.62       97.39
           [9-10] |          8        1.60       99.00
             [11+ |          5        1.00      100.00
      ------------+-----------------------------------
            Total |        499      100.00
      
      . // generate indicator variables from hhsize_grp:
      . qui tab1 hhsize_grp, gen(hhsize_grp)
      
      . // display variable labels of the indicator variables:
      . d hhsize_grp?
      
      Variable      Storage   Display    Value
          name         type    format    label      Variable label
      --------------------------------------------------------------------
      hhsize_grp1     byte    %8.0g                 hhsize_grp==[1-2]
      hhsize_grp2     byte    %8.0g                 hhsize_grp==[3-4]
      hhsize_grp3     byte    %8.0g                 hhsize_grp==[5-6]
      hhsize_grp4     byte    %8.0g                 hhsize_grp==[7-8]
      hhsize_grp5     byte    %8.0g                 hhsize_grp==[9-10]
      hhsize_grp6     byte    %8.0g                 hhsize_grp==[11+
      
      . // show proportions of hhsize_grp1 to hhsize_grp6:
      . sum hhsize_grp? [fw=freq]
      
          Variable |        Obs        Mean    Std. dev.       Min        Max
      -------------+---------------------------------------------------------
       hhsize_grp1 |        499     .240481    .4278044          0          1
       hhsize_grp2 |        499    .3306613    .4709234          0          1
       hhsize_grp3 |        499    .2865731    .4526141          0          1
       hhsize_grp4 |        499    .1162325    .3208251          0          1
       hhsize_grp5 |        499    .0160321    .1257248          0          1
      -------------+---------------------------------------------------------
       hhsize_grp6 |        499      .01002    .0996973          0          1

      Comment

      Working...
      X