Announcement

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

  • Carry forward observations in panel-data

    Hi,

    I am working with the V-Party dataset, where the variable v2pagovsup shows if a party (v2paid) was in government or opposition at a specific point in time. However, this data is only given for the first date in the electoral cycle – the rest of the time there are no observations. I would like to carry forward the last known observation on v2pagovsup for each party so that it covers the whole electoral cycle.

    I have tried doing this using the following code

    Code:
    * Sort the data by party and year
    sort v2paid year historical_date
    
    * Declare data as time series
    tsset v2paid year
    
    * Fill in missing observations for each party and each year
    fillin v2paid year
    
    * Carry the last known value of v2pagovsup forward for each party
    bysort v2paid (year): replace v2pagovsup = v2pagovsup[_n-1] if missing(v2pagovsup)
    The code runs without problems and the number of observations on v2pagovsup increases. However, most years still lack observations.

    Does anyone have any ideas on what might be wrong?

    Thanks in advance.

  • #2
    I am not familiar with this dataset. Anybody sharing this ignorance might want to see a data example exposing your problem.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      I am not familiar with this dataset. Anybody sharing this ignorance might want to see a data example exposing your problem.
      Sure! Here is an example of how it may look.
      Code:
      +---------+------+------------------+--------------+
      |  v2paid | year | historical_date  | v2pagovsup   |
      +---------+------+------------------+--------------+
      | Party1  | 2014 | 26oct2014        | 1            |
      | Party1  | 2015 |                  |              | 
      | Party1  | 2016 |                  |              | 
      | Party1  | 2017 |                  |              | 
      | Party1  | 2018 | 24oct2018        | 0            |
      +---------+------+------------------+--------------+

      Comment


      • #4
        I interpret your data example as equivalent to

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str9 v2paid int year str18 historical_date byte v2pagovsup
        "Party1" 2014 "26oct2014" 1
        "Party1" 2015 ""          .
        "Party1" 2016 ""          .
        "Party1" 2017 ""          .
        "Party1" 2018 "24oct2018" 0
        end
        (please use dataex as here) and I can't see why that code wouldn't work.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          I interpret your data example as equivalent to

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str9 v2paid int year str18 historical_date byte v2pagovsup
          "Party1" 2014 "26oct2014" 1
          "Party1" 2015 "" .
          "Party1" 2016 "" .
          "Party1" 2017 "" .
          "Party1" 2018 "24oct2018" 0
          end
          (please use dataex as here) and I can't see why that code wouldn't work.
          Thanks for your advice!

          Turns out that everything works just as intended, the mistake was in how I verified its correctness.

          Comment


          • #6
            Thanks for closure.

            Comment

            Working...
            X