Originally posted by Jan Filips
View Post
Didn't work for me...
* ------------ regressions over a window of 6 periods using -rangestat- -------- * define a linear regression in Mata using quadcross() - help mata cross(), example 2 mata: mata clear mata set matastrict on real rowvector myreg(real matrix Xall) { real colvector y, b, Xy real matrix X, XX y = Xall[.,1] // dependent var is first column of Xall X = Xall[.,2::cols(Xall)] // the remaining cols are the independent variables X = X,J(rows(X),1,1) // add a constant XX = quadcross(X, X) // linear regression, see help mata cross(), example 2 Xy = quadcross(X, y) b = invsym(XX) * Xy return(rows(X), b') } end * regressions with a constant over a rolling window of 6 periods by permno rangestat (myreg) stockexcessret mktrf, by(permno) interval(time -5 0) casewise * the Mata function returns first the number of observations and then as many * variables as there are independent variables (plus the constant) for the betas rename (myreg1 myreg2 myreg3) (nobs rs_mktrf rs_cons) * reject results if the window is less than 6 or if the number of obs < 4 isid permno year by permno: replace rs_mktrf = . if _n < 6 | nobs < 4 by permno: replace rs_cons = . if _n < 6 | nobs < 4 save "rangestat_results.dta", replace * ----------------- replicate using -rolling- ---------------------------------- use "test_data.dta", clear levelsof permno, local(permno) foreach s of local permno { rolling _b, window(6) saving(betas_`s', replace) reject(e(N) < 4): /// regress stockexcessret mktrf if permno == `s' } clear save "betas.dta", replace emptyok foreach s of local permno { append using "betas_`s'.dta" } rename end year merge 1:1 permno year using "rangestat_results.dta" isid permno year, sort gen diff_mktrf = abs(_b_mktrf - float(rs_mktrf)) gen diff_cons = abs(_b_cons - float(rs_cons)) summ diff*
use "test_data.dta", clear
rangestat (reg) Re_Rf Rm_Rf, interval(fyear -4 0) by(gvkey)
qui levelsof co_code, local(co_code) capture drop resid ge resid = . foreach stk of local co_code{ reg returns lag_mw mw lead_mw if co_code == "`stk'" predict res, residuals replace resid = res if e(sample) drop res }
. qui levelsof co_code, local(co_code) . capture drop resid . ge resid = . (7,127,274 missing values generated) . forval yr = 2008/2018 { 2. foreach stk of local co_code{ 3. reg wnreturns lag_mwret mwret lead_mwret if fyear==`yr' co_code =="`stk'" 4. predict res, residuals 5. replace resid = res if e(sample) 6. drop res 7. } 8. } invalid 'co_code' r(198);
http://lwstatantu.blogspot.com/
. qui levelsof co_code, local(co_code) . qui levelsof week_id, local(week_id) . capture drop resid . ge resid = . (1,672,630 missing values generated) . foreach stk of local co_code{ 2. foreach w of local week_id{ 3. reg wnreturns lag_mwret mwret lead_mwret if week_id=="`w'" & co_code =="`stk'" 4. predict res, residuals 5. replace resid = res if e(sample) 6. drop res 7. } 8. } type mismatch r(109);
* Example generated by -dataex-. To install: ssc install dataex clear input long co_code float(year week_id) double(mwret lag_mwret lead_mwret) float resid 11 2008 2535 -.06649971008300781 -.09247142169624567 -.03148415847681463 . 11 2008 2536 -.03148415847681463 -.06649971008300781 -.11137222312390804 . 11 2008 2537 -.11137222312390804 -.03148415847681463 -.21280589513480663 . 11 2008 2538 -.21280589513480663 -.11137222312390804 .18719903379678726 . 11 2008 2539 .18719903379678726 -.21280589513480663 .03372354805469513 . 11 2008 2540 .03372354805469513 .18719903379678726 -.11738803843036294 . 11 2008 2541 -.11738803843036294 .03372354805469513 -.03315603360533714 . 11 2008 2542 -.03315603360533714 -.11738803843036294 -.010439421981573105 . 11 2008 2543 -.010439421981573105 -.03315603360533714 .03699047735426575 . 11 2008 2544 .03699047735426575 -.010439421981573105 .06843714625574648 . 11 2008 2545 .06843714625574648 .03699047735426575 .019301355816423893 . 11 2008 2546 .019301355816423893 .06843714625574648 -.02672520885244012 . 11 2008 2547 -.02672520885244012 .019301355816423893 -.013181474292650819 . 11 2009 2548 -.013181474292650819 -.02672520885244012 -.029572789557278156 . 11 2009 2549 -.029572789557278156 -.013181474292650819 -.04662084439769387 . 11 2009 2550 -.04662084439769387 -.029572789557278156 .05161661631427705 . 11 2009 2551 .05161661631427705 -.04662084439769387 -.016435411293059587 . 11 2009 2552 -.016435411293059587 .05161661631427705 .04282562481239438 . 11 2009 2553 .04282562481239438 -.016435411293059587 -.05246870941482484 . 11 2009 2554 -.05246870941482484 .04282562481239438 -.004928974318318069 . 11 2009 2555 -.004928974318318069 -.05246870941482484 -.043389408849179745 . 11 2009 2556 -.043389408849179745 -.004928974318318069 -.02761586755514145 . 11 2009 2557 -.02761586755514145 -.043389408849179745 .08259395556524396 . 11 2009 2558 .08259395556524396 -.02761586755514145 .06565725135806133 . 11 2009 2559 .06565725135806133 .08259395556524396 .02514732163399458 . 11 2009 2560 .02514732163399458 .06565725135806133 .08832431398332119 . 11 2009 2561 .08832431398332119 .02514732163399458 .041370447957888246 . 11 2009 2562 .041370447957888246 .08832431398332119 -.04516167985275388 . 11 2009 2563 -.04516167985275388 .041370447957888246 .04222985589876771 . 11 2009 2564 .04222985589876771 -.04516167985275388 -.007954363245517015 . 11 2009 2565 -.007954363245517015 .04222985589876771 .002809791825711727 . 11 2009 2566 .002809791825711727 -.007954363245517015 .16100615786854178 . 11 2009 2567 .16100615786854178 .002809791825711727 .0013456032902467996 . 11 2009 2568 .0013456032902467996 .16100615786854178 .05784685374237597 . end format %tw week_id
. qui levelsof week_id, local(week_id) . qui levelsof co_code, local(co_code) . capture drop resid . ge resid = . (1,672,630 missing values generated) . foreach stk of local co_code{ 2. foreach w of local week_id{ 3. reg wnreturns lag_mwret mwret lead_mwret if week_id==`w' & co_code =="`stk'" 4. predict res, residuals 5. replace resid = res if e(sample) 6. drop res 7. } 8. } type mismatch r(109);
Comment