xtset code0 month0
egen g=group(code0 month0) //总是显示insufficient observations,改为egen g=group(code0 month2)也一样
qui sum g
local n=r(max)
gen b_mkt = .
gen b_smb = .
gen b_hml = .
gen e = .
gen e0 = .
gen e1 = .
forval i=1/`n'{
reg R1 mkt smb hml if g == `i'
matrix b = e(b)
replace alpha = b[1,4] if g == `i'
replace b_mkt = b[1,1] if g == `i'
replace b_smb = b[1,2] if g == `i'
replace b_hml = b[1,3] if g == `i'
cap drop re
predict re if g == `i'
replace e = re if g == `i'
cap drop e0
egen e0_sd = sd(e) if g == `i'
replace e1 = e0 if g == `i'
}
drop g
use "F:\JG\LUNWEN\SHUJU\最新数据\特征变量.dta", clear //Fama-French三因子回归得到系数和残差值
sort code month
gen alpha = .
gen b_mkt = .
gen b_smb = .
gen b_hml = .
gen e = .
gen e0 = .
gen e1 = .
forvalues i = 1/2500{
forvalues j = 200403/201412{ //结果显示no observations,将forvalues j = 200403/201412和 & month2 == `j'去掉可以运行,但是成了每只股票的回归,怎样改成每只股票每月各回归一次?
reg R1 mkt smb hml if code0 == `i' & month2 == `j'
matrix b = e(b)
replace alpha = b[1,4] if code0 == `i' & month2 == `j'
replace b_mkt = b[1,1] if code0 == `i' & month2 == `j'
replace b_smb = b[1,2] if code0 == `i' & month2 == `j'
replace b_hml = b[1,3] if code0 == `i' & month2 == `j'
cap drop re
predict re if code0 == `i' & month2 == `j'
replace e = re if code0 == `i' & month2 == `j'
cap drop e0
egen e0_sd = sd(e) if code0 == `i' & month2 == `j'
replace e1 = e0 if code0 == `i' & month2 == `j'