Announcement

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

  • computing a running slope in panel data

    I am using panel data in which a series of entities have varying values for the variable "q". I would like to compute a new variable "slope" equal to the running value of the slope of the values of "q" across n prior observations.

    The data look like this:

    entity #, observation # for entity, q

    1,1,-1
    1,2,1
    1,3,-3
    1,4,3
    1,5,2
    1,6,-2
    ...many more observations for entity 1...
    2,1,3
    2,2,-2
    2,3,4
    2,4,0
    2,5,-2
    2,6,1
    etc.

    So, for example, beginning in observation #4 for entity #1, I might have "slope" equal to the slope of the values for "q" across the three prior observations for that entity, values -1, 1 and -3 in, respectively, observations 1 through 3.

    Can you recommend code to accomplish this?

    Thank you!

  • #2
    This sounds like:

    Code:
    rangestat (reg) q observation_number, by(entity) interval(observation_number -3 -1)
    -rangestat- was written by Robert Picard, Niick Cox, and Roberto Ferrer, and is available from SSC. It is, in my opinion, one of the most useful user-written programs ever! Do read its help file for lots of well-explained examples of the many things you can use it for.

    In the future, please follow the advice in FAQ #12 and show example data by using the -dataex- command. (-dataex- is also written by Robert Picard and Nick Cox.) The kind of tabulation you show gives us the visual gist of your data, but it is not readily importable into Stata. By using -dataex- to show example data, you make it possible for those who want to help you to easily create a completely faithful replica of your situation in their own Stata and experiment on it with code.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      This sounds like:

      Code:
      rangestat (reg) q observation_number, by(entity) interval(observation_number -3 -1)
      -rangestat- was written by Robert Picard, Niick Cox, and Roberto Ferrer, and is available from SSC. It is, in my opinion, one of the most useful user-written programs ever! Do read its help file for lots of well-explained examples of the many things you can use it for.

      In the future, please follow the advice in FAQ #12 and show example data by using the -dataex- command. (-dataex- is also written by Robert Picard and Nick Cox.) The kind of tabulation you show gives us the visual gist of your data, but it is not readily importable into Stata. By using -dataex- to show example data, you make it possible for those who want to help you to easily create a completely faithful replica of your situation in their own Stata and experiment on it with code.
      Thanks! I will try this. Thanks also for the tip on data examples.

      Comment


      • #4
        Another way to do this is just a slope from first and last observations in a window. With rangestat (SSC) this could be something like

        Code:
        . webuse grunfeld
        
        . rangestat (first) first=mvalue (last) last=mvalue (count) count=mvalue , by(company) int(year -5 -1)
        
        . edit if company == 1
        
        . edit year mvalue first last count if company == 1
        and now slope could be say (last - first) ./ (count - 1)

        Comment

        Working...
        X