Thanks a ton. I am now able to run OLS. I would appreciate you if you help me on another GAUSS code (causality test designed by Anne PEGUIN-FEISSOLLE and Timo TERASVIRTA, 2000) .
I am uploading the codes. I want to run this codes on my variables (nifty and mid). The codes are written on random variables which are generated. However, I would like to run this on my real variables. Kindly navigate me in doing this. I would be grateful to you for this.
new ; cls;
/**********************************************************************
Causality tests: Anne PEGUIN-FEISSOLLE and Timo TERASVIRTA (2000)
A general framework for testing the Granger noncausality hypothesis,
SSE/EFI Working Paper Series in Economics and Finance, n° 343, Stockholm
School of Economics, Email: [email protected]
May 2010 - Can be applied on economic series
***********************************************************************/
timedeb = time;
library pgraph ;
/*0000000000000000000000000000000000000000000000000000000000000000000000*/
/*00000000000000000 YOU CAN CHANGE SOME PARTS BELOW 0000000000000000000*/
/*0000000000000000000000000000000000000000000000000000000000000000000000*/
@ Parameters for testing @
qretards = 2 ; /* Nb of lags of the endogenous y for testing */
nretards = 3 ; /* Nb of lags of the exogenous x for testing */
ktaylor = 3 ; /* Order k the Taylor expansion */
qqcp = 20 ; /* Nb of hidden units in ANN test */
cpseuil = 0.80 ; /* Threshold of princ. comp. */
@ Generating or reading
- the endogenous vector yzero of dimension (T x 1)
- the exogenous vector vectorX of dimension Tx1 @
T = 200 ; /* Number of observations */
seedn=982674 ; seedu=68372 ; seednh0=45914 ; seednh1=seednh0 ;
/* generation of the exogenous vector vectorX of dimension Tx1 */
vectorX=zeros(T+100+nretards,1);
i=2;
do while i <= T+100+nretards;
vectorX[i,1]=0.7*vectorX[i-1,1]+rndns(1,1,seedn);
i=i+1;
endo;
/* generation of the endogenous vector yzero of dimension (T x 1) */
/* yzero = NLIN ; without causality */
yzero = NLINX(vectorX) ; /* with causality */
/*0000000000000000000000000000000000000000000000000000000000000000000000*/
/*0000000000000000000 DO NOT CHANGE AFTER THIS LINE 00000000000000000000*/
/*0000000000000000000000000000000000000000000000000000000000000000000000*/
@ Computation of the matrices of lagged endogenous and exogenous variables @
/* creation of xxx (T x nretards) */
xxx0=MATXXX(vectorX) ;
xxx=xxx0[100+1+nretards:rows(xxx0),1:nretards] ;
/* generation of yyy (T x qretards) */
yyy = MATYYY ;
yzero=yzero[100+1+nretards:rows(yzero),.] ;
yyy=yyy[100+1+nretards:rows(yyy),.] ;
@ Printing of parameters @
print "/**********************************************************************
Causality tests: Anne PEGUIN-FEISSOLLE and Timo TERASVIRTA (2000)
A general framework for testing the Granger noncausality hypothesis,
SSE/EFI Working Paper Series in Economics and Finance, n° 343, Stockholm
School of Economics, Email: [email protected]
***********************************************************************/" ;
format /mat /mb1 /rd 5,0 ;
print ; print "Some parameters: ";
print " - Number of observations* = " rows(yzero) ;
print " - Number of lags of the endogenous y for testing = " qretards ;
print " - Number of lags of the exogenous x for testing = " nretards ;
print " - Order k of Taylor expansion = " ktaylor ;
print " - Number of hidden units in ANN test = " qqcp ;
format /mat /mb1 /rd 7,2 ;
print " - Threshold for principal components = " cpseuil ;
print "(*: after tre creation of the matrices of lagged endogenous
and exogenous variables)";
@ Computation of some matrices @
/* Taylor series expansion for lagged x only */
xtaylor = TAYLOR(xxx,xxx) ;
santafe = xxx~xtaylor ;
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
i=1 ;
do while i = cpseuil ; goto labelstopdebu1 ;
endif ;
i=i+1 ;
endo;
labelstopdebu1: ;
onlyxtaylorallcp = santafecp[.,1:i] ;
/* Taylor series expansion for lagged y only */
ytaylor = TAYLOR(yyy,yyy) ;
ytaylorall = ones(rows(ytaylor),1)~yyy~ytaylor ;
santafe = yyy~ytaylor ;
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
i=1 ;
do while i = cpseuil ; goto labelstop1 ;
endif ;
i=i+1 ;
endo;
labelstop1: ;
ytaylorallcp = santafecp[.,1:i] ;
ytaylorallcp = ones(rows(ytaylor),1)~ytaylorallcp ;
/* Taylor series expansion mixing lagged x and y */
xynavajo= TAYLOR(xxx~yyy,xxx~yyy) ;
xytaylor = TAYXY ;
/* Taylor series terms with at least one lagged x inside */
xytaylorall = xxx~xytaylor~xtaylor ;
santafe = xytaylorall ;
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
i=1 ;
do while i = cpseuil ; goto labelstop2 ;
endif ;
i=i+1 ;
endo;
labelstop2: ;
xytaylorallcp = santafecp[.,1:i] ;
@ Testing @
/* tests DIRECTLY*/
{ statgranger, pdgranger } = GRANGER ;
{ statgeneral, pdgeneral } = GENERALCP ;
{ statneural, pdneural } = NEURALCP ;
{ statadditive, pdadditive } = ADDITIVECP ;
/* ------------ STRATEGY : LINEARITY all param ass. with other terms than 1 ,
simple lagged x and lagged y are zeros */
plinearCP = LINEARITYCP ;
if plinearcp >= 0.05 ;
/*print " ------------> accept linearity " ; */
goto labelgrangerh0 ;
else ; /*print "------------> accept non linearity " ;*/
goto labelgeneralh0 ;
endif ;
/* tests */
labelgeneralh0:;
{ statunderh0, punderh0 } = GENERALCP ;
goto labelnextih0 ;
labelgrangerh0:;
{ statunderh0, punderh0 } = GRANGER ;
labelnextih0:;
@ Printing of results @
format /mat /mb1 /rd 8,4 ;
print ; print"F-versions of the different causality tests:" ;
print " stat pvalue ";
print "Linear Granger " statgranger pdgranger ;
print "General Taylor-based " statgeneral pdgeneral ;
print "Semi-additive Taylor-based " statadditive pdadditive ;
print "P-General Taylor based " statunderh0 punderh0 ;
print "ANN-based " statneural pdneural ;
print "(if pvalue reject H0 (H0: non causality) --> accept causality)";
print;
end;
/**************************************************************************/
/**************************** PROCEDURES ****************************/
/**************************************************************************/
PROC(1)=NLIN ; /*generation of y without causality*/
local uu1,i ;
uu1=rndns(100+nretards+T,1,seednh0);
yzero=zeros(100+nretards+T,1);
i=6; /* <--------------------------------------------------- TO CHANGE IF NECESSARY -------*/
do while i<=100+nretards+T;
yzero[i]= 0.5+10/(1+exp(-2*(-0.05+1*yzero[i-1]-3*yzero[i-2]) ))+0.001*uu1[i] ;
i=i+1;
endo;
retp(yzero);
ENDP;
proc(1)=NLINX(xx) ; /*generation of y with causality*/
local uu1,i,x1, maxofx ;
uu1=rndns(100+nretards+T,1,seednh1);
yzero=zeros(100+nretards+T,1);
maxofx = nretards ;
x1=zeros(rows(xx),maxofx);
/*generation de x1=matrice des nretards retards simples*/
i=1;
do while i <= maxofx ;
x1[.,i]=lagn(xx,i);
i=i+1;
endo;
xxx0 = x1;
i=6; /* <--------------------------------------------------- TO CHANGE IF NECESSARY -------*/
do while i<=100+nretards+T;
yzero[i]= 0.5+0.1*yzero[i-1]-0.3*yzero[i-2]+0.1*xxx0[i,1]-0.2*xxx0[i,2]+
0.100*uu1[i] ;
/*
yzero[i]= 0.5+ln(10*(0.1*yzero[i-1]^2+0.3*yzero[i-2]^2+
0.1*xxx0[i,1]^2+0.2*xxx0[i,2]^2+0.3*xxx0[i,3]^2))+0.100*uu1[i] ;
yzero[i]= 0.5+0.1*yzero[i-1]+0.5*uu1[i-1]*xxx0[i,1]+1*uu1[i] ;
yzero[i]= 0.5+100/(1+exp(-2*(-0.05+1*yzero[i-1]-3*yzero[i-2])
+ 100/(1+exp(xxx0[i,1]-2*xxx0[i,2]+3*xxx0[i,3]))))+0.001*uu1[i] ;
yzero[i]= (0.02-0.9*yzero[i-1]+0.795*yzero[i-2]+xxx0[i,1]+0.9*xxx0[i,3])/
(1+exp(-10*(yzero[i-1]-0.02)))+0.1*uu1[i] ;
yzero[i]= 0.5+0.1*yzero[i-1]-0.3*yzero[i-2]+0.1*xxx0[i,1]-0.2*xxx0[i,2]+
0.100*uu1[i] ;
*/
i=i+1;
endo;
retp(yzero);
ENDP;
PROC(1)=SEUIL; /*generation of y with causality*/
local uu1,i ;
uu1=rndns(100+nretards+T,1,seedn);
yzero=zeros(100+nretards+T,1);
i=4; /* <--------------------------------------------------- TO CHANGE IF NECESSARY -------*/
do while i<=100+nretards+T;
if uu1[i-1] <0;
yzero[i]=0.1*yzero[i-1]+0.01*uu1[i] ;
else;
yzero[i]=-0.5*yzero[i-1]+0.01*uu1[i] ;
endif;
i=i+1;
endo;
retp(yzero);
ENDP;
PROC(1)=SEUILX; /*generation of y with causality*/
local uu1,i ;
uu1=rndns(100+nretards+T,1,seedn);
yzero=zeros(100+nretards+T,1);
i=4; /* <--------------------------------------------------- TO CHANGE IF NECESSARY -------*/
do while i<=100+nretards+T;
/* if yzero[i-1] <0;
yzero[i]=0.1*yzero[i-1]+0.03*xxx0[i,1]^2+0.1*uu1[i] ;
else;
yzero[i]=-0.5*yzero[i-1]+0.1*uu1[i] ;
endif;*/
if uu1[i-1] <0;
yzero[i]=0.1*yzero[i-1]+10*xxx0[i,1]^2+0.01*uu1[i] ;
else;
yzero[i]=-0.5*yzero[i-1]+0.01*uu1[i] ;
endif;
i=i+1;
endo;
retp(yzero);
ENDP;
PROC(1) = LINEARITY ;
local exog, eps, SSR0, zm, nu, SSR, statcau, pstatcau ;
exog=ones(rows(xxx),1)~xxx~yyy ;
eps = yzero-exog*inv(exog'exog)*exog'yzero ;
SSR0=eps'eps ;
zm=exog~ytaylor~xtaylor~xytaylor ;
nu=eps-zm*inv(zm'zm)*zm'eps ;
SSR=nu'nu ;
statcau = ( (SSR0-SSR) / (cols(zm)-cols(exog)) ) / (SSR / (rows(zm)-cols(zm) ) ) ;
pstatcau = cdffc( statcau, (cols(zm)-cols(exog)),rows(zm)-cols(zm) );
retp(pstatcau) ;
ENDP ;
PROC(1) = LINEARITYCP ;
local exog, eps, SSR0, zm, nu, SSR, statcau, pstatcau , xxxcp , santafe , santafecp , vertigo , anon ;
exog=ones(rows(xxx),1)~xxx~yyy ;
eps = yzero-exog*inv(exog'exog)*exog'yzero ;
SSR0=eps'eps ;
santafe = ytaylor~xtaylor~xytaylor ;
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
i=1 ;
do while i = cpseuil ; goto labelstop3 ;
endif ;
i=i+1 ;
endo;
labelstop3: ;
xxxcp = santafecp[.,1:i] ;
zm=exog~xxxcp ;
nu=eps-zm*inv(zm'zm)*zm'eps ;
SSR=nu'nu ;
statcau = ( (SSR0-SSR) / ( cols(xxxcp) ) ) / (SSR / (rows(zm)-cols(zm) ) ) ;
pstatcau = cdffc( statcau, cols(xxxcp) ,rows(zm)-cols(zm) );
retp(pstatcau) ;
ENDP ;
proc(2) = ADDITIVECP ;
local exog, eps, SSR0, zm, nu, SSR, statcau, pstatcau ;
exog=ytaylorallcp ;
eps = yzero-exog*inv(exog'exog)*exog'yzero ;
SSR0=eps'eps ;
zm=ytaylorallcp~onlyxtaylorallcp ;
nu=eps-zm*inv(zm'zm)*zm'eps ;
SSR=nu'nu ;
statcau = ( (SSR0-SSR) / cols(onlyxtaylorallcp) ) / (SSR / (rows(zm)-cols(zm) ) ) ;
pstatcau = cdffc( statcau, cols(onlyxtaylorallcp),rows(zm)-cols(zm) );
retp(statcau, pstatcau) ;
ENDP ;
proc(2) = GENERALCP ;
local exog, eps, SSR0, zm, nu, SSR, statcau, pstatcau ;
exog=ytaylorallcp ;
eps = yzero-exog*inv(exog'exog)*exog'yzero ;
SSR0=eps'eps ;
zm=ytaylorallcp~xytaylorallcp ;
nu=eps-zm*inv(zm'zm)*zm'eps ;
SSR=nu'nu ;
statcau = ( (SSR0-SSR) / cols(xytaylorallcp) ) / (SSR / (rows(zm)-cols(zm) ) ) ;
pstatcau = cdffc( statcau, cols(xytaylorallcp),rows(zm)-cols(zm) );
retp(statcau, pstatcau) ;
ENDP ;
proc(2)=GRANGER ; /* calcul de la p-value du test en F de noncausalite lineaire */
local exog,eps,SSR0,zm,nu,SSR,grangerF1,pgrangerF1;
exog=ones(rows(yzero),1)~yyy ;
eps = yzero-exog*inv(exog'exog)*exog'yzero ;
SSR0=eps'eps ;
zm=exog~xxx ;
nu=eps-zm*inv(zm'zm)*zm'eps ;
SSR=nu'nu ;
/*calcul de la statistique de test*/
grangerF1=((SSR0-SSR)/cols(xxx))/(SSR/(T-cols(zm))) ;
pgrangerF1=cdffc(grangerF1,cols(xxx),t-cols(zm)) ;
retp(grangerF1, pgrangerF1) ;
ENDP ;
proc(2)=NEURALCP;
local exog , eps , SSR0 , xn , nn , wn , gam , bid , psi , ecap , SSR ;
local neural1, cp , vertigo, anon , pneural1b , santafe , santafecp ;
exog=ytaylorallcp ;
eps = yzero-exog*inv(exog'exog)*exog'yzero ;
SSR0=eps'eps ;
xn = ones(rows(eps),1)~xxx ;
label1:;
nn=1;
wn=zeros(rows(xn),1);
do while nn<=qqcp;
gam=-2+4*rndus(cols(xn),1,seedu);
bid=xn*gam;
psi=1/(1+exp(-bid));
wn=wn~psi;
nn=nn+1;
endo;
psi=wn[.,2:cols(wn)];
santafe = psi ;
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
i=1 ;
do while i = cpseuil ; goto labelstop6 ;
endif ;
i=i+1 ;
endo;
labelstop6: ;
cp = santafecp[.,1:i] ;
wn = exog~xxx~cp;
ecap=eps-wn*(inv(wn'wn))*wn'eps;
SSR=ecap'ecap;
neural1=((SSR0-SSR)/(cols(wn)-cols(exog)))/(SSR/(rows(wn)-cols(wn)));
if neural1 <= 0 ;
goto label1;
endif;
pneural1b=cdffc(neural1,cols(wn)-cols(exog),rows(wn)-cols(wn));
retp(neural1, pneural1b);
ENDP;
PROC(1)=TAYXY ;
local xygarder , ytaylorout , inavajo, iy , xtaylorout ;
xygarder = ones(rows(xynavajo),1)~xynavajo~ones(rows(xynavajo),1) ;
ytaylorout = ones(rows(xygarder),1)~ytaylor~ones(rows(xynavajo),1) ;
inavajo = 2 ;
do while inavajo <= cols(xygarder)-1 ;
iy=2 ;
do while iy <= cols(ytaylorout)-1 ;
if xygarder[.,inavajo] == ytaylorout[.,iy] ;
xygarder = xygarder[.,1:inavajo-1]~xygarder[.,inavajo+1:cols(xygarder)] ;
ytaylorout = ytaylorout[.,1:iy-1]~ytaylorout[.,iy+1:cols(ytaylorout)] ;
goto label1 ;
endif ;
iy=iy+1;
endo;
inavajo = inavajo + 1 ;
label1: ;
endo ;
xtaylorout = ones(rows(xygarder),1)~xtaylor~ones(rows(xynavajo),1) ;
inavajo = 2 ;
do while inavajo <= cols(xygarder)-1 ;
iy=2 ;
do while iy <= cols(xtaylorout)-1 ;
if xygarder[.,inavajo] == xtaylorout[.,iy] ;
xygarder = xygarder[.,1:inavajo-1]~xygarder[.,inavajo+1:cols(xygarder)] ;
xtaylorout = xtaylorout[.,1:iy-1]~xtaylorout[.,iy+1:cols(xtaylorout)] ;
goto label2 ;
endif ;
iy=iy+1;
endo;
inavajo = inavajo + 1 ;
label2: ;
endo ;
xytaylor = xygarder[.,2:cols(xygarder)-1] ;
retp(xytaylor);
ENDP;
PROC(1)=TAYLOR(yprime,ysecond) ;
local zy , i1, navajo1, i2, navajo2 , i3, navajo3 , itaylor ;
zy = ones(rows(yprime),1) ;
i1=1;
do while i1 <= cols(yprime) ;
navajo1 = yprime[.,i1] ;
i2=i1;
do while i2 <= cols(ysecond) ;
navajo2 = navajo1.*ysecond[.,i2] ;
zy = zy~navajo2 ;
itaylor = 3 ;
do while itaylor <= ktaylor ;
i3=i2 ;
do while i3 <= cols(ysecond) ;
navajo3 = navajo2.*ysecond[.,i3] ;
zy = zy~navajo3 ;
i3 = i3+1;
endo;
itaylor = itaylor + 1 ;
endo;
i2 = i2+1 ;
endo;
i1=i1+1 ;
endo;
zy = zy[.,2:cols(zy)] ;
retp(zy);
ENDP;
proc(1)=MATXXX(xx) ; /*creation de la matrice des x */
local xx, x1,i , maxofx ;
maxofx = nretards ;
x1=zeros(rows(xx),maxofx);
/*generation de x1=matrice des nretards retards simples*/
i=1;
do while i <= maxofx ;
x1[.,i]=lagn(xx,i);
i=i+1;
endo;
retp(x1);
endp;
PROC(1)=MATYYY; /*creation de la matrice des y */
local yy,i;
yy=yzero;
i=1;
do while i <= qretards;
yy=yy~lagn(yzero,i) ;
i=i+1;
endo;
yy=yy[.,2:cols(yy)] ;
retp(yy);
ENDP;
6 Answers
0
There is a problem with this bit of code:
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
i=1 ;
do while i = cpseuil ; goto labelstopdebu1 ;
endif ;
i=i+1 ;
endo;
labelstopdebu1: ;
onlyxtaylorallcp = santafecp[.,1:i] ;
I will assume that this code is intended to calculate the minimum number of principal component vectors needed to explain 80% (cpseuil is set to 0.8 earlier in the code) of the variance of santafe.
The second return from the GAUSS princomp function is a vector with the percent of variance explained by each principal component vector. If we apply the cumsumc function to this output, we will get a vector of cumulative variance explained. For example, if:
//Made up percent variance explained
var_exp = { 0.62, 0.19, 0.09, 0.06, 0.04 };
//Create cumulative percent variance explained
cs_var_exp = cumsumc(var_exp);
print cs_var_exp;
will return:
0.6200 0.8100 0.9000 0.9600 1.0000
Now we need to select the number of principal components needed to explain at least 80% of the variance. There are any number of ways to do this, but what we will do is: 1) Create a vector of 1's and 0's with a 1 at every location that is less than or equal to 0.8. 2) Sum this vector. 3) Add one. Continuing with our example from above:
//Create logical vector of 1's and 0's
num_vars = cs_var_exp .< 0.8;
print "logical vector = ";
print num_vars;
//Sum the elements of 'num_vars' and
//add one to find the minimum number of vars
num_vars = sumc(num_vars) + 1;
print "Minimum number of variables = ";
print num_vars;
The above snippet of code should return:
logical vector = 1.0000 0.0000 0.0000 0.0000 0.0000 Minimum number of variables = 2.0000
With that information, we can change this:
//Calculate principal components
//and percent variance explained
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
//Calculate minimum number of principal components
//to explain 80% of variance
i=1 ;
do while i = cpseuil ; goto labelstopdebu1 ;
endif ;
i=i+1 ;
endo;
labelstopdebu1: ;
//Create new matrix with needed principal components
onlyxtaylorallcp = santafecp[.,1:i] ;
to this:
//Calculate principal components
//and percent variance explained
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
//Calculate minimum number of principal components
//to explain 80% of variance
//Create cumulative percent variance explained
cs_var_exp = cumsumc(vertigo);
//Create logical vector of 1's and 0's
num_vars = cs_var_exp .< 0.8;
//Sum the elements of 'num_vars' and
//add one to find the minimum number of vars
num_vars = sumc(num_vars) + 1;
//Create new matrix with needed principal components
onlyxtaylorallcp = santafecp[.,1:num_vars] ;
Or more compactly:
//Calculate principal components
//and percent variance explained
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe));
//Calculate num principal components needed
//to explain 'cpseuil' percent variance
num_vars = sumc(cumsumc(vertigo) .< cpseuil) + 1;
//Create new matrix with needed principal components
onlyxtaylorallcp = santafecp[.,1:num_vars] ;
0
This looks pretty well now. Kindly compile all the codes and run them on nifty=vectorX and mid=yzero whether they are giving output.
0
I have downloaded the code that you have given in the above link. However, I am not able to get output when I run the codes on variables. I have done up to the point; nifty=myvars[.,1], mid=[.,2]
What are the steps I need to follow after these? As I am new to Gauss, I need your kind support to run these codes, as the results are important for my study.
0
If you want the code to use your data, instead of the simulated data, then you need to set the variables that the code uses equal to your data. For example, assuming that earlier in the file, you loaded a column vector named nifty, and that you want vectorX to be equal to nifty instead of the simulated values, you would replace this code:
/* generation of the exogenous vector vectorX of dimension Tx1 */
vectorX=zeros(T+100+nretards,1);
i=2;
do while i <= T+100+nretards;
vectorX[i,1]=0.7*vectorX[i-1,1]+rndns(1,1,seedn);
i=i+1;
endo;
with this code:
//From now on 'vectorX' will contain the data from nifty
vectorX = nifty;
You will also have to set the variable T to the correct length as well.
0
HELP REQUIRED:
Can anyone suggest how to save the output from this code in a single excel file?
I am new to GAUSS and need to run this code multiple times to estimate the causalities.
I want to be able to save the outputs in a single excel file to later use.
I would really appreciate the help.
0
You can use the xlsWrite
command which is described in this tutorial to write data from GAUSS to an Excel file.
Your Answer
6 Answers
There is a problem with this bit of code:
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
i=1 ;
do while i = cpseuil ; goto labelstopdebu1 ;
endif ;
i=i+1 ;
endo;
labelstopdebu1: ;
onlyxtaylorallcp = santafecp[.,1:i] ;
I will assume that this code is intended to calculate the minimum number of principal component vectors needed to explain 80% (cpseuil is set to 0.8 earlier in the code) of the variance of santafe.
The second return from the GAUSS princomp function is a vector with the percent of variance explained by each principal component vector. If we apply the cumsumc function to this output, we will get a vector of cumulative variance explained. For example, if:
//Made up percent variance explained
var_exp = { 0.62, 0.19, 0.09, 0.06, 0.04 };
//Create cumulative percent variance explained
cs_var_exp = cumsumc(var_exp);
print cs_var_exp;
will return:
0.6200 0.8100 0.9000 0.9600 1.0000
Now we need to select the number of principal components needed to explain at least 80% of the variance. There are any number of ways to do this, but what we will do is: 1) Create a vector of 1's and 0's with a 1 at every location that is less than or equal to 0.8. 2) Sum this vector. 3) Add one. Continuing with our example from above:
//Create logical vector of 1's and 0's
num_vars = cs_var_exp .< 0.8;
print "logical vector = ";
print num_vars;
//Sum the elements of 'num_vars' and
//add one to find the minimum number of vars
num_vars = sumc(num_vars) + 1;
print "Minimum number of variables = ";
print num_vars;
The above snippet of code should return:
logical vector = 1.0000 0.0000 0.0000 0.0000 0.0000 Minimum number of variables = 2.0000
With that information, we can change this:
//Calculate principal components
//and percent variance explained
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
//Calculate minimum number of principal components
//to explain 80% of variance
i=1 ;
do while i = cpseuil ; goto labelstopdebu1 ;
endif ;
i=i+1 ;
endo;
labelstopdebu1: ;
//Create new matrix with needed principal components
onlyxtaylorallcp = santafecp[.,1:i] ;
to this:
//Calculate principal components
//and percent variance explained
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe)) ;
//Calculate minimum number of principal components
//to explain 80% of variance
//Create cumulative percent variance explained
cs_var_exp = cumsumc(vertigo);
//Create logical vector of 1's and 0's
num_vars = cs_var_exp .< 0.8;
//Sum the elements of 'num_vars' and
//add one to find the minimum number of vars
num_vars = sumc(num_vars) + 1;
//Create new matrix with needed principal components
onlyxtaylorallcp = santafecp[.,1:num_vars] ;
Or more compactly:
//Calculate principal components
//and percent variance explained
{ santafecp , vertigo, anon } = princomp(santafe,cols(santafe));
//Calculate num principal components needed
//to explain 'cpseuil' percent variance
num_vars = sumc(cumsumc(vertigo) .< cpseuil) + 1;
//Create new matrix with needed principal components
onlyxtaylorallcp = santafecp[.,1:num_vars] ;
This looks pretty well now. Kindly compile all the codes and run them on nifty=vectorX and mid=yzero whether they are giving output.
I have downloaded the code that you have given in the above link. However, I am not able to get output when I run the codes on variables. I have done up to the point; nifty=myvars[.,1], mid=[.,2]
What are the steps I need to follow after these? As I am new to Gauss, I need your kind support to run these codes, as the results are important for my study.
If you want the code to use your data, instead of the simulated data, then you need to set the variables that the code uses equal to your data. For example, assuming that earlier in the file, you loaded a column vector named nifty, and that you want vectorX to be equal to nifty instead of the simulated values, you would replace this code:
/* generation of the exogenous vector vectorX of dimension Tx1 */
vectorX=zeros(T+100+nretards,1);
i=2;
do while i <= T+100+nretards;
vectorX[i,1]=0.7*vectorX[i-1,1]+rndns(1,1,seedn);
i=i+1;
endo;
with this code:
//From now on 'vectorX' will contain the data from nifty
vectorX = nifty;
You will also have to set the variable T to the correct length as well.
HELP REQUIRED:
Can anyone suggest how to save the output from this code in a single excel file?
I am new to GAUSS and need to run this code multiple times to estimate the causalities.
I want to be able to save the outputs in a single excel file to later use.
I would really appreciate the help.
You can use the xlsWrite
command which is described in this tutorial to write data from GAUSS to an Excel file.