Announcement

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

  • tsfill for unbalanced panel data

    Hello,
    I have a unbalanced panel data set of companies for which the monthly stock holdings data are available for a specific time period (say from 01/01/2003 - 01/06/2003). The data set looks like this.
    Company stock date qty price
    A 1 01/01/2003 4 25
    A 1 01/02/2003 5 30
    A 1 01/04/2003 2 22
    A 1 01/06/2003 6 31
    A 2 01/01/2003 2 2
    A 2 01/03/2003 4 4
    A 2 01/05/2003 8 3
    A 2 01/06/2003 2 5
    B 1 01/01/2003 1 45
    B 1 01/04/2003 6 42
    B 1 01/05/2003 8 41
    B 3 01/02/2003 21 13
    B 3 01/05/2003 10 11
    B 3 01/06/2003 8 21
    I want want to fill the gaps in the dates to make it consecutive from 01/01/2003 to 01/06/2003 so the data should finally look like this.
    Company stock date qty price
    A 1 01/01/2003 4 25
    A 1 01/02/2003 5 30
    A 1 01/03/2003
    A 1 01/04/2003 2 22
    A 1 01/05/2003
    A 1 01/06/2003 6 31
    A 2 01/01/2003 2 2
    A 2 01/02/2003
    A 2 01/03/2003 4 4
    A 2 01/04/2003
    A 2 01/05/2003 8 3
    A 2 01/06/2003 2 5
    B 1 01/01/2003 1 45
    B 1 01/02/2003
    B 1 01/03/2003
    B 1 01/04/2003 6 42
    B 1 01/05/2003 8 41
    B 1 01/06/2003
    B 3 01/01/2003
    B 3 01/02/2003 21 13
    B 3 01/03/2003
    B 3 01/04/2003
    B 3 01/05/2003 10 11
    B 3 01/06/2003 8 21
    I am unsure as to how I should use tsfill in this scenario (Is by group possible?). Any advice is highly appreciated

    Thank you
    Last edited by Yapa Bandara; 22 Sep 2016, 00:17.

  • #2
    See help tsfill
    First you have to declare your data to be panel.
    In your specific case : what is your ID variable? Is it the pair of (company stock)? I assume it is
    Code:
    egen ID=group(Company stock)
    xtset ID date
    tsfill, full
    tsfill doesn't need a bygroup subcommand, because it uses the ID (groups) you're panel is based on.
    Best,

    Comment


    • #3
      Charlie gives good advice but there remains a substantive question of what is best for analysis.

      Note that the consequence of an xtset in this way is that (company A, stock 1) and (company A, stock 2) are just as different as (company A, stock 1) and (company B, stock 2). All Stata knows in either case is that they are different panels.

      Comment


      • #4
        Dear Charlie and Nick,
        Thank you very much for your responses. I tried Charlie's method. My data has a monthly frequency and when I try to xtset, it identifies the data with one day gap. Hence tsfill command fills the gaps on a daily basis. However I need just monthly fillings. Does that mean I have to change the format of the date variable?
        Nick, thank you for your valuable advice, and yes it is not a problem for observations to be identified uniquely as you specified as I take them separately for further analysis.
        Thank you
        Yapa

        Comment


        • #5
          Nick's right, however my answer was only to give the OP a solution to run tsfill with his data.
          It doesn't mean I recommend this panel to run an analysis afterward.
          With the above specified panel, a possibility would be to use vce(cluster Company) to allow for intra-group correlations.

          Comment


          • #6
            Try :
            Code:
             
             xtset ID date, monthly

            Comment


            • #7
              It works! Thank you Charlie. To mention again, these data aren't directly used for analysis, rather these companies will be treated separately to construct another variable. Thank you Nick and Charlie again for your time and concern in this regard. Appreciate a lot!!

              Comment


              • #8
                Good practice to create a monthly date. Here is a reproducible example:

                Code:
                clear 
                input str1 Company    stock str10 sdate    qty    price
                A    1    "01/01/2003"    4    25
                A    1    "01/02/2003"    5    30
                A    1    "01/04/2003"    2    22
                A    1    "01/06/2003"    6    31
                A    2    "01/01/2003"    2    2
                A    2    "01/03/2003"    4    4
                A    2    "01/05/2003"    8    3
                A    2    "01/06/2003"    2    5
                B    1    "01/01/2003"    1    45
                B    1    "01/04/2003"    6    42
                B    1    "01/05/2003"    8    41
                B    3    "01/02/2003"    21    13
                B    3    "01/05/2003"    10    11
                B    3    "01/06/2003"    8    21
                end 
                gen date = daily(sdate, "DMY") 
                gen mdate = mofd(date) 
                egen group = group(Company stock), label 
                xtset group mdate

                Comment


                • #9
                  Thank you very much Nick

                  Comment

                  Working...
                  X