Announcement

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

  • rangestat question

    Hellow
    I have a question in rangestat
    why do I always get the result that never calculates?
    My mean is the result that is always the same with original

    I want to compute MA5
    rangestat (mean) v_debt v_roe v_Cdebt, interval(time -5 0) by(id time)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(time id v_debt v_roe v_Cdebt) double(v_debt_mean v_roe_mean v_Cdebt_mean)
    19328 1          .           .           .                    .                   .                   .
    19693 1  .18179545           .    .9389104   .18179544806480408                   .   .9389104247093201
    20058 1   .6492594   -.7089842    2.163649    .6492593884468079  -.7089841961860657   2.163649082183838
    20423 1  .04369804   -.7517949  .008138238  .043698038905858994  -.7517948746681213 .008138238452374935
    20606 1   .2233313   1.0564739    .3494784    .2233312875032425  1.0564738512039185  .34947842359542847
    20789 1 .028437067   2.5170796   .04255668  .028437066823244095  2.5170795917510986 .042556677013635635
    20971 1   .2298042  -.21633975     .353778   .22980420291423798 -.21633975207805634  .35377803444862366
    21154 1 -.04178981     .818469  -.28660178 -.041789811104536057   .8184689879417419  -.2866017818450928
    21336 1   .3777859  -.24989977    .7478976   .37778589129447937 -.24989977478981018   .7478975653648377
    21428 1   .4533736    .3821486    .4520029   .45337361097335815   .3821485936641693   .4520029127597809
    21519 1   -.326355 -.024748646   -.4122111   -.3263550102710724 -.02474864572286606  -.4122111201286316
    21609 1   .7289726   -.7508591     .622786    .7289725542068481  -.7508590817451477   .6227859854698181
    21701 1   .9563724   1.2806365    .8218676    .9563724398612976  1.2806365489959717   .8218675851821899
    21793 1   .2532618   .04117237    .4774139    .2532618045806885 .041172366589307785  .47741392254829407
    21884 1  -.4537844    .2989276  -.53304607  -.45378437638282776   .2989276051521301  -.5330460667610169
    21975 1 -.02242019   -.8486412 -.063347004 -.022420192137360573  -.8486412167549133 -.06334700435400009
    22067 1 -.07704715    .7840909  -.22435866  -.07704714685678482   .7840909361839294  -.2243586629629135
    22159 1  .07434082   .20955414   .29531816       .0743408203125  .20955413579940796   .2953181564807892
    22250 1 -.23197924    -.325961  -.26288265  -.23197923600673676 -.32596102356910706 -.26288264989852905
    22340 1 -.03853066   -.8265625  -.02780134 -.038530658930540085  -.8265625238418579 -.02780134230852127
    end
    format %dCY/N/D time

  • #2
    You have daily dates all more than 5 days apart. So the means are just the original data. In fact doing calculations separately by(id time) enforces that too. by(id) is what you want, although it won't fix the first point.

    Comment


    • #3
      the result still is the same
      rangestat (mean) v_debt v_roe v_Cdebt, interval(time -5 0) by(id)

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(time id v_debt v_roe v_Cdebt) double(v_debt_mean v_roe_mean v_Cdebt_mean)
      19328 1          .           .           .                    .                   .                   .
      19693 1  .18179545           .    .9389104   .18179544806480408                   .   .9389104247093201
      20058 1   .6492594   -.7089842    2.163649    .6492593884468079  -.7089841961860657   2.163649082183838
      20423 1  .04369804   -.7517949  .008138238  .043698038905858994  -.7517948746681213 .008138238452374935
      20606 1   .2233313   1.0564739    .3494784    .2233312875032425  1.0564738512039185  .34947842359542847
      20789 1 .028437067   2.5170796   .04255668  .028437066823244095  2.5170795917510986 .042556677013635635
      20971 1   .2298042  -.21633975     .353778   .22980420291423798 -.21633975207805634  .35377803444862366
      21154 1 -.04178981     .818469  -.28660178 -.041789811104536057   .8184689879417419  -.2866017818450928
      21336 1   .3777859  -.24989977    .7478976   .37778589129447937 -.24989977478981018   .7478975653648377
      21428 1   .4533736    .3821486    .4520029   .45337361097335815   .3821485936641693   .4520029127597809
      21519 1   -.326355 -.024748646   -.4122111   -.3263550102710724 -.02474864572286606  -.4122111201286316
      21609 1   .7289726   -.7508591     .622786    .7289725542068481  -.7508590817451477   .6227859854698181
      21701 1   .9563724   1.2806365    .8218676    .9563724398612976  1.2806365489959717   .8218675851821899
      21793 1   .2532618   .04117237    .4774139    .2532618045806885 .041172366589307785  .47741392254829407
      21884 1  -.4537844    .2989276  -.53304607  -.45378437638282776   .2989276051521301  -.5330460667610169
      21975 1 -.02242019   -.8486412 -.063347004 -.022420192137360573  -.8486412167549133 -.06334700435400009
      22067 1 -.07704715    .7840909  -.22435866  -.07704714685678482   .7840909361839294  -.2243586629629135
      22159 1  .07434082   .20955414   .29531816       .0743408203125  .20955413579940796   .2953181564807892
      22250 1 -.23197924    -.325961  -.26288265  -.23197923600673676 -.32596102356910706 -.26288264989852905
      22340 1 -.03853066   -.8265625  -.02780134 -.038530658930540085  -.8265625238418579 -.02780134230852127
      end
      format %dCY/N/D time

      Comment


      • #4
        Indeed; I said that such a change will not fix the first point.

        You are asking for means across values on this and the previous 5 days. So for day 19693 that is the mean for daily dates 19688 to 19693. But there is only one observations in that window. And so on.

        If you want the mean of this observation and the previous 5 observations, you need something different, such as

        Code:
        bysort id (time) : gen seq = _n
        and then phrase your call to rangestat in terms of that variable.

        If that's not what you want, I think you need to explain what you want.

        (rangestat is from SSC, as you are asked to explain: FAQ Advice #12.)

        Comment


        • #5
          Yes, I want the previous 5 observations
          This is my original Code

          cd "D:\操盤準備\財務data"
          import delimited "All finance data.txt", encoding(Big5) clear
          //ren 年月 time

          egen id = group(簡稱)
          destring 負債總額 流動負債 負債比率 流動比率 roea稅後 roe綜合損益, replace force
          gen time = date(年月, "YM")

          format time %dCY/N/D replace
          order time

          sort id time
          by id: gen t = _n # I have use it

          order time t


          by id: gen v_debt = (負債總額/負債總額[_n-1])-1
          by id: gen v_roe = (roea稅後/roea稅後[_n-1])-1
          by id: gen v_Cdebt = (流動負債/流動負債[_n-1])-1


          rangestat (mean) v_debt v_roe v_Cdebt, interval(t -5 0) by(id) # However, the result is the same

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(time id v_debt v_roe v_Cdebt) double(v_debt_mean v_roe_mean v_Cdebt_mean)
          19328 1          .           .           .                   .                     .                    .
          19693 1  .18179545           .    .9389104  .18179544806480408                     .    .9389104247093201
          20058 1   .6492594   -.7089842    2.163649  .41552741825580597    -.7089841961860657    1.551279753446579
          20423 1  .04369804   -.7517949  .008138238   .2915842918058236    -.7303895354270935    1.036899248448511
          20606 1   .2233313   1.0564739    .3494784  .27452104073017836    -.1347684065500895    .8650440422352403
          20789 1 .028437067   2.5170796   .04255668   .2253042459487915     .5281935930252075    .7005465691909194
          20971 1   .2298042  -.21633975     .353778   .2260542387763659    .37928692400455477    .6427518134005368
          21154 1 -.04178981     .818469  -.28660178  .18879002891480923     .4524839346607526    .4384997789748013
          21336 1   .3777859  -.24989977    .7478976  .14354444605608782     .5289980048934618   .20254119283830127
          21428 1   .4533736    .3821486    .4520029  .21182370806733766     .7179885829488436   .27651863855620223
          21519 1   -.326355 -.024748646   -.4122111  .12020932510495186     .5377848334610462    .1495703812688589
          21609 1   .7289726   -.7508591     .622786  .23696523966888586  -.006871612121661504     .246275266011556
          21701 1   .9563724   1.2806365    .8218676   .3580599458267291    .24262443805734316     .324290191133817
          21793 1   .2532618   .04117237    .4774139   .4072352151075999     .1130750011652708   .45162614186604816
          21884 1  -.4537844    .2989276  -.53304607   .2686401704947154     .2045462311555942    .2381355365117391
          21975 1 -.02242019   -.8486412 -.063347004  .18934120330959558 -.0005854039142529169   .15224388365944228
          22067 1 -.07704715    .7840909  -.22435866   .2308925138786435      .134221193070213   .18355262652039528
          22159 1  .07434082   .20955414   .29531816  .12178722489625216     .2942900626609723    .1289746550222238
          22250 1 -.23197924    -.325961  -.26288265 -.07627138774842024   .026523800566792488 -.051817050824562706
          22340 1 -.03853066   -.8265625  -.02780134 -.12490346500029166   -.11809868117173512  -.13601959496736526
          end
          format %dCY/N/D time
          ------------------ copy up to and including the previous line ------------------

          Listed 20 out of 39449 observations

          Comment


          • #6
            OK, so the code in #4 applles, Use seq not time in your call to rangestat.

            Code:
             
             bysort id (time) : gen seq = _n 
            rangestat (mean) v_debt v_roe v_Cdebt, interval(seq -5 0) by(id)

            Comment


            • #7
              thanks very much
              Last edited by Curtis Chou; 18 Jun 2021, 05:38.

              Comment


              • #8
                Thanks, it really different

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float(time id v_debt v_roe v_Cdebt) double(v_debt_mean v_roe_mean v_Cdebt_mean)
                19328 1          .           .           .                   .                     .                    .
                19693 1  .18179545           .    .9389104  .18179544806480408                     .    .9389104247093201
                20058 1   .6492594   -.7089842    2.163649  .41552741825580597    -.7089841961860657    1.551279753446579
                20423 1  .04369804   -.7517949  .008138238   .2915842918058236    -.7303895354270935    1.036899248448511
                20606 1   .2233313   1.0564739    .3494784  .27452104073017836    -.1347684065500895    .8650440422352403
                20789 1 .028437067   2.5170796   .04255668   .2253042459487915     .5281935930252075    .7005465691909194
                20971 1   .2298042  -.21633975     .353778   .2260542387763659    .37928692400455477    .6427518134005368
                21154 1 -.04178981     .818469  -.28660178  .18879002891480923     .4524839346607526    .4384997789748013
                21336 1   .3777859  -.24989977    .7478976  .14354444605608782     .5289980048934618   .20254119283830127
                21428 1   .4533736    .3821486    .4520029  .21182370806733766     .7179885829488436   .27651863855620223
                21519 1   -.326355 -.024748646   -.4122111  .12020932510495186     .5377848334610462    .1495703812688589
                21609 1   .7289726   -.7508591     .622786  .23696523966888586  -.006871612121661504     .246275266011556
                21701 1   .9563724   1.2806365    .8218676   .3580599458267291    .24262443805734316     .324290191133817
                21793 1   .2532618   .04117237    .4774139   .4072352151075999     .1130750011652708   .45162614186604816
                21884 1  -.4537844    .2989276  -.53304607   .2686401704947154     .2045462311555942    .2381355365117391
                21975 1 -.02242019   -.8486412 -.063347004  .18934120330959558 -.0005854039142529169   .15224388365944228
                22067 1 -.07704715    .7840909  -.22435866   .2308925138786435      .134221193070213   .18355262652039528
                22159 1  .07434082   .20955414   .29531816  .12178722489625216     .2942900626609723    .1289746550222238
                22250 1 -.23197924    -.325961  -.26288265 -.07627138774842024   .026523800566792488 -.051817050824562706
                22340 1 -.03853066   -.8265625  -.02780134 -.12490346500029166   -.11809868117173512  -.13601959496736526
                end
                format %dCY/N/D time

                Comment

                Working...
                X