*! xcorplot NJC/AT 1.2.0 15 September 1998
* xcorplot NJC/AT 1.1.0 4 September 1998
* xcorplot NJC 1.0.0 2 September 1998
* xcorr -- Cross correlations with p-values.     STB-13: sts3
* version 1.0.0     Sean Becketti     April 11, 1993
program define xcorplot
        version 5.0
        local varlist "req ex min(2) max(2)"
        local if "opt"
        local in "opt"
        #delimit ;
        local options "Kendall Lags(int 0) Pearson Spearman SPike CI
        noPrint
        Symbol(str) TItle(str) Connect(str) Gap(int 6) XLAbel(str)
        YLAbel(str) RLAbel(str) YLIne(str) XLine(str) SOrt noBorder *" ;
        #delimit cr
        parse "`*'"

        if `lags' <= 0 {
                qui period
                if $S_1 > 1 { local lags = $S_1 }
                else { local lags = 5 }
        }

        local corr "pearson"
        if "`spearma'" != "" { local corr "spear" }
        if "`kendall'" != "" { local corr "ktau" }

        preserve

        tempvar l xc zero
        qui gen `l' = .
        qui gen `xc' = .
        local L 1

        capture {
                global S_1
                global S_2
                global S_3
                parse "`varlist'", parse(" ")
                count if `1' != . & `2' != .
                if _result(1) < 2 + `lags' {
                        noi di in r "Not enough observations"
                        error 99
                }

                noi if "`print'" != "noprint" {
                        di _n in g _col(12) "lags of" _col(31) "lags of"
                        di in y _col(12) "`1'" _col(31) "`2'"
                        di in g "Lag" _col(10) "r" _col(15) "p-value" /*
                         */ _col(29) "r" _col(34) "p-value"
                        di in g "---" _col(8) "-----" _col(15) "-------" /*
                         */ _col(27) "-----"  _col(34) "-------"
                }

                `corr' `1' `2' `if' `in'
                qui replace `xc' = $S_4 in `L'
                qui replace `l' = 0 in `L'
                local L = `L' + 1
                local r0 = $S_4
                local p0 = $S_5
                noi if "`print'" != "noprint" {
                        di in y " 0" _col(8) %5.2f = `r0' /*
                         */ _col(16) %5.2f = `p0'
                }

                lag `lags' `1'
                lag `lags' `2'
                local lagx "`1'"
                local lagy "`2'"

                local i = 0
                while `i' < `lags' {
                        local i = `i' + 1
                        _addl `lagx'
                        local lagx "$S_1"
                        _addl `lagy'
                        local lagy "$S_1"
                        local j = 2 * `i' - 1
                        `corr' `2' `lagx' `if' `in'
                        qui replace `xc' = $S_4 in `L'
                        qui replace `l' = `i' in `L'
                        local L = `L' + 1
                        local r`j' = $S_4
                        local p`j' = $S_5
                        local k = `j' + 1
                        `corr' `1' `lagy' `if' `in'
                        qui replace `xc' = $S_4 in `L'
                        qui replace `l' = - `i' in `L'
                        local L = `L' + 1
                        local r`k' = $S_4
                        local p`k' = $S_5
                        noi if "`print'" != "noprint" {
                                di in y %2.0f =`i' _col(8) %5.2f =`r`j'' /*
                                 */ _col(16) %5.2f =`p`j'' /*
                                 */ _col(27) %5.2f =`r`k'' /*
                                 */ _col(35) %5.2f =`p`k''
                        }
                }
        }
        local rc = _rc
        error `rc'

        global S_1 "`r0'"
        global S_2 "`p0'"
        local i = 0
        while `i' < `lags' {
                local i= `i' + 1
                local j = 4 * (`i' - 1) + 3
                local k = 2 * (`i' - 1) + 1
                global S_`j' "`r`k''"
                local j = `j' + 1
                global S_`j' "`p`k''"
                local k = `k' + 1
                local j = `j' + 1
                global S_`j' "`r`k''"
                local j = `j' + 1
                global S_`j' "`p`k''"
        }

        if "`title'" != "" {
                if "`title'" == "." { local title }
                else local title "title(`title')"
        }
        else local title "title(`1' and `2')"

        if "`spike'" == "spike" {
                local connect "||"
                local symbol "ii"
        }

        if "`connect'" == "" { local connect "l." }
        if "`symbol'" == "" { local symbol "oi" }
        if "`yline'" == "" { local yline "0" }
        if "`xline'" == "" & "`spike'" != "spike" { local xline "0" }
        if "`xline'" != "" { local xline "xli(`xline')" }

        if "`ci'" == "ci" {
                qui su `1' `if' `in', meanonly
                local n1 = _result(1)
                qui su `2' `if' `in', meanonly
                local n2 = _result(1)
                local n = min(`n1',`n2')
                local ul = 2 / sqrt(`n')
                local ll = -2 / sqrt(`n')
                local yline "`yline',`ll',`ul'"
        }

        if "`xlabel'" == "" { local xla "xla" }
        else local xla "xla(`xlabel')"
        if "`ylabel'" == "" { local yla "yla" }
        else local yla "yla(`ylabel')"
        if "`rlabel'" == "" { local rla "rla" }
        else local rla "rla(`rlabel')"
        if "`sort'" == "" { local sort "sort" }
        if "`border'" == "" { local border "border" }

        qui gen `zero' = 0 if `xc' != .
        label var `xc' "Cross-correlation"
        label var `l' "Lag"

        gr `xc' `zero' `l', yline(`yline') `xline' `border' /*
                */ c(`connect') s(`symbol') gap(`gap') `sort' /*
                */ `xla' `yla' `rla' `title' `options'

end