I am trying to modify a model I use to calculate earnings management, the model is perfect but for a cross-sectional sample for one country. I want to modify the model to work across various countries-Industries of each country - and year finding coefficients to each country-industry-year. The model is as below, I tried to add to sortby the country but I am not sure if this is enough, when I run the same model over one country alone (using a separate dataset includes only the specific country variables) the coefficients of the residual were not the same, still I am not sure if it should be the same:
:
Code:
gen z2_coef=.
gen z3_coef=.
gen z4_coef=.
gen cons_coef=.
gen R2=.
gen AjR2=.
gen fitted=.
gen resi =.
gen n1=.
gen n2=.
by SIC year, sort: gen normaln =_N
forval i=1/89 {
forval y=2013/2021 {
count if SIC==`i' & year==`y'
return list
if `r(N)' >=3 {
capture reg z1 z2 z3 z4 if SIC==`i' & year==`y'
capture matrix a = e(b)
gen aaa if SIC== `i' & year==`y'= a[1,1]
gen bbb if SIC== `i' & year==`y'= a[1,2]
gen ccc if SIC== `i' & year==`y'= a[1,3]
gen ccc1 if SIC== `i' & year==`y'= a[1,4]
replace z2_coef= aaa if SIC== `i' & year==`y'
replace z3_coef= bbb if SIC== `i' & year==`y'
replace z4_coef= ccc if SIC== `i' & year==`y'
replace cons_coef= ccc1 if SIC== `i' & year==`y'
drop aaa
drop bbb
drop ccc
drop ccc1
predict iii if SIC== `i' & year==`y'
predict jjj if SIC== `i' & year==`y', resid
replace fitted= iii if SIC== `i' & year==`y'
replace resi = jjj if SIC== `i' & year==`y'
drop iii
drop jjj
ereturn list
gen ggg if SIC== `i' & year==`y'= e(r2)
gen hhh if SIC== `i' & year==`y'= e(r2_a)
replace R2= ggg if SIC== `i' & year==`y'
replace AjR2= hhh if SIC== `i' & year==`y'
drop ggg
drop hhh
gen kkk if SIC== `i' & year==`y'= e(N)
gen mmm if SIC== `i' & year==`y'= _N
replace n1= kkk if SIC== `i' & year==`y'
replace n2= mmm if SIC== `i' & year==`y'
drop kkk
drop mmm
}
}
}

Comment