Announcement

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

  • Getting unexpected error while running Fama-Macbeth regression

    Hi people from Statalist,

    I am getting unexpected error messages after trying to run a Fama-Macbeth two-step regression

    My initial data looks like the following:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int fyear double(BH1 SH1 BH2 BH4) float ENT_Factor double Rf
    1966   -.0931677594780922  -.22325588762760162                    0  -.17158393561840057 -.009890694                 .05
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1969  -.20049288868904114   -.2568358778953552  -.15935015678405762   -.2505379617214203  -.09002729               .0832
    1970  .053339794278144836 -.020984245464205742   .09377075731754303    .1467593014240265  -.02894144 .049400000000000006
    1971   .17877981066703796    .0914343073964119    .2408723086118698    .3553159236907959  -.08042617               .0446
    1972   .09672121703624725   -.0740591436624527   .12717123329639435   .30931907892227173  -.17474627 .056799999999999996
    1973   .04438353702425957  -.23002906143665314   -.0322846919298172   -.2686358094215393  -.09999046                .073
    1974    -.205159530043602  -.17706629633903503  -.21290186047554016  -.28413623571395874   .03173215               .0735
    1975    .3234483301639557    .3049960136413574   .29543182253837585     .458263099193573   .12285192               .0616
    1976    .1988406628370285    .1928432136774063    .1248864233493805   .17149516940116882   .04048293 .048600000000000004
    1977  -.11372006684541702   .08067355304956436   -.1128876805305481   -.1684066206216812   .22300674               .0698
    1978   .04086245596408844    .0477270632982254    .0384380966424942   .04805698245763779  .026573204               .1057
    1979   .06726029515266419   .06941448152065277   .09794113785028458   .05182604119181633    .0696834  .11699999999999999
    1980   .15167495608329773   .09065254032611847   .19350561499595642   .21233008801937103    -.053161               .1386
    1981  -.13656291365623474   .06408078968524933  -.03801873326301575   -.2046550065279007   .07942122               .1335
    1982 -.045518483966588974 -.016930749639868736   .02803255245089531   .22480489313602448   .01780938               .0868
    1983    .2057950496673584   .17656035721302032   .17736491560935974   .12527137994766235   .04141217               .1008
    1984  -.09119043499231339   -.1490718275308609  -.03271746262907982  -.03367561846971512 -.016410518               .0922
    1985   .22003142535686493   .06953927874565125   .22126232087612152   .27967599034309387  -.12046785                .076
    1986   .13706491887569427   .06548002362251282   .06259170919656754   .09255068749189377   .02503202 .059500000000000004
    1987 -.002735091606155038  -.02348390594124794  .009247072972357273  -.07404982298612595  -.02140268                .071
    1988 -.004782224074006081     .105547234416008   .05380646511912346   .04351597651839256   .08488517               .0902
    1989   .12887944281101227  .008753606118261814   .07307246327400208   .28277266025543213  -.05303585               .0776
    1990  -.13178525865077972  -.09342804551124573  -.11961032450199127  -.13600079715251923   .06830745               .0682
    1991   .05553749203681946   .11657202988862991   .13540604710578918    .2445092797279358   .11135559               .0412
    1992 -.041848137974739075   .04496622085571289  -.01872728019952774  -.07017866522073746   .12293859               .0361
    1993   .18095359206199646    .1589204967021942   .13676345348358154   .08980792760848999   .13639818               .0363
    1994  -.00756983645260334 -.012461794540286064  -.01410653255879879  -.09944070130586624  .019016653  .07200000000000001
    1995    .1364327073097229   .04993479326367378   .16317661106586456   .26851198077201843  -.03791674               .0518
    1996   .07671297341585159   .08399267494678497   .10819978266954422   .10068926215171814   .07602091 .055099999999999996
    1997   .11235480010509491   .19239002466201782   .14804206788539886   .28711915016174316   .08805727 .055099999999999996
    1998  -.21100890636444092  -.22832456231117249  -.13766969740390778 -.016598155722022057  .004727644               .0453
    1999 -.025414342060685158  -.11044283956289291 -.012465708889067173 .0055834222584962845  -.03800985 .059800000000000006
    2000  -.06321937590837479  .014771484769880772 -.014780322089791298    -.223695307970047     .155873 .060899999999999996
    2001   .05215299129486084   .18261636793613434   .08493885397911072  -.06771183758974075   .21364613               .0217
    2002  -.13117147982120514  .029295628890395164  -.06217939034104347   -.1526198536157608   .16244952               .0132
    2003    .3051352798938751    .2713721990585327    .2756713330745697   .39864784479141235  .014644504               .0126
    2004    .1304800659418106   .10089210420846939   .17386822402477264    .1597801148891449   .04929395               .0275
    2005   .02779119648039341 -.015930652618408203    .0425284244120121   .11163264513015747   .02286742               .0279
    2006   .10765527188777924   .06959732621908188   .11718408018350601    .1765100061893463 -.014506575               .0438
    2007   .03548966348171234  -.15685304999351501   .04816693067550659   .06009546294808388  -.05754094               .0334
    2008   -.3497254252433777  -.24159081280231476   -.3368995189666748   -.4298965632915497   .12097402               .0037
    2009    .3101717233657837   .01874624378979206   .33388304710388184     .512330174446106  -.19778638 .004699999999999999
    2010    .1299227923154831   .13636653125286102   .18224291503429413    .2594715356826782  .017381892               .0029
    2011  -.07393523305654526 -.008770035579800606  -.04247064143419266   -.0379888117313385   .08021694               .0029
    2012   .07265020906925201   .11839137226343155   .12167210131883621   .19287985563278198   .04595768               .0016
    2013   .15165553987026215   .24131080508232117     .202187180519104   .27715104818344116   .10034878               .0013
    2014 -.017233319580554962  .006779185030609369  .023328838869929314  .015851562842726707   .04437155               .0025
    2015  -.09098861366510391 -.014002025127410889 -.058479614555835724  -.06565603613853455    .0829958 .006500000000000001
    1966   -.0931677594780922  -.22325588762760162                    0  -.17158393561840057 -.009890694                 .05
    1966   -.0931677594780922  -.22325588762760162                    0  -.17158393561840057 -.009890694                 .05
    1966   -.0931677594780922  -.22325588762760162                    0  -.17158393561840057 -.009890694                 .05
    1966   -.0931677594780922  -.22325588762760162                    0  -.17158393561840057 -.009890694                 .05
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1967   .11946436017751694    .3939952850341797  .034682102501392365   .18941856920719147     .210428               .0576
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    1968  .006173937581479549   .14093846082687378   .03458116948604584 -.004401206038892269   .09205266               .0646
    end


    My code looks like the following:

    Code:
     
    
    
    quietly {
    
    foreach var of varlist BH1 BL1 SH1 SL1 BH2 BL2 SH2 SL2 BH3 BL3 SH3 SL3 BH4 BL4 SH4 SL4 BH5 BL5 SH5 SL5 BH6 BL6 SH6 SL6 BH7 BL7 SH7 SL7 {
    
    replace `var'=`var'-Rf
    
      }
    
    }
    
    ** Number of observations defining **
    
    local obs=14802
    
    tempname betas
    
    postfile `betas' beta_c beta_HML_Factor using "D:\betas.dta", replace
    
    quietly {
    
    foreach y of varlist BH1 BL1 SH1 SL1 BH2 BL2 SH2 SL2 BH3 BL3 SH3 SL3 BH4 BL4 SH4 SL4 BH5 BL5 SH5 SL5 BH6 BL6 SH6 SL6 BH7 BL7 SH7 SL7 {
    regress `y' HML_Factor if fyear>=1962 & fyear<=2017
    scalar beta_c=_b[_cons]
    scalar beta_HML_Factor=_b[HML_Factor]
    post `betas' (beta_c) (beta_HML_Factor)
     }
    }
    
    postclose `betas'
    
    drop HML_Factor
    
    xpose, clear varname
    
    drop in 1
    
    merge 1:1 _n using "D:\betas.dta"
    
    tempname lambdas
    
    local obs=14802
    
    postfile `lambdas' lambda_c lambda_HML_Factor lambda_r2 using "D:\lambdas.dta", replace
    
    quietly {
    
    forvalues i=1(1)`obs' {
    
    regress v`i' beta_HML_Factor
    scalar lambda_c=_b[_cons]
    scalar lambda_HML_Factor=_b[beta_HML_Factor]
    scalar lambda_r2=e(r2)
    
    post `lambdas' (lambda_c) (lambda_HML_Factor)(lambda_r2)
      }
     }
     
    postclose `lambdas'
    
     
     snapshot restore 1
     use "D:\lambdas.dta", clear
     local obs=14802
    
     quietly {
     
     foreach var of varlist (lambda_c) (lambda_HML_Factor) { 
     summarize `var'
     scalar `var'_mean=r(mean)
     
     scalar `var'_tratio=sqrt(`obs')*r(mean)/r(sd)
     }
     summarize lambda_r2
     scalar lambda_r2_mean=r(mean)
     }
     
    display lambda_c_mean
    display lambda_c_tratio
    display lambda_HML_Factor_mean
    display lambda_HML_Factor_tratio
    display lambda lambda_r2_mean


    And I get the following errors:




    In addition I do not get any summarized tables or displays...






    I would be very glad if someone could help me, I am stuck in this for like two weeks or so.

  • #2
    You're using the same names for variables and scalars. That's unlikely to help.

    I'd try rewriting

    Code:
    quietly {  
        forvalues i=1(1)`obs' {  
            regress v`i' beta_HML_Factor
            scalar lambda_c=_b[_cons]
            scalar lambda_HML_Factor=_b[beta_HML_Factor]
            scalar lambda_r2=e(r2)  
            post `lambdas' (lambda_c) (lambda_HML_Factor)(lambda_r2)  
        }
    }
    as

    Code:
    quietly {  
        forvalues i=1(1)`obs' {  
            regress v`i' beta_HML_Factor
            post `lambdas' (_b[_cons]) (_b[beta_HML_Factor)(e(r2)) 
        }
    }
    Putting stuff into scalars only to take them out again immediately isn't needed in any case.
    Last edited by Nick Cox; 08 May 2017, 13:46.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      You're using the same names for variables and scalars. That's unlikely to help.

      I'd try rewriting

      Code:
      quietly {
      forvalues i=1(1)`obs' {
      regress v`i' beta_HML_Factor
      scalar lambda_c=_b[_cons]
      scalar lambda_HML_Factor=_b[beta_HML_Factor]
      scalar lambda_r2=e(r2)
      post `lambdas' (lambda_c) (lambda_HML_Factor)(lambda_r2)
      }
      }
      as

      Code:
      quietly {
      forvalues i=1(1)`obs' {
      regress v`i' beta_HML_Factor
      post `lambdas' (_b[_cons]) (_b[beta_HML_Factor)(e(r2))
      }
      }
      Putting stuff into scalars only to take them out again immediately isn't needed in any case.
      Thank you for your quick response. Do you also know what to change to overcome the other errors and to get the summarize and display values/tables?

      Comment


      • #4
        My attitude is to fix the first bug. But what I see in red in #1 includes

        postclose lambdas:: you used a temporary name as your handle, not lambdas.

        isplay not found: looks like a typo for display

        lambda ambiguous abbreviation: that sounds correct; several of your variables and scalars have names starting that way.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          My attitude is to fix the first bug. But what I see in red in #1 includes

          postclose lambdas:: you used a temporary name as your handle, not lambdas.

          isplay not found: looks like a typo for display

          lambda ambiguous abbreviation: that sounds correct; several of your variables and scalars have names starting that way.
          Alright. I will implement your given solution for the first bug then... brb

          Comment


          • #6
            Originally posted by Nick Cox View Post
            You're using the same names for variables and scalars. That's unlikely to help.

            I'd try rewriting

            Code:
            quietly {
            forvalues i=1(1)`obs' {
            regress v`i' beta_HML_Factor
            scalar lambda_c=_b[_cons]
            scalar lambda_HML_Factor=_b[beta_HML_Factor]
            scalar lambda_r2=e(r2)
            post `lambdas' (lambda_c) (lambda_HML_Factor)(lambda_r2)
            }
            }
            as

            Code:
            quietly {
            forvalues i=1(1)`obs' {
            regress v`i' beta_HML_Factor
            post `lambdas' (_b[_cons]) (_b[beta_HML_Factor)(e(r2))
            }
            }
            Putting stuff into scalars only to take them out again immediately isn't needed in any case.
            I get the same error. And by the way, the tempname was indeed lambdas... So I do not know why I get the error at the part of postclose... I used the same method as the method of the beginning part regarding the tempname betas and postclose betas...

            Comment


            • #7
              Stata's error message, and a careful look at the post command, tells you that you forgot the right bracket following _b[beta_HML_Factor in the second term of your postfile (the term you called lambda_HML_factor).

              Fix that and see if it solves the postclose problem as well.

              Comment

              Working...
              X