Announcement

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

  • Query about r(5); error

    I am following certain accounting models to estimate measures of financial reporting quality. I have to run a cross-sectional regression for each year and industry. The residuals from that equation are then extracted. The model looks like as follows:

    Working Capital Change (t) = Cash Flow (t-1) + Cash Flow (t) + Cash Flow (t+1)

    I try to run my regression using this code:

    by time industry, sort : regress Working_Capital L1.Cash_Flow Cash_Flow F1.Cash_Flow

    I get the error:

    -> time = 2003, industry = Financials
    not sorted
    r(5);

    So far I have encountered no problem in running any kind of year-industry cross-sectional regressions but not sure why getting this error for this particular model. Please help. Following is my sample data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int time str22 firmid_time float(Working_Capital Cash_Flow) long(firmid industry)
    2010 "0876711D LN Equity2010"         .  .0116214  1  3
    2011 "0876711D LN Equity2011"         .  .4808342  1  3
    2005 "1218069D LN Equity2005" -.0837562  .2320575  2  7
    2006 "1218069D LN Equity2006"  .0725541  .3132595  2  7
    2007 "1218069D LN Equity2007" -.0636922  .0447656  2  7
    2008 "1218069D LN Equity2008"  .1583379   .174898  2  7
    2009 "1218069D LN Equity2009"  .0083617  .0979778  2  7
    2010 "1218069D LN Equity2010"  .0211161   .114875  2  7
    2011 "1218069D LN Equity2011" -.0058695  .1904697  2  7
    2008 "1334987D LN Equity2008"  .1507705   .059889  3  4
    2009 "1334987D LN Equity2009" -.1790414  .1588346  3  4
    2010 "1334987D LN Equity2010"  .0493072 -.0484923  3  4
    2005 "1561649D LN Equity2005" -.0543373 -.0856612  4  8
    2006 "1561649D LN Equity2006" -.0198585  .3446159  4  8
    2008 "1561649D LN Equity2008"         .         .  4  8
    2009 "1561649D LN Equity2009"   .055356   .207237  4  8
    2010 "1561649D LN Equity2010" -.2488663  .1168646  4  8
    2011 "1561649D LN Equity2011"  .0365924  .0681397  4  8
    2012 "1561649D LN Equity2012" -.0026056  .1955632  4  8
    2013 "1561649D LN Equity2013"  .0721054  .1905114  4  8
    2014 "1561649D LN Equity2014"   .009598  .2175281  4  8
    2009 "1638414D LN Equity2009"  .0120353  .0618803  5  8
    2010 "1638414D LN Equity2010" -.1701783  .0821014  5  8
    2011 "1638414D LN Equity2011"  .1824905  .1499038  5  8
    2012 "1638414D LN Equity2012" -.0806033  .1632631  5  8
    2013 "1638414D LN Equity2013"  .0052654  .1488947  5  8
    2008 "1655637D LN Equity2008"         .  .0738092  6  4
    2009 "1655637D LN Equity2009"         .  .0785614  6  4
    2010 "1655637D LN Equity2010"         .  .0281156  6  4
    2011 "1655637D LN Equity2011"         .  .0440912  6  4
    2012 "1655637D LN Equity2012"         . -.0091816  6  4
    2013 "1655637D LN Equity2013"         .  .0216734  6  4
    2014 "1655637D LN Equity2014"         .  .0498851  6  4
    2006 "3572335Q LN Equity2006" -.0247408  .1234333  7  3
    2007 "3572335Q LN Equity2007"   .012937  .1029526  7  3
    2008 "3572335Q LN Equity2008" -.0003566 -.0124822  7  3
    2007 "8190169Q LN Equity2007" -.0842475  .0695495  8 10
    2008 "8190169Q LN Equity2008" -.0081827  .0431299  8 10
    2009 "8190169Q LN Equity2009" -.0124594  .0520247  8 10
    2010 "8190169Q LN Equity2010"  .0513056   .048758  8 10
    2007 "9408593Q LN Equity2007" -.0584078  .1321769  9  6
    2008 "9408593Q LN Equity2008" -.1135363  .1497753  9  6
    2014 "AA/ LN Equity2014"      -.0063207  .0804459 10  6
    2015 "AA/ LN Equity2015"      -.0188968   .104188 10  6
    2016 "AA/ LN Equity2016"      -.0115068  .1112329 10  6
    2017 "AA/ LN Equity2017"      -.0021075  .0953635 10  6
    2005 "AAL LN Equity2005"       .0048643   .120559 11  1
    2006 "AAL LN Equity2006"       .0084024     .1582 11  1
    2007 "AAL LN Equity2007"      -.1000796  .1507863 11  1
    2008 "AAL LN Equity2008"      -.0704839   .170122 11  1
    2009 "AAL LN Equity2009"       .1659496  .0721782 11  1
    2010 "AAL LN Equity2010"      -.0075833  .1265362 11  1
    2011 "AAL LN Equity2011"      -.0073962  .1335964 11  1
    2012 "AAL LN Equity2012"        .011209  .0748461 11  1
    2013 "AAL LN Equity2013"       .0022619  .0768035 11  1
    2014 "AAL LN Equity2014"       .0107637  .0763718 11  1
    2015 "AAL LN Equity2015"      -.0037873  .0495077 11  1
    2016 "AAL LN Equity2016"      -.0149578  .0909196 11  1
    2017 "AAL LN Equity2017"      -.0065206  .1529841 11  1
    2018 "AAL LN Equity2018"       .0068364  .1256025 11  1
    2005 "ABF LN Equity2005"      -.0838311  .1120494 12  2
    2006 "ABF LN Equity2006"      -.0074111  .0678524 12  2
    2007 "ABF LN Equity2007"       .0483672  .1018176 12  2
    2008 "ABF LN Equity2008"       .0127507  .0716332 12  2
    2009 "ABF LN Equity2009"      -.0463747  .0932401 12  2
    2010 "ABF LN Equity2010"       .0328794  .1223292 12  2
    2011 "ABF LN Equity2011"      -.0133506  .0707364 12  2
    2012 "ABF LN Equity2012"       .0068614  .1130171 12  2
    2013 "ABF LN Equity2013"       .0191406  .1162109 12  2
    2014 "ABF LN Equity2014"      -.0161943  .1338924 12  2
    2015 "ABF LN Equity2015"       .0127005  .1115355 12  2
    2016 "ABF LN Equity2016"       .0135043   .125159 12  2
    2017 "ABF LN Equity2017"      -.0103727  .1458333 12  2
    2018 "ABF LN Equity2018"       .0018735  .1094457 12  2
    2010 "ACA LN Equity2010"       .3212871  .1459229 13  1
    2011 "ACA LN Equity2011"       .0081743  .1702442 13  1
    2012 "ACA LN Equity2012"       .0054695  .0815711 13  1
    2013 "ACA LN Equity2013"      -.0303554  .0561305 13  1
    2014 "ACA LN Equity2014"       -.003637  .1190502 13  1
    2015 "ACA LN Equity2015"      -.0324888  .0605652 13  1
    2016 "ACA LN Equity2016"      -.0255682  .1364426 13  1
    2017 "ACA LN Equity2017"      -.0176201 -.0091791 13  1
    2007 "ADM LN Equity2007"              .  .1149488 14  4
    2008 "ADM LN Equity2008"              .  .1362829 14  4
    2009 "ADM LN Equity2009"              .   .223668 14  4
    2010 "ADM LN Equity2010"              .   .179803 14  4
    2011 "ADM LN Equity2011"              .  .1074885 14  4
    2012 "ADM LN Equity2012"              .  .0842862 14  4
    2013 "ADM LN Equity2013"              .  .0727171 14  4
    2014 "ADM LN Equity2014"              .  .0518165 14  4
    2015 "ADM LN Equity2015"              .  .0773437 14  4
    2016 "ADM LN Equity2016"              .  .1002046 14  4
    2017 "ADM LN Equity2017"              .  .0683632 14  4
    2018 "ADM LN Equity2018"              .  .0314212 14  4
    2007 "ADN LN Equity2007"       .0467431  -.013448 15  4
    2008 "ADN LN Equity2008"       .0722687  .0283481 15  4
    2009 "ADN LN Equity2009"      -.1285712  .0071969 15  4
    2010 "ADN LN Equity2010"       .0131214  .0691271 15  4
    2011 "ADN LN Equity2011"       .0765588  .1164431 15  4
    2012 "ADN LN Equity2012"      -.4430586   .127776 15  4
    end
    label values firmid n_fid
    label def n_fid 1 "0876711D LN Equity", modify
    label def n_fid 2 "1218069D LN Equity", modify
    label def n_fid 3 "1334987D LN Equity", modify
    label def n_fid 4 "1561649D LN Equity", modify
    label def n_fid 5 "1638414D LN Equity", modify
    label def n_fid 6 "1655637D LN Equity", modify
    label def n_fid 7 "3572335Q LN Equity", modify
    label def n_fid 8 "8190169Q LN Equity", modify
    label def n_fid 9 "9408593Q LN Equity", modify
    label def n_fid 10 "AA/ LN Equity", modify
    label def n_fid 11 "AAL LN Equity", modify
    label def n_fid 12 "ABF LN Equity", modify
    label def n_fid 13 "ACA LN Equity", modify
    label def n_fid 14 "ADM LN Equity", modify
    label def n_fid 15 "ADN LN Equity", modify
    label values industry n_ind
    label def n_ind 1 "Basic Materials", modify
    label def n_ind 2 "Consumer Goods", modify
    label def n_ind 3 "Consumer Services", modify
    label def n_ind 4 "Financials", modify
    label def n_ind 6 "Industrials", modify
    label def n_ind 7 "Oil & Gas", modify
    label def n_ind 8 "Technology", modify
    label def n_ind 10 "Utilities", modify

  • #2
    The use of L1. and F1. requires the same sort order as previously set by

    Code:
    tsset
    or
    Code:
    xtset
    In any case, with a structure of firms as well as industries, what are previous and next values anyway?

    Is this what you want

    Code:
    tsset firmid time 
    gen previous = L1.Cash_Flow 
    gen next = F1.Cash_Flow 
    
    by time industry, sort : regress Working_Capital previous Cash_Flow next
    If so, how are you going to process all those regressions? Check out statsby or rangestat (SSC).

    Comment


    • #3
      it worked like a magic. Thanks a lot.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        The use of L1. and F1. requires the same sort order as previously set by

        Code:
        tsset
        or
        Code:
        xtset
        In any case, with a structure of firms as well as industries, what are previous and next values anyway?

        Is this what you want

        Code:
        tsset firmid time
        gen previous = L1.Cash_Flow
        gen next = F1.Cash_Flow
        
        by time industry, sort : regress Working_Capital previous Cash_Flow next
        If so, how are you going to process all those regressions? Check out statsby or rangestat (SSC).
        Thanks I was actually able to carry by cross-sectional regression year-industry level. However when I try to attempt residuals I get the following error:

        predict residuals, residuals
        last estimates not found
        r(301);

        I acknowledge that for some year-industry groups there were no observations hence I got no regression results which is fine. How can I extract residuals for my above regression? many thanks.

        Comment


        • #5
          So it turns out that Nick Cox was on the right track in suggesting that doing this with -by- would prove inadequate for your purposes. You can't calculate and save the residuals with -by-.

          Code:
          tsset firmid time
          gen previous = L1.Cash_Flow
          gen next = F1.Cash_Flow
          
          by time industry, sort : regress Working_Capital previous Cash_Flow next
          
          
          capture program drop one_regression
          program define one_regression
              capture regress Working_Capital previous Cash_Flow next
              if !inlist(c(rc), 0, 2000, 2001) {
                  gen problem = "Unexpcted error in regression"
              }
              else if c(rc) == 0 {
                  predict residual, resid
              }
              else {
                  gen problem = "No or insufficient observations"
              }
              exit
          end
          
          runby one_regression, by(time industry) status
          To use this code, you must install the -runby- command, written by Robert Picard and me, available from SSC.

          In addition to adding the residuals to your data set, the code will create a string variable called problem. That variable will alert you to any time-industry groups for which there was no, or insufficient data to do the regression, and also any such groups for which the -regress- command failed for other, unexpected, reasons. If there are any of the latter, you will need to investigate what the source of the problem is.

          Note: code not tested. Beware of typos, unbalanced brackets, quotes, etc. If this code does not perform correctly for you, please post back and include example data that reproduces whatever errors you encounter.


          Comment


          • #6
            Also

            Code:
            tsset firmid time
            gen previous = L1.Cash_Flow
            gen next = F1.Cash_Flow
            
            rangestat (reg) Working_Capital previous Cash_Flow next, ///
            int(time 0 0) by(industry)
            
            gen double residuals = Working_Capital - b_cons - b_previous * previous - b_next * next - b_Cash_Flow * Cash_Flow

            Comment

            Working...
            X