*! bgtest V1.03 C F Baum/Vince Wiggins 11Aug2002 * 1.0.2 15 Oct 2000: zerofill lagged residuals per Greene 2000 * 1.0.3 11 Aug 2002: add force. disallow use with robust, predict double res program define bgtest, rclass syntax [if] [in] [,Lags(integer 1) FORCE] version 6.0 if "`e(cmd)'" ~= "regress" & "`e(cmd)'" ~= "newey" { error 301 } if "`e(vcetype)'" ~= "" & "`force'" == "" { di as error "force option must be used with robust standard errors." error 198 } if `lags'<1 | `lags' > 0.25*e(N) { dis in red "Error: lags must be positive and <25% of N." error 301 } tempname b why yhat res regest pred /* get regressorlist from previous regression */ mat `b' = e(b) local varlist : colnames `b' local varlist : subinstr local varlist "_cons" "", /* */ word count(local hascons) marksample touse if "`sample'" == "" { qui replace `touse' = 0 if !e(sample) } /* get time variables */ _ts timevar, sort markout `touse' `timevar' /* calc residuals */ qui predict double `yhat' if `touse' , xb qui gen double `res' = `e(depvar)' - `yhat' tsreport if `touse', report return scalar N_gaps = r(N_gaps) return scalar N = e(N) - r(N_gaps) return scalar k = e(N) - e(df_r) /* regress resids on lagged resids to * order `lags' and regressorlist */ tempvar touse gen byte `touse' = e(sample) tsrevar l(1/`lags').`res' local reslist `r(varlist)' tokenize `reslist' local i 1 while "``i''" != "" { qui replace ``i'' = 0 if ``i'' == . local i = `i' + 1 } estimates hold `regest' if !`hascons' { local cons "noconstant" } qui regress `res' `reslist' `varlist' if `touse', `cons' return scalar bg = e(N)*e(r2) return scalar df = `lags' return scalar p = chiprob(`lags',return(bg)) estimates unhold `regest' di in gr "Breusch-Godfrey LM statistic: " /* */ in ye %9.0g return(bg) in gr /* */ in gr " Chi-sq(" %2.0f return(df) ") P-value = " /* */ in ye %6.0g return(p) end exit