Announcement

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

  • Help with creating a variable with lagged values

    Hey guys,

    I am currently a final year student and i am writing my Economics dissertation on Oil prices. I am trying to mathematically create the command for the variable (Net Oil price increase) shown below. OPt is the current oil price variable and OPt-1...OPt-6 is it's lagged values. Your help would be much appreciated!

    𝑁𝑂𝑃𝐼𝑑 = max{0, π‘œπ‘π‘‘ βˆ’ max{π‘œπ‘π‘‘βˆ’1, π‘œπ‘π‘‘βˆ’2, π‘œπ‘π‘‘βˆ’3, π‘œπ‘π‘‘βˆ’4, π‘œπ‘π‘‘βˆ’5, π‘œπ‘π‘‘βˆ’6}
    Last edited by Ismayil Ismayilov; 19 Jan 2019, 10:43. Reason: VAR, Lagged Values

  • #2
    Try something like this:

    Code:
    sort date
    
    forvalues i = 1/6 {
    gen optlagged`i' =  opt[_n-`i']
    }
    
    gen zero = 0
    
    egen maxtemp = rowmax(optlagged?)
    
    gen optminmaxtemp = opt - maxtemp
    
    egen nopi = rowmax(zero optminmaxtemp)

    Comment


    • #3
      So, I take it you have time series data here And presumably you have a variable that records time--let's assume it's called time.

      Code:
      tsset time
      gen nopl = max(0, OPt - max(L1.OPt, L2.OPt, L3.OPt, L4.OPt, L5.OPt, L6.OPt))
      should do it.

      Read -help tsset- and -help tsvarlist- for more information.

      In the future, when requesting help with code, show example data. The code above is untested because there was no example data to work with, so it may contain typos or other errors. The useful way to show example data is by using the -dataex- command. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

      When asking for help with code, always show example data. When showing example data, always use -dataex-.

      Added: Crossed with #2, offering a different solution.

      Comment


      • #4
        Thanks so much for your help! Appreciate your tips for the future

        Comment


        • #5
          We all can agree that the code in #2 is a monstrosity compared to Clyde's solution

          I would just add that if you want to avoid time series operators, Clyde's code also works without time series operators:

          Code:
          sort date 
            gen nopl = max(0, OPt - max(OPt[_n-1], OPt[_n-2], OPt[_n-3], OPt[_n-4], OPt[_n-5], OPt[_n-6]))

          Comment


          • #6
            Clyde's code also works without time series operators:
            Well, maybe. Only if there are no gaps in the time series.

            Comment

            Working...
            X