Announcement

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

  • #16
    You're referring to code Clyde posted in #5 and #6 in 2015 -- more than 2 years ago.

    See e.g. #14 for a terse pointer to tsegen and rangestat as methods for solving this more concisely. Both must be installed from SSC.

    Code:
    rangestat (sd) CFTA, interval(fyear -10 -1) by(gvkey)
    is indicative code

    Comment


    • #17
      Without an example of data that reproduces the problem you are encountering, it is not possible to say what is going wrong. This is pretty venerable code that has worked well in many situations. Most likely there is some anomaly in your data, perhaps missing values in the data, or insufficient numbers of lags are the problem. It's really impossible to say.

      In any case, there are better ways to do this now. Robert Picard, Nick Cox & Roberto Ferrer's -rangestat- command, available from SSC, handles this kind of calculation much more efficiently, and in just a single line of code now.

      Code:
      rangestat (count) running_n = CFTA (mean) running_mean = CFTA (sd) sigma = CFTA, by(gvkey) interval(fyear -10 -1)
      will give you the number of non-missing values of CFTA, the running mean and the standard deviation for the preceding 10 fiscal years within each firm.

      Added: Surprisingly, in light of the elapsed time between timestamps, this crossed with #16 which gives the same advice.

      Comment


      • #18
        Thank you both for your help! the command is very nice. But the problem is still there. the command works and generates but all generated values are missing values. I realize I dont have many values for resid, but the same problem occurs when I tried to use lags (L.) or the restat command with another variable that has only very few missing values.
        The data must be in a wrong format or something, I am really puzzled.

        I attach an example of the data set, I use Stata 12. The code I use is from this thread:


        xtset id year
        gen resid = .

        forvalues j = 1/`=_N' {
        capture noisily {
        reg TCA b1 b2 b3 b4 b5 if SIC== SIC[`j'] & year == year[`j'] & _n != `j'
        predict uhat in `j', resid
        replace resid = uhat in `j'
        drop uhat
        }
        }


        rangestat (count) running_n = resid (mean) running_mean = resid (sd) sigma = resid, by(gvkey) interval(year -4 -1)
        Attached Files

        Comment


        • #19
          Please read the FAQ for advice about how to show example data. Some of us, myself included, will not download anything from strangers--it is just too risky. The -dataex- command, by Robert Picard,is available from SSC. Install it if you do not already have it, and then read -help dataex- for the simple instructions. Then use it to post an example of your data the reproduces your problem.

          Comment


          • #20
            Alright, thanks for the reminder. My data looks the following:


            dataex if year >2005, c(50)

            ----------------------- copy starting from the next line -----------------------
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input int(id year) double(TCA b1 b2 b3 b4 b5) byte SIC float SICgroups
             2 2006  -.107885423139058  .0699989018961194  .0992916614887536   .100749063670412    34729693.795995 .1475083882192121 30 1
             3 2007  .0137328339575531  .0992916614887536   .100749063670412  .0581129611492001  2134014.869888476 .1518102372034956 30 1
             4 2008 -.0175209489607139   .100749063670412  .0581129611492001  .1434479498995628  8603932.584269663 .1678093372510611 30 1
             5 2009  .0196147938083422  .0581129611492001  .1434479498995628   .090702274975272 -2913948.929159802 .1748788845563039 30 1
             6 2010  .0444114737883284  .1434479498995628   .090702274975272  .0755028933590521  17739356.59760087 .1779426310583581 30 1
             7 2011  .0473959768531276   .090702274975272  .0755028933590521  .0878360154634541  17933087.59124088 .1902268760907504 30 1
             8 2012  .0099793221253259  .0755028933590521  .0878360154634541  .0592999190137677  17521286.59160696 .2033624022296143 30 1
             9 2013 -.0549806532889409  .0878360154634541  .0592999190137677  .0606418918918919 -6593600.910470409 .2176729955907496 30 1
            10 2014   .086402027027027  .0592999190137677  .0606418918918919  .0873603022194239  692590.5292479108 .2385135135135135 30 1
            11 2015 -.0682354793011176  .0606418918918919  .0873603022194239  .0947106064801994  27254942.34234234 .2535022823862742 30 1
            12 2016  .0242315148158405  .0873603022194239  .0947106064801994                  .  25086947.36842105 .2525616172805317 30 1
            14 2006                  .  .1218591493463787 -.0398097489896352  .1170403830162034 -1542762.804558011 .2296621120061584 36 1
            15 2007                  . -.0398097489896352  .1170403830162034  .1165633009113169  502194.1870427983 .2638107750370272 36 1
            16 2008  -.090757385929434  .1170403830162034  .1165633009113169   .145976578443377 -290091.3043439479 .2924167832022416 36 1
            17 2009 -.0626877230437558  .1165633009113169   .145976578443377  .0854640873863428  103687.8666523282 .3159921718836667 36 1
            18 2010  .1132953822280728   .145976578443377  .0854640873863428  .1532299253194134  689377.2788289897 .3021428135542983 36 1
            19 2011 -.0017292758355821  .0854640873863428  .1532299253194134  .1612936481053298  125432.4177208545 .3068016842342324 36 1
            20 2012   .021247846128923  .1532299253194134  .1612936481053298  .1089466738108681  118566.3553688824 .2844803463866512 36 1
            21 2013  .0871796347107497  .1612936481053298  .1089466738108681  .1501637919507956    -177765.8676723 .2942344137139898 36 1
            22 2014  .2874649017248295  .1089466738108681  .1501637919507956  .1058631023007599  189566.3370656456 .2942104559433079 36 1
            23 2015  .1037963366648087  .1501637919507956  .1058631023007599  .1398077764631518  970782.0549980451 .2634339766336831 36 1
            24 2016 -.2379779250879269  .1058631023007599  .1398077764631518                  .  892282.2689543241 .2463156383641319 36 1
            26 2006  .0064514585057354  .0622260101426278  .0693424726896234  .0339964528478043  5193858.627050297 .7760083080476969 45 2
            27 2007   .033590372196932  .0693424726896234  .0339964528478043  .0051029205779148  28270743.54794264 .6039542649186013 45 2
            28 2008 -.1291210914771246  .0339964528478043  .0051029205779148  .0540717393098261  169352430.0064919 .7093106321675542 45 2
            29 2009  .2264754801605781  .0051029205779148  .0540717393098261 -.0060983182941786 -2965467.766465229 .6897567816707768 45 2
            30 2010  .0217936930425438  .0540717393098261 -.0060983182941786 -.0921675289123191 -79240534.30620182 .5146064663477711 45 2
            31 2011 -.1355551781548335 -.0060983182941786 -.0921675289123191 -.1019358175121725  -5465482.32273015 .5402674286606786 45 2
            32 2012  .0548738982140263 -.0921675289123191 -.1019358175121725   -.09155212259329 -827559.9495724504 .4313619431452398 45 2
            33 2013 -.2852797708440599 -.1019358175121725   -.09155212259329 -.1880005826537061   1800963.15988729 .4651363557055098 45 2
            34 2014   -.00682777005539   -.09155212259329 -.1880005826537061 -.1677210080787016 -71063.60954534325 .2343015167948848 45 2
            35 2015  .0172501676159617 -.1880005826537061 -.1677210080787016 -.3414335652955077  467431.0087810373 .2062906570052192 45 2
            36 2016 -.1100268850452749 -.1677210080787016 -.3414335652955077                  .  867946.3008960915 .1701041479060839 45 2
            38 2006  .0004004061262137  .0743463576544576  .0485921434597949  .0697722368077415  107589409.0641554 .3559324457664203 37 1
            39 2007  .0241508707784085  .0485921434597949  .0697722368077415  .0606155564941346 -4457360.323089046  .329012659619799 37 1
            40 2008  .0483670994727305  .0697722368077415  .0606155564941346  .0317458273233052  68332293.54911216 .3063749199558565 37 1
            41 2009 -.0485138058932619  .0606155564941346  .0317458273233052  .0638103804299631 -13954589.85801217 .3104265402843602 37 1
            42 2010 -.0104133676222811  .0317458273233052  .0638103804299631  .0515522710801145  45917294.02422077 .3307701078074921 37 1
            43 2011 -.0492710919956753  .0638103804299631  .0515522710801145  .0443688344903179  65486930.62917723  .336545201801181 37 1
            44 2012 -.0090541651443186  .0515522710801145  .0443688344903179  .0221636060846531  165701896.0370561 .3553388746803069 37 1
            45 2013  .0260307809234277  .0443688344903179  .0221636060846531  .0299054046578525  125250376.1976803 .3636373797096266 37 1
            46 2014  .0116612269735022  .0221636060846531  .0299054046578525  .0363683673265147  48720290.41805401 .3000276594567683 37 1
            47 2015 -.0290226376573728  .0299054046578525  .0363683673265147  .0440352977523751  102754131.5354981 .2909569464182062 37 1
            48 2016  .0156503437089673  .0363683673265147  .0440352977523751                  .   48392996.2727472  .349308720166834 37 1
            50 2006  .0529984269784814 -.0528213831141281  .0804681869958389  .1125271353927698  401189.6030622562 .2749029453471883 35 1
            51 2007  .0899251508282715  .0804681869958389  .1125271353927698  .0508781836966496  383285.3280185005 .2582022502687859 35 1
            52 2008  .0538300616685811  .1125271353927698  .0508781836966496  .1410827484465108  1171209.262407769 .2721342719449668 35 1
            53 2009  .5494729312888372  .0508781836966496  .1410827484465108  .1837111906891238   201675.968984879 .1463310831954938 35 1
            54 2010  .2815141990365228  .1410827484465108  .1837111906891238 -.0084722904693742  2617793.713034117 .1612599087271429 35 1
            55 2011  -.107594750785139  .1837111906891238 -.0084722904693742 -.0815693953835585  20397671.75413711 .2068400559745337 35 1
            end

            Comment


            • #21
              If I understand correctly the code in #18, you are doing regressions within by-groups of SIC and year that omit the current observation and you then generate the residual for each observation. Without commenting on whether this makes sense or not, this is something that can be done with rangestat (from SSC).
              Code:
              clear all
              use "testdata_LariFari.dta"
              
              rangestat (reg) TCA b1 b2 b3 b4 b5, interval(year . .) by(SIC year) excludeself
              gen resid = TCA - (b1*b_b1 + b2*b_b2 + b3*b_b3 + b4*b_b4 + b5*b_b5 + b_cons)
              Note that rangestat will not present results if any variable is omitted (because the model is different).

              With respect to the second act, there's no gvkey variable in the data and id cannot be used as a substitute because there is only one observation per id.

              Comment


              • #22
                For anyone trying to follow a now very intermittent thread I have the following suggestions:

                1. Ignore #15 #16 #17 (unless they independently seem interesting or useful) as they appear only minimally related to the question in #18 and #20.

                2. Please start a new thread if you have a new question: this one is messy enough.

                Comment


                • #23
                  I'd just like to thank the three of you! Rangestat worked fine for me. Your input was very important to my research. Thanks a lot.

                  Comment


                  • #24
                    Hi. I have a question related to rolling standard deviation. I have looked at the functions, tsegen, rangestat and asrol and the posts in this discussion. I would like to estimate the rolling SD, by id (or givky as in the posts above) and year (monthly observations), using a rolling window of 10 years. However if 10 years of data is not available then use the maximum available data, with the minimum threshold (number of years) being 1 i.e. if less than 1 year of data is available no SD is calculated (missing is instead shown). Would appreciate help in this regards. Thank You.

                    Comment


                    • #25
                      Zain Mian Please study https://www.statalist.org/forums/help#stata and act on it.

                      The lack of a data example here from you frustrates a good reply.

                      Please note that tsegen, rangestat and asrol are commands, not functions. They are all available from SSC.

                      Something like

                      Code:
                      rangestat (count) whatever (sd) whatever, by(id) int(mdate -120 -1) 
                      replace whatever_sd = . if whatever_count < 12
                      seems to be what you are reaching for. I can't sense anything unusual in your problem and so why the many posts in this thread have not allowed an attempt at code.

                      Comment

                      Working...
                      X