Dear All,
I run the following code with the aim of producing a table, which summarizes the results of my analysis (the code needs some refinments, but at the moment I am more interested in the output):
Apparently, the command pairs should produce a table in the excel file pairs.xlsx. Indeed, this happens, but the table reports only information about stocks with no causality. I cannot understand why. The things becomes more puzzling, if you consider that it run smoothly until last year. Is there any issue with the code (maybe due to the fact that I use now Stata 18)? Can you get a complete table?
Thanks in advance for your help.
Dario
I run the following code with the aim of producing a table, which summarizes the results of my analysis (the code needs some refinments, but at the moment I am more interested in the output):
Code:
capture ssc install getsymbols getsymbols ADBE AMZN AAPL EBAY GE JPM MSFT ORCL WMT DIS, fm(1) fd(1) fy(2012) lm(5) ld(31) ly(2017) frequency(d) price(adjclose) clear yahoo capture program drop pairs program define pairs, rclass version 15 syntax varlist(min=2 max=100000 numeric) [if] [, SIg(numlist min=1 max=1)] putexcel set pairs.xlsx, sheet(Sheet1) replace putexcel (A1:D1)=("Pairs"), border(bottom, double) merge hcenter vcenter bold putexcel A2=("Stock 1"), hcenter border(bottom) italic putexcel B2=("Causality"), hcenter border(bottom) italic putexcel C2=("Stock 2"), hcenter border(bottom) italic putexcel D2=("Result"), hcenter border(bottom) italic local result "Pair" local noresult "Not a pair" local rcausality "-->" local lcausality "<--" local dcausality "<-->" local nocausality "No causality" // Construct the list and count the items. local k: word count `varlist' // Loop over distinct pairs of variables: // 1,2; 1,3, ... 2,3, ...; k-1, k local km1 = `k' - 1 // k minus 1 local m 2 //counter for row numbers forval i = 1/`km1' { disp "i: `i'" local ip1 = `i' + 1 // i plus 1 forval j = `ip1'/`k' { local ++m disp "j: `j'" local v1: word `i' of `varlist' local v2: word `j' of `varlist' di as result _newline "working with `v1' and `v2'" reg L(0/2).`v1' L(1/2).`v2' `if' testparm L(1/2).`v2' local p1=r(p) reg L(0/2).`v2' L(1/2).`v1' `if' testparm L(1/2).`v1' local p2=r(p) if `p1'<0.10 & `p2'>=0.10 { reg `v1' `v2' tempvar res_`v1'_`v2' predict `res_`v1'_`v2'' `if', res dfuller `res_`v1'_`v2'' `if', nocon local dfstat=r(Zt) local cv1=r(cv1) local cv5=r(cv5) local cv10=r(cv10) if "`sig'"=="" { if `dfstat'<`cv10' { di as result _newline "`v1' and `v2' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`lcausality'"), hcenter } else if `dfstat'>=`cv10' { di as result _newline "`v1' and `v2' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`lcausality'"), hcenter } } else if "`sig'"=="5" { if `dfstat'<`cv5' { di as result _newline "`v1' and `v2' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`lcausality'"), hcenter } else if `dfstat'>=`cv5' { di as result _newline "`v1' and `v2' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`lcausality'"), hcenter } } else if "`sig'"=="1" { if `dfstat'<`cv1' { di as result _newline "`v1' and `v2' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`lcausality'"), hcenter } else if `dfstat'>=`cv1' { di as result _newline "`v1' and `v2' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`lcausality'"), hcenter } } } if `p1'>=0.10 & `p2'<0.10 { reg `v2' `v1' tempvar res_`v2'_`v1' predict `res_`v2'_`v1'' `if', res dfuller `res_`v2'_`v1'' `if', nocon local dfstat=r(Zt) local cv1=r(cv1) local cv5=r(cv5) local cv10=r(cv10) if "`sig'"=="" { if `dfstat'<`cv10' { di as result _newline "`v2' and `v1' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`rcausality'"), hcenter } else if `dfstat'>=`cv10' { di as result _newline "`v2' and `v1' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`rcausality'"), hcenter } } else if "`sig'"=="5" { if `dfstat'<`cv5' { di as result _newline "`v2' and `v1' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`rcausality'"), hcenter } else if `dfstat'>=`cv5' { di as result _newline "`v2' and `v1' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`rcausality'"), hcenter } } else if "`sig'"=="1" { if `dfstat'<`cv1' { di as result _newline "`v2' and `v1' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`rcausality'"), hcenter } else if `dfstat'>=`cv1' { di as result _newline "`v2' and `v1' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`rcausality'"), hcenter } } } if `p1'<0.10 & `p2'<0.10 { reg `v1' `v2' tempvar res_`v1'_`v2' predict `res_`v1'_`v2'' `if', res dfuller `res_`v1'_`v2'' `if', nocon local dfstat=r(Zt) local cv1=r(cv1) local cv5=r(cv5) local cv10=r(cv10) if "`sig'"=="" { if `dfstat'<`cv10' { di as result _newline "`v1' and `v2' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`dcausality'"), hcenter } else if `dfstat'>=`cv10' { di as result _newline "`v1' and `v2' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`dcausality'"), hcenter } } else if "`sig'"=="5" { if `dfstat'<`cv5' { di as result _newline "`v1' and `v2' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`dcausality'"), hcenter } else if `dfstat'>=`cv5' { di as result _newline "`v1' and `v2' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`dcausality'"), hcenter } } else if "`sig'"=="1" { if `dfstat'<`cv1' { di as result _newline "`v1' and `v2' are cointegrated and can be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`result'"), hcenter putexcel B`m'=("`dcausality'"), hcenter } else if `dfstat'>=`cv1' { di as result _newline "`v1' and `v2' are not cointegrated and cannot be treated as a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`dcausality'"), hcenter } } } else if `p1'>=0.10 & `p2'>=0.10 { di as result _newline "`v1' and `v2' are not a pair" putexcel A`m'=("`v1'"), hcenter putexcel C`m'=("`v2'"), hcenter putexcel D`m'=("`noresult'"), hcenter putexcel B`m'=("`nocausality'"), hcenter } local f=((`k'*`km1')/2)+2 putexcel A`f', border(bottom, double) putexcel B`f', border(bottom, double) putexcel C`f', border(bottom, double) putexcel D`f', border(bottom, double) } } end pairs ADBE-DIS, sig(5)
Thanks in advance for your help.
Dario