A loop of cross-sectional regressions for calculating abnormal accruals in Stata

I write a loop of cross-sectional regressions for calculating abnormal accruals. This program can be easily modified and replaced with Jones, modified Jones, or Dechow and Dichev model.

I add detailed comments in the program to help you prepare the input file.

This entry was posted in Stata. Bookmark the permalink.

5 Responses to A loop of cross-sectional regressions for calculating abnormal accruals in Stata

  1. Renee Adebile says:

    I am using your codes to calculate DA. However, I get ” Invalid Syntax”. Can you please help me figure out what I do wrong.
    this the codes I used;
    forvalues i=1/num' {
    regress ta x1 x2 x3 x4 x5 if groupid==
    i’
    predict resid if groupid==i', residuals
    replace da=resid if groupid==
    i’
    drop resid
    replace regobs=e(N) if groupid==i'
    replace df=e(df_r) if groupid==
    i’
    replace r2a=e(r2_a) if groupid==i'
    foreach x in x1 x2 x3 x4 x5 _cons {
    replace
    x’_b=_b[x'] if groupid==i’
    replace x'_se=_se[x’] if groupid==`i’
    }
    }

  2. Renee Adebile says:

    those are the entire codes I used:
    * Remove obs without required data
    foreach v in ta x1 x2 x3 x4 x5 {
    drop if v'==.
    }

    * Handle outliers for each industry year
    ge trunc=0
    local vlist "ta x1 x2 x3 x4 x5"
    egen groupid=group(fyear sic_2)
    sum groupid, meanonly
    local num=r(max)
    forvalues i=1/
    num’ {
    foreach v in vlist' {
    _pctile
    v’ if groupid==i', p(1 99)
    replace trunc=1 if groupid==
    i’ & (v'r(r2)) //drop top and bottom 1%. You can do winsorize as well
    }
    }
    drop if trunc==1
    drop trunc groupid

    * Require at least 20 obs for each estimation
    bysort groupid: egen obsnum=count(fyear)
    keep if obsnum>=20
    drop groupid
    egen groupid=group(fyear sic_2)
    sum groupid, meanonly
    local num=r(max)

    * Run cross-sectional regressions
    ge da=.
    ge regobs=.
    ge df=.
    ge r2a=.

    foreach x in x1 x2 x3 x4 x5 _cons {
    ge
    x’_b=.
    ge x'_se=.
    }

    forvalues i=1/num’ {
    regress ta x1 x2 x3 x4 x5 if groupid==i'
    predict resid if groupid==
    i’, residuals
    replace da=resid if groupid==i'
    drop resid
    replace regobs=e(N) if groupid==
    i’
    replace df=e(df_r) if groupid==i'
    replace r2a=e(r2_a) if groupid==
    i’
    foreach x in x1 x2 x3 x4 x5 _cons {
    replace x'_b=_b[x’] if groupid==i'
    replace
    x’_se=_se[x']if groupid==i’
    }
    }

    save da, replace

    Note that when I run all the entire codes, I get da regobs r2a x1_b for the first regression. and then I get ” invalid syntax”.

  3. Suleman says:

    I am working on discretionary accrual model. I am having problem with calculating the Total accrual and PPE in Stata . I used this command :gen TAC =(NI-CFO)/lagged TA and Gross property plant and equipment using the following command :gen var name =PPE/lagged TA. How do I go about it please

    • Kai Chen says:

      Do you mean you don’t know how to calculate lagged TA? If this is your problem, check the command tsset in Stata documentation. Stata is very powerful in calculating lagged value or changes in value.

Leave a Reply

Your email address will not be published. Required fields are marked *