Converting a stata code into eviews
Posted: Tue Jan 21, 2014 4:36 am
Dear Sir/Madam,
I obtained this piece of Stata code from the authors of ‘What does the yield curve tell us about exchange rate predictability’.
I'm fine with most of the code, the remaining bit -which is a standalone part of the paper- is where I got stuck, and I was hoping you could give me a hand with it.
There have been several requests on the forum about the Nelson-Siegel model -the code below is precisely that0, so I think this will be very useful to other users (and pacify them).
I’ve include in squared brackets info on what the adjacent Stata commands do, but I presume you know that programming language already.
Lastly, this program was written for monthly data- how would the NS1 and NS2 have to be amended to handle weekly data (presumably the parameter 0.0609, which is from Diebold and Li 2006 would have to be re-estimated separately)?
Here it is:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
clear
#delimit;
(resets the character that marks the end of a command [assigns a semicolon to denote the end of a line]
set more 1;
[tells Stata not to wait for keyboard input before waiting to display the next screen of output -- you will not be able to read what Stata is doing as it scrolls by, but you can read the output in the log file.]
capture log close; [close a log if any is open and do nothing if no log is open]
set mem 400m;
set matsize 800; [sets the max amount of variables in the estimation]
use yields_data.dta, replace;
gen number=_n; [generates a number whose value is the sequential order of the observation in the dataset, starting from 1 from the Nst observation. _n is often used to generate unique codes for each observation]
reshape long y ycan yuk yjp, i(number) j(month);
[takes wide data files and reshapes them into long form]
gen level_ns=0;
gen slope_ns=0;
gen curvature_ns=0;
gen level_se_ns=0;
gen slope_se_ns=0;
gen curvature_se_ns=0;
gen r2_ns = 0;
gen diff=y-yuk;
gen ns1 =1;
gen ns2 = (1-exp(-0.0609*month))/(0.0609*month);
gen ns3 = (1-exp(-0.0609*month))/(0.0609*month) -exp(-0.0609*month);
foreach x of numlist 1/240 {;
regress diff ns1 ns2 ns3 if number==`x', robust noconstant ;
replace level_ns = _b[ns1] if number==`x';
replace slope_ns = _b[ns2] if number==`x';
replace curvature_ns = _b[ns3] if number==`x';
replace level_se_ns = _se[ns1] if number==`x';
replace slope_se_ns = _se[ns2] if number==`x';
replace curvature_se_ns = _se[ns3] if number==`x';
replace r2_ns = e(r2_a) if number==`x';
};
drop ns1 ns2 ns3;
reshape wide diff y ycan yuk yjp , i(number) j(month);
outsheet using NS_factors, replace;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Any help with this would be much appreciated.
I obtained this piece of Stata code from the authors of ‘What does the yield curve tell us about exchange rate predictability’.
I'm fine with most of the code, the remaining bit -which is a standalone part of the paper- is where I got stuck, and I was hoping you could give me a hand with it.
There have been several requests on the forum about the Nelson-Siegel model -the code below is precisely that0, so I think this will be very useful to other users (and pacify them).
I’ve include in squared brackets info on what the adjacent Stata commands do, but I presume you know that programming language already.
Lastly, this program was written for monthly data- how would the NS1 and NS2 have to be amended to handle weekly data (presumably the parameter 0.0609, which is from Diebold and Li 2006 would have to be re-estimated separately)?
Here it is:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
clear
#delimit;
(resets the character that marks the end of a command [assigns a semicolon to denote the end of a line]
set more 1;
[tells Stata not to wait for keyboard input before waiting to display the next screen of output -- you will not be able to read what Stata is doing as it scrolls by, but you can read the output in the log file.]
capture log close; [close a log if any is open and do nothing if no log is open]
set mem 400m;
set matsize 800; [sets the max amount of variables in the estimation]
use yields_data.dta, replace;
gen number=_n; [generates a number whose value is the sequential order of the observation in the dataset, starting from 1 from the Nst observation. _n is often used to generate unique codes for each observation]
reshape long y ycan yuk yjp, i(number) j(month);
[takes wide data files and reshapes them into long form]
gen level_ns=0;
gen slope_ns=0;
gen curvature_ns=0;
gen level_se_ns=0;
gen slope_se_ns=0;
gen curvature_se_ns=0;
gen r2_ns = 0;
gen diff=y-yuk;
gen ns1 =1;
gen ns2 = (1-exp(-0.0609*month))/(0.0609*month);
gen ns3 = (1-exp(-0.0609*month))/(0.0609*month) -exp(-0.0609*month);
foreach x of numlist 1/240 {;
regress diff ns1 ns2 ns3 if number==`x', robust noconstant ;
replace level_ns = _b[ns1] if number==`x';
replace slope_ns = _b[ns2] if number==`x';
replace curvature_ns = _b[ns3] if number==`x';
replace level_se_ns = _se[ns1] if number==`x';
replace slope_se_ns = _se[ns2] if number==`x';
replace curvature_se_ns = _se[ns3] if number==`x';
replace r2_ns = e(r2_a) if number==`x';
};
drop ns1 ns2 ns3;
reshape wide diff y ycan yuk yjp , i(number) j(month);
outsheet using NS_factors, replace;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Any help with this would be much appreciated.