Announcement

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

  • Time Series Regressions for each Cross-Sectional Unit in an Unbalanced Panel Dataset

    Hi,

    I have an unbalanced panel dataset with 1696 cross-sectional units and 16 time points (years). I want to run a simple bivariate time series regression for each cross-sectional unit and save the coefficient of the independent variable for all 1696 cross sectional regressions. Below is the regression I would like to run for each cross-sectional unit.

    Code:
    reg D.Cash L.D.Cash
    where Cash represents cash holdings of a firm in a given year.

    It is also important to note that since my dataset is unbalanced, the number of time points available for each cross-sectional regression may not be the same due to missing observations.

    Any help in this regard is highly appreciated.

  • #2
    Install runby by Robert Picard and Clyde Schechter from SSC, then

    Code:
    program tsreg
    tsset year
    reg D.Cash L.D.Cash
    gen coef=_b[L.D.Cash]
    keep in 1
    end
     
    runby tsreg, by(firm)
    where the time variable above is named "year" and the panel identifier is "firm".

    Comment


    • #3
      Thanks a lot Andrew Musau! This works well for me. Further, I would like to run this regression only for those cross-sectional units which have a minimum number of observations (say, 8) across time. What changes do I need to make in the command?

      Thanks and Regards
      Prateek

      Comment


      • #4
        You are using the difference and the lag operators, so the question is when you state "a minimum of 8 observations", do you mean 8 consecutive observations or just 8 total? I guess the former makes sense. Note that 8 consecutive observations translates to 6 sample observations because of differencing and lagging.

        Code:
        program tsreg2
        tsset year
        reg D.Cash L.D.Cash
        bys firm: egen count= count(year) if e(sample)
        bys firm: egen total= max(count)
        gen coef=cond(total>=6, _b[L.D.Cash], .)
        drop count total
        keep in 1
        end
        
        runby tsreg2, by(firm)
        You could also just run the code in #2 and generate a variable that counts how many observations are used in a regression, then decide on what to do afterwards. Add the lines

        Code:
        program tsreg
        tsset year
        reg D.Cash L.D.Cash
        gen coef=_b[L.D.Cash]
        bys firm: egen count= count(year) if e(sample)
        bys firm: egen sample_obs= max(count)
        drop count
        keep in 1
        end
        
        runby tsreg, by(firm)
        Last edited by Andrew Musau; 11 Oct 2019, 08:46.

        Comment


        • #5
          Thanks a lot, Andrew Musau for your brilliant guidance and perfect commands. It has been very informative and helpful indeed. Really appreciate your help. I now humbly request you to guide me for developing programming skills in Stata. I am completely new to coding and hence I would like to know how should I go about building foundations for programming in Stata step by step.

          Thanks once again!

          Comment


          • #6
            Stata has really good documentation which you should study. By typing -help command name- you can find links to the PDF manuals.Personally, I have benefited immensely by examining the codes of very experienced users in the forum, e.g., Nick Cox, Clyde Schechter, Robert Picard, etc. But ultimately, I believe you learn by doing - so it usually comes with experience. Participating in the forum is a good way to gain experience, for example, by attempting to answer questions posed - as they come in varying degrees of difficulty - and you certainly should be able to handle some of them. I do not consider myself an expert, I am still learning as you are but maybe I have had more time doing it.

            Comment

            Working...
            X