Announcement

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

  • how to display (tab) values only if y=0 or y=1 in all 13 waves

    Hi,

    I have a panel dataset with 13 waves, where the identifier is hhid (household ID). My dependent variable is saving1 (binary, which takes either 1 or 0).

    When I run a Hausman test for xtlogit RE vs FE, I get the error message:
    model fitted on these data fails to meet the asymptotic assumptions of the Hausman test;


    I believe this is because when I run the xtlogit, fe model, I get this
    note: multiple positive outcomes within groups encountered.
    note: 60 groups (767 obs) dropped because of all positive or all negative outcomes.

    Anyway, I wanted to show that running a FE model would drop many values for which saving1 is time-invariant.
    E.g. as can be seen in the screenshot below, for hhid=6, saving1 varies over time, whereas for hhid=21, saving1=0 for all 13 waves.



    So I wanted to display
    Code:
    tab hhid saving1
    but only if saving1==0 or if saving1==1 for all 13 waves.

    Something like the following (although I know my attempt is incorrect):
    Code:
    tab hhid year if saving1==0 or saving1==1 for nyear==13
    Would you be able to suggest the code for this please?

  • #2
    See How do I list observations in a group that differ on a variable?

    http://www.stata.com/support/faqs/da...ions-in-group/

    and don't be misled by the title, as it continues to discuss identifying homogeneous groups.

    Or http://www.stata.com/support/faqs/da...ble-recording/

    noting the correspondence for 0, 1 variables:

    all are 1 implies that the minimum is 1

    all are 0 implies that the maximum is 0.

    Code:
     
    bysort hhid (saving) : gen constant = saving[1] == saving[_N] 
    
    
    egen max = max(saving), by(hhid) 
    egen min = min(saving), by(hhid) 
    
    gen constant2 = (max == 0) | (min == 1)
    Last edited by Nick Cox; 27 Feb 2017, 12:35.

    Comment


    • #3
      That is very helpful - thank you (and for the links)

      I now have the output below:



      I wondered, what the difference is between constant and constant2? Is it that constant2 takes into account that saving1 is between [0,1], so it is "smart enough to ignore" missing values, and hence constant2 should be used over constant?

      Many thanks

      Comment


      • #4
        I have just noticed that constant only shows the hhid for which saving1==0 in all 13 waves or saving1==1 in all 13 waves, as requested.




        Q: Why does the Total column for constant2 show 11 or 12, and not just 13?



        If this helps, I only had 29 missing values for saving1:


        Thank you

        Comment


        • #5
          You have posted several images in the last two questions that are unreadable by me and perhaps others. Please review FAQ Advice #12 which does explain.

          Nothing was said about missing values. If you want the code to ignore missing values, that is a related but different problem. You need to tell us what your rules are.

          Comment


          • #6
            Apologies, I tried to use dataex earlier but had issues with it. Here is what I posted above in what is hopefully a more readable format:

            I now have the output below:
            Code:
            bysort hhid (saving) : gen constant = saving[1] == saving[_N]
            egen max = max(saving), by(hhid)
            egen min = min(saving), by(hhid)
            gen constant2 = (max == 0) | (min == 1)
            
            tab constant
            
            constant |      Freq.     Percent     Cum.
            ------------+-----------------------------------
                      0 |      2,431       78.57       78.57
                      1 |        663       21.43      100.00
            ------------+-----------------------------------
                  Total |      3,094      100.00
            
            
            tab constant2
            
            constant2 |      Freq.     Percent        Cum.
            ------------+-----------------------------------
                      0 |      2,314       74.79       74.79
                      1 |        780       25.21      100.00
            ------------+-----------------------------------
                  Total |      3,094      100.00
            
            
            tab hhid saving if constant==1
            
            
                       |   household income
                       | exceeded spending in
                       |    past 12 months,
                       |    excluding major
                       |     purchases
                  hhid |        No        Yes |     Total
            -----------+----------------------+----------
                    21 |        13          0 |        13
                   175 |        13          0 |        13
                   403 |        13          0 |        13
                   844 |        13          0 |        13
                  1019 |        13          0 |        13
                  1197 |        13          0 |        13
                  1202 |        13          0 |        13
                  1217 |        13          0 |        13
                  1242 |        13          0 |        13
                  1337 |        13          0 |        13
                  1952 |        13          0 |        13
                  2200 |        13          0 |        13
                  2471 |        13          0 |        13
                  2597 |         0         13 |        13
                  3096 |         0         13 |        13
                  3153 |        13          0 |        13
                  4197 |        13          0 |        13
                  7514 |        13          0 |        13
                 10241 |         0         13 |        13
                 10764 |         0         13 |        13
                 11846 |         0         13 |        13
                 12103 |        13          0 |        13
                 12467 |        13          0 |        13
                 12471 |        13          0 |        13
                 12516 |        13          0 |        13
                 12522 |         0         13 |        13
                 12650 |        13          0 |        13
                 12660 |        13          0 |        13
                 12743 |        13          0 |        13
                 12913 |        13          0 |        13
                 12964 |        13          0 |        13
                 12992 |         0         13 |        13
                 13075 |         0         13 |        13
                 13081 |         0         13 |        13
                 13199 |        13          0 |        13
                 13202 |         0         13 |        13
                 13214 |        13          0 |        13
                 13352 |         0         13 |        13
                 13411 |         0         13 |        13
                 13507 |        13          0 |        13
                 13527 |        13          0 |        13
                 13619 |        13          0 |        13
                 13761 |        13          0 |        13
                 13808 |         0         13 |        13
                 24446 |         0         13 |        13
                 28875 |        13          0 |        13
                 34154 |        13          0 |        13
                 44494 |         0         13 |        13
                 47145 |         0         13 |        13
                 64763 |        13          0 |        13
                 77729 |        13          0 |        13
            -----------+----------------------+----------
                 Total |       455        208 |       663
            
            tab hhid saving if constant2==1
            
                       |   household income
                       | exceeded spending in
                       |    past 12 months,
                       |    excluding major
                       |       purchases
                hhid |        No        Yes |     Total
            -----------+----------------------+----------
                    21 |        13          0 |        13
                   175 |        13          0 |        13
                   403 |        13          0 |        13
                   733 |        12          0 |        12
                   844 |        13          0 |        13
                  1019 |        13          0 |        13
                  1197 |        13          0 |        13
                  1202 |        13          0 |        13
                  1217 |        13          0 |        13
                  1242 |        13          0 |        13
                  1337 |        13          0 |        13
                  1952 |        13          0 |        13
                  2200 |        13          0 |        13
                  2471 |        13          0 |        13
                  2597 |         0         13 |        13
                  3096 |         0         13 |        13
                  3153 |        13          0 |        13
                  4197 |        13          0 |        13
                  7514 |        13          0 |        13
                 10241 |         0         13 |        13
                 10764 |         0         13 |        13
                 11318 |        11          0 |        11
                 11583 |        12          0 |        12
                 11846 |         0         13 |        13
                 12103 |        13          0 |        13
                 12467 |        13          0 |        13
                 12471 |        13          0 |        13
                 12516 |        13          0 |        13
                 12522 |         0         13 |        13
                 12650 |        13          0 |        13
                 12660 |        13          0 |        13
                 12743 |        13          0 |        13
                 12913 |        13          0 |        13
                 12964 |        13          0 |        13
                 12992 |         0         13 |        13
                 13075 |         0         13 |        13
                 13081 |         0         13 |        13
                 13199 |        13          0 |        13
                 13202 |         0         13 |        13
                 13214 |        13          0 |        13
                 13273 |        12          0 |        12
                 13352 |         0         13 |        13
                 13411 |         0         13 |        13
                 13507 |        13          0 |        13
                 13527 |        13          0 |        13
                 13619 |        13          0 |        13
                 13734 |        12          0 |        12
                 13761 |        13          0 |        13
                 13808 |         0         13 |        13
                 14248 |        12          0 |        12
                 24446 |         0         13 |        13
                 28875 |        13          0 |        13
                 31385 |        12          0 |        12
                 34154 |        13          0 |        13
                 35335 |        12          0 |        12
                 44494 |         0         13 |        13
                 46127 |        12          0 |        12
                 47145 |         0         13 |        13
                 64763 |        13          0 |        13
                 77729 |        13          0 |        13
            -----------+----------------------+----------
                 Total |       562        208 |       770
            
            codebook saving
            ------------------------------------------------------------------------------------------------------
            saving                 household income exceeded spending in past 12 months, excluding major purchases
            ------------------------------------------------------------------------------------------------------
            
                              type:  numeric (float)
                             label:  saving
            
                             range:  [0,1]                        units:  1
                     unique values:  2                        missing .:  29/3,094
            
                        tabulation:  Freq.   Numeric  Label
                                     1,810         0  No
                                     1,255         1  Yes
                                        29         .

            I would not like the code to ignore missing values, sorry for being unclear. I was wondering whether this was why the output for tab constant and tab constant2 were different.

            constant only shows the hhid for which saving1==0 in all 13 waves or saving1==1 in all 13 waves, as requested.
            Why does the Total column for constant2 show 11 or 12, and not just 13?

            Comment


            • #7
              The second solution looks for panels in which the variable is constant. If you have a mix of (0 or 1) and missing then it won't find panels in which there are some missings even if all the known values are 0 OR all known values are 1. If you have all missings, then such panels are identified too.

              The first solution hinges on the egen functions max() and min() which ignore missings as far as possible, so will only return missing if all values are missing. This behaviour is like that of summarize.

              You could use egen's total() function too, which would yield 0, 13 or something else.

              Comment


              • #8
                I understand the difference now, thank you very much for your help

                Comment

                Working...
                X