*! version 3.0.0 *! findlag -- estimate lag length of dependent variable *! Sean Becketti, April 1991. program define findlag version 3.0 local varlist "req ex min(1)" local weight "aweight fweight" local options "noCons Detail Nlags(int 4) Robust Zero *" parse "`*'" if (`nlags'<=0) { di in red "nlags must be positive" findlag exit } local ifcons = "`cons'"=="" /* is there a constant? */ /* Save the input data set, then drop the unwanted observations. This procedure guarantees that unwanted observations do not creep into the lags. */ tempfile tmpfile local dsn "$S_FN" quietly save `tmpfile', replace capture { if "`weight'"!="" { tempvar wgt gen float `wgt' `exp' local weight "[`weight'=`wgt']" keep `varlist' `wgt' } else keep `varlist' /* Create the lags and drop the observations where the longest lag is a missing value. This ensures that each regression uses the same number of observations. */ parse "`varlist'", parse(" ") local yvar "`1'" lag `nlags' `yvar', s(y) drop if L`nlags'.y == . /* Run a regression to obtain the number of observations. */ reg `varlist' `weight', `options' `cons' if _result(1)==0 | _result(1)==. { error 2000 } local nobs = _result(1) noisily { if "`detail'"~="" { di _n in gr "(obs=`nobs')" di in gr /* */ "Lags nvars RMSE AIC PC SC P(all) P(last)" di in gr _dup(74) "-" } else { di _n in gr /* */ "RMSE AIC PC SC (obs=`nobs')" di in gr "---------------------" } _ts_flag `varlist' `weight', reg(regress) /* */ nlags(`nlags') /* */ ifcons(`ifcons') `detail' `zero' `options' `cons' if "`robust'"!="" { di _n in gr "Biweight estimates" if "`detail'"~="" { di in gr /* */ "Lags nvars RMSE AIC PC SC P(all) P(last)" di in gr _dup(74) "-" } else { di in gr /* */ "RMSE AIC PC SC (obs=`nobs')" di in gr "---------------------" } _ts_flag `varlist', reg(rreg) nlags(`nlags') /* */ ifcons(`ifcons') `detail' `zero' /* */ `options' `cons' } } } local rc=_rc qui use `tmpfile', clear mac def S_FN "`dsn'" cap erase `tmpfile' error `rc' end