Announcement

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

  • Panel data regression for each id

    Hi, I have a sample (in long format) of 52 funds (id 1-52) with monthly returns (return), i also have market returns (market) and a time-variable (time from 2007m01 to 2016m12). I have managed to tell Stata that Im working with panel data with the command:
    Code:
     xtset id time
    .

    What i want to do is separate regressions for each id en two separate time periods, from 2007m1 to 2011m12 and from 2012m01 to 2016m12.


    Im thinking about two loops that intuitively looks something like this:

    Code:
    foreach id & time<2012m01
    
    xtreg return market
    and

    Code:
    foreach id & time>2011m12
    
    xtreg return market
    I would be very thankfull if someone could help me out here.



    Olle






    Attached Files

  • #2
    I would start with creating an indicator variable for your two periods.
    Code:
    gen period = time >= tm(2012m1)
    Then you could indeed use a loop. What you intended would be something like this.
    Code:
    levelsof id, local(ids)
    foreach id of local ids {
        forval i = 0/1 {
            reg return market if id==`id' & period==`i'
        }
    }
    However, there are much simpler ways to do this, for example:
    Code:
    bys id period: reg return market
    Note that if you're running a regression for each id, they are essentially time-series instead of panel data so you won't need to use xtreg.

    Comment


    • #3
      Thank you for the help, when i run your code for creating periods, Im only getting 0 for all observations. Any advice how to solve that?

      Comment


      • #4
        It's hard to give advice without knowing what your time variable looks like exactly. It would be helpful if you could give an example of your data using dataex.

        If you're running Stata 14.2 or higher it should be already included, otherwise you can install it by typing ssc install dataex.

        Comment


        • #5
          Here is a dataex for my data.

          label values time1 time1
          label def time1 1 "2007m01", modify
          label def time1 2 "2007m02", modify
          label def time1 3 "2007m03", modify
          label def time1 4 "2007m04", modify
          label def time1 5 "2007m05", modify
          label def time1 6 "2007m06", modify
          label def time1 7 "2007m07", modify
          label def time1 8 "2007m08", modify
          label def time1 9 "2007m09", modify
          label def time1 10 "2007m10", modify

          Comment


          • #6
            Your time1 variable is in a non-standard format. I would convert it Stata's datetime format because it allows for all calculations and transformations that help datetime offers.
            Code:
            decode time1, gen(time2)
            replace time2 = monthly(time2, "YM")

            Comment


            • #7
              Im getting type mismatch when trying the second part of your code. My time1 is in format long and time2 str7


              Code:
              describe time1
              storage display value
              variable name type format label variable label

              time1 long %8.0g time1 time

              Code:
              describe time2
              storage display value
              variable name type format label variable label

              time2 str7 %9s time


              Any ideas how to solve this?

              Comment


              • #8
                My bad, I forgot you can't replace with monthly(). This should work.
                Code:
                decode time1, gen(time2)
                gen time3 = monthly(time2, "YM")
                format time3 %tm

                Comment


                • #9
                  Originally posted by Wouter Wakker View Post
                  My bad, I forgot you can't replace with monthly(). This should work.
                  Code:
                  decode time1, gen(time2)
                  gen time3 = monthly(time2, "YM")
                  format time3 %tm
                  Thank you for your help!

                  Comment


                  • #10
                    Hi i did what was mentioned in this thread and i have a question obout outreg2. Using the "bys" command i am running 1500 individual regressions, if i use outrag2 it only gives me the output for the last of the 1500 funds how can i get the results for all of them in word? Thanks for your time.

                    Comment

                    Working...
                    X