In
https://github.com/OpenSourceAP/CrossSection/blob/d81c696d283d62b61260f223eeac0e90511a4e77/Signals/Code/Predictors/TrendFactor.do
We use asreg to run regressions every month. This returns firm-month data with coefficients duplicated for each firm.
* Cross-sectional regression of returns on trend signals in month t-1
xtset permno time_avail_m
gen fRet = f.ret // Instead of lagging all moving averages, I lead the return (and adjust the rolling sums below accordingly)
bys time_avail_m: asreg fRet A_*
* Take 12-month rolling average of MA beta coefficients (leaving out most recent one to not use future information from fRet)
preserve
bys time_avail_m: keep if _n == 1 // Current dataset is firm-time-level but we only need time-level here
foreach L of numlist 3 5 10 20 50 100 200 400 600 800 1000 {
asrol _b_A_`L', window(time_avail_m -13 -1) stat(mean) gen(EBeta_`L')
}
keep time_avail_m EBeta*
save tempBeta, replace
restore
To deduplicate, we use bys time_avail_m: keep if _n == 1. But this will sometimes keep a permno-month which has all missing values for coefficients, perhaps because asreg does not assign coefficients to permnos that have no LHS variable data.
The way to fix this is to filter for _Nobs != . before doing the keep if _n == 1.
But we're moving to python anyway.
In
https://github.com/OpenSourceAP/CrossSection/blob/d81c696d283d62b61260f223eeac0e90511a4e77/Signals/Code/Predictors/TrendFactor.do
We use asreg to run regressions every month. This returns firm-month data with coefficients duplicated for each firm.
To deduplicate, we use
bys time_avail_m: keep if _n == 1. But this will sometimes keep a permno-month which has all missing values for coefficients, perhaps because asreg does not assign coefficients to permnos that have no LHS variable data.The way to fix this is to filter for _Nobs != . before doing the keep if _n == 1.
But we're moving to python anyway.