Announcement

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

  • sort by 'Cross country industry year' model

    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
              
                      }
                 }
           }
    :

  • #2
    I have updated the following code, it works and gets me the residuals but still, I am not sure if it is correct or not.
    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 country_id SIC year, sort: gen normaln =_N
    
    forval i=1/89 {
        forval y=2013/2021 {
            count if SIC==`i' & year==`y' & country_id!=. 
            return list
            if `r(N)' >=3 {
                capture reg z1 z2 z3 z4 if SIC==`i' & year==`y' & country_id!=. 
                capture matrix a = e(b)
                gen aaa = a[1,1] if SIC== `i' & year==`y' & country_id!=.
                gen bbb = a[1,2] if SIC== `i' & year==`y' & country_id!=.
                gen ccc = a[1,3] if SIC== `i' & year==`y' & country_id!=.
                gen ccc1 = a[1,4] if SIC== `i' & year==`y' & country_id!=.
                replace z2_coef = aaa if SIC== `i' & year==`y' & country_id!=.
                replace z3_coef = bbb if SIC== `i' & year==`y' & country_id!=.
                replace z4_coef = ccc if SIC== `i' & year==`y' & country_id!=.
                replace cons_coef = ccc1 if SIC== `i' & year==`y' & country_id!=.
                drop aaa bbb ccc ccc1
                predict iii if SIC== `i' & year==`y' & country_id!=.
                predict jjj if SIC== `i' & year==`y' & country_id!=., resid
                replace fitted = iii if SIC== `i' & year==`y' & country_id!=.
                replace resi = jjj  if SIC== `i' & year==`y' & country_id!=.
                drop iii jjj
                ereturn list 
                gen ggg = e(r2) if SIC== `i' & year==`y' & country_id!=.
                gen hhh = e(r2_a) if SIC== `i' & year==`y' & country_id!=.
                replace R2 = ggg if SIC== `i' & year==`y' & country_id!=.
                replace AjR2 = hhh if SIC== `i' & year==`y' & country_id!=.
                drop ggg hhh
                gen kkk = e(N) if SIC== `i' & year==`y' & country_id!=.
                gen mmm = _N if SIC== `i' & year==`y' & country_id!=.
                replace n1 = kkk if SIC== `i' & year==`y' & country_id!=.
                replace n2 = mmm if SIC== `i' & year==`y' & country_id!=.
                drop kkk mmm
            }
        }
    }

    Comment

    Working...
    X