Announcement

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

  • Conducting panel analysis using a subset of data

    Dear Statalist

    I have a question regarding panel analysis using a subset of panel.

    I have a balanced panel data from 2004 to 2015, with string variables including "incomelevel" and "region".
    The variable "incomelevel" has two values ("Least Developed Countries", "Low Middle Income Countries"),
    while variable "region" has five different values ("Far East Asia", "South America", etc.).

    I already did a panel analysis using the whole data after defining the panel data as below.

    Code:
    xtset id year
           panel variable:  id (strongly balanced)
            time variable:  year, 2004 to 2015
                    delta:  1 unit
    Now I want to do the same analysis using a subset of the panel, according to the region and income level of a panel.

    For example, I want to conduct a same analysis using the countries whose "incomelevel" is "Least Developed Countries".
    Because I want to see the result depending on the different income levels and regions, I will have to do the same thing for 7 times.
    (2 times with different income level, 5 times with different regions)
    What will be the best way to this?

    Any advice will be a great help for me.
    Thank you!

  • #2
    Jae:
    you may want to try (I assume that you'll use -re- specification):
    Code:
    bysort incomelevel: xtreg <depvar> <indepvars>
    and

    Code:
    bysort region: xtreg <depvar> <indepvars>
    As an aside, It's a good practice to keep -string- variables at minimum.

    Last edited by Carlo Lazzaro; 05 Jul 2018, 09:34. Reason: Errors in codes: semicolon instead of colon after -incomelevel- and -region-. I should change my spectacles.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      The simplest case is if your analysis consists of a single command that supports the -by:- prefix, e.g. -regress dep_var ind_vars-. Then it's just:

      Code:
      by incomelevel, sort: regress dep_var ind_vars
      by region, sort: regress dep_var ind_vars
      If your analysis is a series of commands, or if it is a single command that does not support the -by:- prefix, then it's a little more complicated. You have to wrap those commands inside a program and then iterate that program under -runby-

      Code:
      capture program drop my_program
      program define my_program
          insert the analysis command(s) here
          exit
      end
      
      runby my_program, by(incomelevel)
      runby my_program, by(region)
      Note: -runby- is written by Robert Picard and me, and is available from SSC.

      Comment


      • #4
        Thank you so much, Carlo & Clyde!
        Because my analysis is single command, I tried the bysort and it worked. :-)

        I have an additional question here.
        Can I save the estimation result by each income level?
        I tried like this:

        Code:
        bysort incomelevel: xtreg Q X1 X2 X3, fe
        estimate store fe_income
        esttab fe_income
        Although the bysort command showed the result of both "Least Developed Countries" and "Low Middle Income Countries",
        it seems the estimate store command only saves the last one.
        Is there any way to save the each result and compare the coefficients with the star marks?
        What I can think of is a code as below, but I guess I need to save the each result separately.

        Code:
        esttab fe_income1 fe_income2
        Thank you for your help.

        Comment


        • #5
          I would try something like:


          Code:
          foreach i in incomelevel {
          eststo: xtreg Q X1 X2 X3, fe if incomelevel==`i'
          }
          esttab

          Comment


          • #6
            The -if- qualifier in Chinmay's interesting code should be moved before the comma.
            Last edited by Carlo Lazzaro; 06 Jul 2018, 08:55.
            Kind regards,
            Carlo
            (StataNow 18.5)

            Comment


            • #7
              Thank you, Chinmay and Carlo!

              This is what I tried based on your advice, but I got an error message.

              Code:
               foreach i in incomelevel {
                2. eststo: xtreg Q X1 X2 X3 if incomelevel=='i', fe 
                3. }
              'i' invalid name
              r(198);
              I tried again after encoding "incomelevel" from string to double, but it did not work either.
              How can I solve this?

              Comment


              • #8
                when referring to a local, the first character should be the left facing apostrophe - on my keyboard, this is on the same key as the tilde in the upper left; compare these:
                Code:
                `i'
                'i'
                where the first is correct and the second is, I think, what you did

                Comment


                • #9
                  Thank you, Rich.
                  I fixed the apostrophe and got a result from Stata, but it gave me a result of the all observations as a whole, without distinguishing the two income levels.
                  But I managed to settle down with a code below, thanks to all of you;

                  Code:
                  eststo fe_ldc: xtreg Q X1 X2 X3 if Income_Level==1, fe
                  eststo fe_lmic: xtreg Q X1 X2 X3 if Income_Level==2, fe
                  esttab fe_ldc fe_lmic
                  Although I needed to write a few more lines in this way, I got the result that I wanted.
                  Thank you all.

                  Comment

                  Working...
                  X