Announcement

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

  • Creating One Categorical Variable by Combining 12 Variables

    Hello,

    I am working on DHS data. I am analyzing the type of facilities used by women for antenatal care. I am coding 12 variables to create one categorical variable. I am combining them either as Private, Public, or Home categories. I have run the following codes but I have run into issues when I am doing regression analysis. The issue arise because some women who received antenatal care at home also visited public or private hospitals/clinics. In regression, home is my compersion group (0). In the regression results, the private category is completely omitted. I do not understand where I have made the mistake. I have made changes in my codes but the problem does not resolve. The coding commands are as below. I can not understand where I am going wrong or what mistakes I have made in any way. Can you kindly help me in pointing out my mistake as well as help me with my codes? I really appreciate your help in this crucial time of mine.
    Thank you

    Aamna


    HTML Code:
    **Ownership of Facilities (m57)
    gen antenatalvisit_facility=0
    foreach xvar of varlist m57a m57b m57e m57f m57g m57h m57l m57m m57n m57o m57p m57q m57s m57x{
    replace antenatalvisit_facility=. if `xvar'==.
    }
    tab antenatalvisit_facility, mi
    
    *Antenatal visit by private/govt/other facilities RECODE
    gen antenatalvisit_public=antenatalvisit_facility
    label var antenatalvisit_public "Public Facility Used For Antenatal Care Visits During Pregnancy"
    label values antenatalvisit_public decision
    foreach xvar of varlist m57e m57f m57g m57l {
    replace antenatalvisit_public=1 if `xvar'==1 & `xvar'!= .
    }
    tab antenatalvisit_public, mi
    
    gen antenatalvisit_private=antenatalvisit_facility
    label var antenatalvisit_private "Private Facility Used For Antenatal Care Visits During Pregnancy"
    label values antenatalvisit_private decision
    foreach xvar of varlist m57m m57n m57h m57o m57p m57q m57s m57x{
    replace antenatalvisit_private=1 if `xvar'==1 & `xvar'!= .
    }
    tab antenatalvisit_private, mi
    
    gen antenatalvisit_home=antenatalvisit_facility
    label var antenatalvisit_home "Home Used For Antenatal Care Visits During Pregnancy"
    label values antenatalvisit_home decision
    foreach xvar of varlist m57a m57b {
    replace antenatalvisit_home=1 if `xvar'==1 & `xvar'!= .
    }
    tab antenatalvisit_home, mi
    
    gen ownership=.
    replace ownership=0 if antenatalvisit_home==1
    replace ownership=1 if antenatalvisit_public==1
    replace ownership=2 if antenatalvisit_private==1
    label var ownership "Ownership of Facilities"
    label values ownership ownership
    tab ownership, mi

  • #2
    HTML Code:
    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(m57a m57b m57e m57f m57h m57n m57m)
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 1 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 1 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 1 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 1 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 1 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 1 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 1 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 1 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 1
    1 0 1 0 0 0 0
    1 0 1 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 1 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 1 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 1
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 0 0 0 0 0 0
    1 1 0 0 0 0 0
    1 1 0 0 0 0 0
    1 1 0 0 0 0 1
    1 1 0 0 0 0 0
    end
    label values m57a M57A
    label def M57A 1 "yes", modify
    label values m57b M57B
    label def M57B 0 "no", modify
    label def M57B 1 "yes", modify
    label values m57e M57E
    label def M57E 0 "no", modify
    label def M57E 1 "yes", modify
    label values m57f M57F
    label def M57F 0 "no", modify
    label values m57h M57H
    label def M57H 0 "no", modify
    label def M57H 1 "yes", modify
    label values m57n M57N
    label def M57N 0 "no", modify
    label def M57N 1 "yes", modify
    label values m57m M57M
    label def M57M 0 "no", modify
    label def M57M 1 "yes", modify
    [/CODE]

    Comment


    • #3
      Aamna Qamar I am afraid you need to help us help you.
      1. Your code doesn't work because many of the variables you use, such as m57g, m57l, m57o, and others do not figure in your data example.
      2. You do not show us the regression command you are running.
      Also, I notice that your code for generating antenatalvisit_facility will never let it take the value 1; it is either 0 or missing. Is that a mistake? Does fixing that solve your problem?

      Also, you may be able to simplify and shorten your code considerably by using -egen- commands (see the anymatch function in the help for egen).

      Edit: this was cross-posted with #4. Nick's use of -inlist()- functions achieves the same thing that I was suggesting using -egen- and -anymatch-.
      Last edited by Hemanshu Kumar; 11 Sep 2022, 01:03.

      Comment


      • #4
        I haven't tried to follow your rather intricate code in full. It seems that you want to combine three (0, 1) indicators that aren't disjoint. Given that, there seem to be 2^3 = 8 possibilities in principle, not 3.

        This may help:


        Code:
        gen antenatalvisit_public = inlist(1, m57e, m57f, m57g, m57l)
        
        gen antenatalvisit_private = inlist(1, m57m, m57n, m57h, m57o, m57p, m57q, m57s, m57x) 
        
        gen antenatalvisit_home = inlist(1, m57a, m57b) 
        
        gen ownership = antenatalvisit_home + 2 * antenatalvisit_public + 4 * antenatalvisit_private 
        
        label def ownership 0 "none" 1 "home only" 2 "public only" 3 "public and home" 4 "private only" 5 "private and home" 6 "private and public" 7 "private, public, home"
        
        label val ownership ownership

        Comment


        • #5
          Hello,

          Thank you so much for your help. I really appreciate it. I will show this to my advisor and ask for his feedback. If he wants something different then I will come back and request more help.

          Nick Cox - Can you kindly explain why we are multiplying by 2 and 4 in the following command. I am new to this type of command.
          HTML Code:
          gen ownership = antenatalvisit_home + 2 * antenatalvisit_public + 4 * antenatalvisit_private
          Hemanshu Kumar - I did not add other variables because all of them were 0 and due to this I was not facing any issue. The issue arose due to the presence of 1 (yes) in 2 or 3 variables for the same observation.

          Thank you once again for your help.

          Aamna

          Comment


          • #6
            As said, the joint occurrence of three binary (indicator) variables can be handled in terms of the 2^3 = 8 joint possibilities.

            Code:
            clear
            input private public home
            1 1 1
            1 1 0
            1 0 1
            1 0 0
            0 1 1
            0 1 0
            0 0 1
            0 0 0
            end
            
            gen ownership = home + 2 * public + 4 * private
            
            list , sep(2)
            
                +------------------------------------+
                 | private   public   home   owners~p |
                 |------------------------------------|
              1. |       1        1      1          7 |
              2. |       1        1      0          6 |
                 |------------------------------------|
              3. |       1        0      1          5 |
              4. |       1        0      0          4 |
                 |------------------------------------|
              5. |       0        1      1          3 |
              6. |       0        1      0          2 |
                 |------------------------------------|
              7. |       0        0      1          1 |
              8. |       0        0      0          0 |
                 +------------------------------------+
            It sounds as if all 8 possibilities don't occur, but you can also visit

            https://www.stata-journal.com/articl...article=dm0034

            and then consider e.g.

            Code:
            egen concat = concat(private public home)
            egen which = group(concat), label

            Comment


            • #7
              Hello,

              I ran the code but there is no observation for home '0'. I checked the raw data and there are observations that have reported only home and no visits to public or private facilities. The code (dataex) also shows observations with 1 in one variable (home) and 0 in others. Why the coding has eliminated those observations?

              Aamna

              Comment


              • #8
                home yes, private no, public no should be 1.

                home no, private no, public no should be 0. May not exist!

                See observations #7 and #8 in post #6.

                The coding eliminates or ignores nothing so long as you have 0 or 1 in all three variables.

                Comment


                • #9
                  Hello,

                  Unfortunately 'home yes, private no, public no' is coming as 0, and due to this is not showing any observation under the 'home'. I am not sure what I am doing wrong as I am following our codes. Here are the results from the tabulation. There is no home category in the table/results. I am not sure where I am going wrong.

                  Aamna


                  HTML Code:
                  tab ownership, mi
                  
                              ownership |      Freq.     Percent        Cum.
                  ----------------------+-----------------------------------
                            public only |      
                        public and home |        
                           private only |      
                     private and public |      
                  private, public, home |      
                                      . |    
                  ----------------------+-----------------------------------
                                  Total |

                  Comment


                  • #10
                    I don’t have suggestions based on your latest. The test example in #6 deliberately spanned all the logical possibilities. If you think you’re getting different results you need to show data examples and the exact code you used.

                    Comment

                    Working...
                    X