Help with code-please

Dear Users,


I have downloaded this gauss code below and want to apply it to my data. I was wondering if anyone could please help in setting up the file to use with my data.




X-differencing Estimator programmed by Donggyu Sul
Version: May 8, 2012


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

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
==========================================================[email protected]

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;

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));

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;

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;

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;


6 Answers


Dear Ozey,

This code is suited for estimating dynamic panel series models. This program works by first removing the fixed effects using x-differencing and then performing pooled least squares on the "transformed" series. These steps are all done internally within the procedure xdiff0.

The procedure xdiff0 requires two inputs, your panel data (x), and the number of AR lags (p). If you do not know the number of lags, this program file also includes the procedure optlag, which reports the optimal lag selection using the methods listed in the procedure comments.  In order to utilize this program, your panel series data should be put in a TxN matrix. In other words, row i represents an observation at time t=i and column k holds all observations for individual k.

The procedure provides four returns:

  • AR(p) coefficients
  • T-ratios based on robust covariance
  • sample variance of the residual
  • Residuals

The following example walks you through using both procedures. In this case, I will load panel series data directly from a GAUSS data set. The first column of this hypothetical data is a date column. The remaining columns house a balanced panel of data across 8 countries.

//Load the example panel data
//This data is a GAUSS data set with the .dat extension
t = data[.,1];
y = data[.,2:cols(data)];

After loading the data set the maximum number of lags used in finding the optimal lag length:

maxlags = 5;

Once the data is loaded and maximum lags are set, the following block of code finds and prints the optimal number of lags:

{k0,k1,k2,k3} = optlag(y,maxLags);
print "Inconsistent BIC optimal lag"; k0;
print "Consistent but not robust BIC"; k1;
print "Consistent and robust BIC"; k2;
print "General to specific method"; k3;
print "_______________________________________";
print "Best choice lag overall"; k2;
print " or"; k3;

Finally, we run the xdiff0 procedure

bestLag= k2;
{beta,trat,sig,ee}= xdiff0(y,bestLag);

and print the results to screen:

print "AR(1),..,AR(bestLag) coefficients"; beta';
print "t-ratios"; trat';
print "Sample variance of error:"; sig;

Hi Eclower,

Many thanks for the help.



Hi Eclower,

Sorry to bug you again.

I have tried to run the example you kindly sent and I am getting the following error:

I have tried to run the example you sent to understand how things work before using my own data; but I am having the following error:
Line 4 in C:\gauss13\examples\xdiff
Operand missing G0064
Line 4 in C:\gauss13\examples\xdiff
Syntax error G0008 : 't = data[.,1]'


I have checked my gauss example folder and don't have the file "y_panel". Please if it is not a problem could you please send me the data. I am using Gauss 13.




Hello Ozey,

I think you need to first change the line:

y = loadd("y_panel");

to assign to a variable named data, because that is the name of the variable that the lines below reference:

data = loadd("y_panel");

Then you need to change the code to the right of the equals sine to a line that loads your data. The GAUSS loadd command will load in data from a GAUSS dataset. If your data is in Excel format, then information on loading data from Excel into GAUSS can be found here.



Please note the original posting has been revised on 5/4/2015 to reflect the necessary adjustments noted above.



Many thanks. All works fine now.


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy