@========================================================================================================= X-differencing Estimator programmed by Donggyu Sul Version: May 8, 2012 Reference: 1. Han, C., P.C.B. Phillips and D. Sul, X-differencing and Dynamic Panel Model Estimation, Forthcoming in Econometric Theory 2011 2. ----------------, Uniform Asymptotic Normality in Stationary and Unit Root Autoregression, Econometric Theory, 2011, 1117--1151. 3. ----------------, Lag Length Selection for Panel Augtoregressive Models, 2012 Usage: Select lag length by using either consistent BIC or General to specific method based on X-differencing residuals. {k0,k1,k2,k3} = optlag(x,p) output: lag length. Use either k2 or k3. k0 => inconsistent BIC k1 => consistent but not robust BIC k2 => consistent and robust BIC k3 => General to specific method. Input: x => TxN matrix p => maximum lag length Estimation AR(p) coefficients by using X-differencing estimation {beta,trat,sig,ee}= xdiff0(x,p) Output: beta => AR(p) coefficients trat => T-ratio based on panel robust covariance estimation sig => sample variance of residuals ee => residuals. Input: x => TxN matrix p => Lag length =========================================================================================================================@ proc(4) = xdiff0(x,p); local t,n,i,nm1,dm1,y1,y2,y3,y4,y5,y6,y7,y; local ip,xx,beta,res,dm3,ee,ex,w,trat,sig,sigma; local res0,res1,res2,kk,tk,tm,ic1,ic2; local beta1,beta2,beta0,nm0,dm0,xx1,nm2,dm2,xx2,ii,yy,yx; t = rows(x); n = cols(x); y = x; if p == 0; beta = 0; trat = 20; sig = x[2:t,.] - meanc(x[2:t,.])'; sig = meanc(meanc(sig.*sig)); ee = x -meanc(x)'; goto final; endif; nm0 = 0; dm0 = 0; i = 1; do while i <= t-p-2; y1 = {}; ii = 0; do while ii <= p; y1 = y1~vec(y[p+1-ii+i:t-ii,.]-y[i+ii,.]); ii = ii + 1; endo; yy = y1[.,1]; yx = y1[.,2:cols(y1)]; nm0 = nm0 + yx'yy; dm0 = dm0 + yx'yx; i = i + 1; endo; beta = invpd(dm0)*nm0; dm3 = 0; i = 1; do while i <= t-p-2; y1 = y[p+1+i:t,.]-y[i,.]; ee = 0; ii = 1; do while ii <= p; y2 = y[p+1-ii+i:t-ii,.]-y[i+ii,.]; ee = ee + y2*beta[ii]; ii = ii + 1; endo; ee = y1 - ee; ex = {}; ii = 1; do while ii <= p; y2 = y[p+1-ii+i:t-ii,.]-y[i+ii,.]; ex = ex~sumc(ee.*y2); ii = ii + 1; endo; dm3 = dm3 + ex; i = i + 1; endo; dm3 = dm3'dm3; w = invpd(dm0)*dm3*invpd(dm0); w= sqrt(diag(w)); trat = beta./w; y1 = y[p+2:t,.]; ee = 0; ii = 1; do while ii <= p; y2 = y[p+2-ii:t-ii,.]; ee = ee + y2*beta[ii]; ii = ii + 1; endo; ee = y1 - ee; ee = ee - meanc(ee)'; sig = meanc(meanc(ee.*ee)); final: retp(beta,trat,sig,ee); endp; proc xdific(x,p); local t,n,i,nm1,dm1,y1,y2,y3,y4,y5,y6,y7,y; local ip,xx,beta,res,dm3,ee,ex,w,trat,sig,sigma; local res0,res1,res2,kk,tk,tm,ic1,ic2; local beta1,beta2,beta0,nm0,dm0,xx1,nm2,dm2,xx2,ii,yy,yx,ee1; t = rows(x); n = cols(x); y = x; nm0 = zeros(p,p*p); dm0 = nm0; i = 1; do while i <= t-p-2; y1 = vec(y[p+1+i:t,.]-y[i,.]); y2 = {}; ii = 1; do while ii <= p; y2 = y2~vec(y[p+1-ii+i:t-ii,.]-y[i+ii,.]); nm0[1:ii,1+(ii-1)*p:ii+(ii-1)*p] = nm0[1:ii,1+(ii-1)*p:ii+(ii-1)*p] + y2'y1; dm0[1:ii,1+(ii-1)*p:ii+(ii-1)*p] = dm0[1:ii,1+(ii-1)*p:ii+(ii-1)*p] + y2'y2; ii = ii + 1; endo; i = i + 1; endo; ee = {}; sig = zeros(p,1); y2 = {}; y1 = vec(y[p+1:t,.]); local tn,tt; ii = 1; do while ii <= p; beta = invpd(dm0[1:ii,1+(ii-1)*p:ii+(ii-1)*p])*nm0[1:ii,1+(ii-1)*p:ii+(ii-1)*p]; y2 = y2~vec(y[p+1-ii:t-ii,.]); ee1 = y1 - y2*beta; tn = rows(ee1); tt = tn/n; ee = zeros(tt,n); i = 1; do while i <= n; ee[.,i] = ee1[1+(i-1)*tt:tt*i,1]; i = i + 1; endo; ee = ee - meanc(ee)'; sig[ii] = meanc(meanc(ee.*ee)); ii = ii + 1; endo; y2 = y2 - meanc(y2)'; y2 = meanc(meanc(y2.*y2)); sig = y2|sig; retp(sig); endp; proc(4) = optlag(x,p); local sigma0,tratio,i,beta,trat,sig,sigma1,kk,tk,tm,ic0,ic1,ic2,ic3,magimak,ee; local alp,pv,cval,t,n; t = rows(x), n = cols(x); pv = 0.2; alp = exp( -ln(pv)*( 10/sqrt(n*t) ) ) /100; cval = cdfni(1-alp/2); sigma0 = zeros(p+1,1); tratio = zeros(p+1,1); i = 0; do while i <= p; {beta,trat,sig,ee} = xdiff0(x,i); sigma0[i+1] = sig; tratio[i+1] = trat[rows(trat)]; i = i + 1; endo; sigma1 = xdific(x,p); kk = seqa(0,1,p+1); tk = t - seqa(1,1,p+1); tm = t - p-1; ic0 = ln(sigma0) + kk.*ln(n.*tk)./(n.*tk); ic1 = ln(sigma0) + kk.*ln(sqrt(n).*tk)./(sqrt(n).*tk); ic2 = ln(sigma1) + kk.*ln(n.*tm)./(n.*tm); ic0 = minindc(ic0)-1; ic1 = minindc(ic1)-1; ic2 = minindc(ic2)-1; i = p+1; do while i >= 1; if abs(tratio[i]) > cval; ic3 = i-1; goto magimak; endif; i = i - 1; endo; ic3 = 0; magimak: retp(ic0,ic1,ic2,ic3); endp;