Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with xtcdf (cross sectional dependency test)

    Dear Friends

    My name is Luqman and I am postgraduate student at Royal Holloway

    I need help with XTCDF, pesaran cross section dependency test. I download and instal file from here http://fmwww.bc.edu/repec/bocode/x/xtcdf.ado

    The log in stata run like this
    . * Pesaran (2004/2015) CD-test for cross sectional dependence

    .
    . *! Version 1.0.0 28jul2017

    .
    . * Contact [email protected] for bug reports/inquiries.

    .
    .
    .
    . * Changelog

    .
    . ** 28jul2017: Submitted to SSC

    .
    .
    .
    . cap mata mata drop flexCorr()

    .
    . cap mata mata drop prepData()

    .
    . cap mata mata drop corrMatrix()

    .
    . cap mata mata drop reshapeWideMata()

    .
    . cap mata mata drop calcCD()

    .
    .
    .
    . cap program drop xtcdf

    .
    . program define xtcdf, rclass
    1.
    . version 12
    2.
    . preserve
    3.
    .
    .
    . * Loop over variables
    .
    . syntax varlist
    4.
    . tempname cd meanObs meanCorr meanAbsCorr pvalue notEnoughJointObs
    5.
    .
    .
    . * Display some stuff
    .
    . qui xtset
    6.
    . di as result _newline "xtcd test on variables `varlist'"
    7.
    . di as text "Panelvar: " r(panelvar)
    8.
    . di as text "Timevar: " r(timevar)
    9.
    . di as text "{hline 16}{c TT}{hline 38}{c TT}{hline 22}{c TRC}"
    10.
    . di as text _col(2) " Variable" _col(16) " {c |}" _col(20) "CD-test" _col(30) "p-value" _col(40) "average joint T" " {c |}" _col(58) "mean ρ" _col(67) "mean abs(ρ)" _col(73
    > ) " {c |}"
    11.
    . di as text "{hline 16}{c +}{hline 38}{c +}{hline 22}{c RT}"
    12.
    .
    .
    . * Perform xtcd test
    .
    . local j = 1
    13.
    . foreach var of local varlist {
    14.
    . ** Reshape data `var'
    .
    . reshapeWide `var'
    15.
    .
    .
    . ** Calculate statistics
    .
    . mata: calcCD(theData)
    16.
    . scalar `cd' = round(cd, 0.001)
    17.
    . scalar `meanObs' = round(meanObs, 0.01)
    18.
    . scalar `meanCorr' = round(meanCorr, 0.01)
    19.
    . scalar `meanAbsCorr' = round(meanAbsCorr, 0.01)
    20.
    . scalar `pvalue' = round(pvalue, 0.001)
    21.
    . scalar `notEnoughJointObs' = notEnoughJointObs
    22.
    .
    .
    . ** Report statistics
    .
    . if `notEnoughJointObs' == 1 local errorMessage = `"" `notEnoughJointObs' " combination of panel units ignored (insufficient joint observations)."'
    23.
    . else if `notEnoughJointObs' > 1 local errorMessage = `"" `notEnoughJointObs' " combinations of panel units ignored (insufficient joint observations)."'
    24.
    . else local errorMessage = ""
    25.
    .
    .
    . di _col(2) %~14s = abbrev("`var'",14) _col(16) " {c +}" _col(20) `cd' _col(30) _skip(1) %4.3f = `pvalue' _col(40) %10.2f = `meanObs' _col(55) " {c +}" _skip(2) %3.2f = `meanCo
    > rr' _col(67) _skip(3) %3.2f = `meanAbsCorr' _col(78) " {c RT}" _skip(1) as error "`errorMessage'"
    26.
    .
    .
    . ** Prep for return
    .
    . mat cds = (nullmat(cds), cd)
    27.
    . mat ps = (nullmat(ps), pvalue)
    28.
    .
    .
    . ** Return as scalar (more useful in some cases)
    .
    . return scalar cd`j' = cd
    29.
    . return scalar pvalue`j' = pvalue
    30.
    . local j = `j' + 1
    31.
    . }
    32.
    .
    .
    . * Display more stuff
    .
    . di as text "{hline 16}{c BT}{hline 38}{c BT}{hline 22}{c BRC}"
    33.
    . di _col(2) "Notes: Under the null hypothesis of cross-section independence, CD ~ N(0,1)"
    34.
    . di _col(9) "P-values close to zero indicate data are correlated across panel groups."
    35.
    .
    .
    . * Return stuff
    .
    . return matrix CD = cds
    36.
    . return matrix p = ps
    37.
    .
    .
    . restore
    38.
    . end

    .
    .
    .
    .
    .
    . cap program drop reshapeWide

    .
    . program define reshapeWide, rclass
    1.
    . * Obtain variables to reshape
    .
    . syntax varlist
    2.
    . tsfill, full
    3.
    .
    .
    . * Obtain time and panel variables
    .
    . qui xtset
    4.
    . local panelvar = r(panelvar)
    5.
    . local timevar = r(timevar)
    6.
    .
    .
    . * Obtain number of time units
    .
    . qui duplicates report `panelvar'
    7.
    . local timelength = r(unique_value)
    8.
    .
    .
    . * Load data in mata and reshape the mata matrix (tsfill, full is crucial)
    .
    . mata: theData = reshapeWideMata("`varlist'", `timelength')
    9.
    . end

    .
    .
    .
    .
    .
    . mata:
    ------------------------------------------------- mata (type end to exit) --------------------------------------------------------------------------------------------------------
    :
    : void calcCD(real matrix theData) {
    >
    > // 1. Define objects
    expression invalid
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    r(3000);

    .
    . real scalar T, N, counter, i, j; real matrix subData, demeaned; real colvector corrVector, tVector; real rowvector s
    command real is unrecognized
    r(199);

    .
    . real scalar CD, corr_nr, meanObs, meanCorr, meanAbsCorr, pvalue
    command real is unrecognized
    r(199);

    .
    .
    .
    . // 2. Count number of groups
    / is not a valid command name
    r(199);

    .
    . N = cols(theData)
    command N is unrecognized
    r(199);

    .
    .
    .
    . // 3. Calculate correlations per pair
    / is not a valid command name
    r(199);

    .
    . corrVector = J(N*(N-1)/2, 1, .)
    command corrVector is unrecognized
    r(199);

    .
    . tVector = J(N*(N-1)/2, 1, .)
    command tVector is unrecognized
    r(199);

    .
    . counter = 1
    command counter is unrecognized
    r(199);

    .
    . for(i=1; i<=N; i++) {
    invalid syntax
    r(198);

    .
    . for(j=i+1; j<=N; j++) {
    invalid syntax
    r(198);

    .
    . // 3a. Select subdata
    / is not a valid command name
    r(199);

    .
    . subData = select(theData[., (i, j)], theData[.,i]+theData[.,j] :!= .)
    command subData is unrecognized
    r(199);

    .
    .
    .
    . // 3b. Count number of joint observations
    / is not a valid command name
    r(199);

    .
    . T = rows(subData)
    command T is unrecognized
    r(199);

    .
    . if (T < 3) {
    T not found
    r(111);

    .
    . corrVector[counter, 1] = 0
    command corrVector is unrecognized
    r(199);

    .
    . tVector[counter, 1] = 0
    command tVector is unrecognized
    r(199);

    .
    . counter++
    command counter is unrecognized
    r(199);

    .
    . continue
    statement out of context
    r(119);

    .
    . }
    } is not a valid command name
    r(199);

    .
    .
    .
    . // 3c. Calculate correlation
    / is not a valid command name
    r(199);

    .
    . demeaned = subData - J(T, 1, 1) * colsum(subData)/T
    command demeaned is unrecognized
    r(199);

    .
    . s = sqrt( 1/(T-1) * colsum(demeaned:^2) )
    command s is unrecognized
    r(199);

    .
    . corrVector[counter, 1] = 1/(T-1) * colsum(demeaned[.,1]/s[1,1] :* demeaned[.,2]/s[1,2])
    command corrVector is unrecognized
    r(199);

    .
    . tVector[counter, 1] = T
    command tVector is unrecognized
    r(199);

    .
    . counter++
    command counter is unrecognized
    r(199);

    .
    . }
    } is not a valid command name
    r(199);

    .
    . }
    } is not a valid command name
    r(199);

    .
    .
    .
    . // 4. Calculate CD statistics
    / is not a valid command name
    r(199);

    .
    . notEnoughJointObs = sum(tVector:==0)
    command notEnoughJointObs is unrecognized
    r(199);

    .
    . corr_nr = N*(N-1)/2 - notEnoughJointObs
    command corr_nr is unrecognized
    r(199);

    .
    . CD = sqrt(1/corr_nr)* (sqrt(tVector)' * corrVector)
    command CD is unrecognized
    r(199);

    .
    . pvalue = (1 - normal(abs(CD))) * 2
    command pvalue is unrecognized
    r(199);

    .
    .
    .
    . meanObs = mean(select(tVector, tVector:!=0))
    command meanObs is unrecognized
    r(199);

    .
    . meanCorr = mean(corrVector)
    command meanCorr is unrecognized
    r(199);

    .
    . meanAbsCorr = mean(abs(corrVector))
    command meanAbsCorr is unrecognized
    r(199);

    .
    . st_numscalar("cd", CD)
    command st_numscalar is unrecognized
    r(199);

    .
    . st_numscalar("meanObs", meanObs)
    command st_numscalar is unrecognized
    r(199);

    .
    . st_numscalar("meanCorr", meanCorr)
    command st_numscalar is unrecognized
    r(199);

    .
    . st_numscalar("meanAbsCorr", meanAbsCorr)
    command st_numscalar is unrecognized
    r(199);

    .
    . st_numscalar("pvalue", pvalue)
    command st_numscalar is unrecognized
    r(199);

    .
    . st_numscalar("notEnoughJointObs", notEnoughJointObs)
    command st_numscalar is unrecognized
    r(199);

    .
    . }
    } is not a valid command name
    r(199);

    .
    .
    .
    .
    .
    . // reshapeWideMata reshapes the mata matrix to a wide format (columns are panel units, rows are time periods)
    / is not a valid command name
    r(199);

    .
    . real matrix reshapeWideMata(string scalar varname, real scalar timelength) {
    command real is unrecognized
    r(199);

    .
    . real matrix wideData
    command real is unrecognized
    r(199);

    .
    .
    .
    . wideData = st_data(wideData=., varname)
    command wideData is unrecognized
    r(199);

    .
    . wideData = rowshape(wideData, timelength)'
    command wideData is unrecognized
    r(199);

    .
    . return(wideData)
    invalid syntax
    r(198);

    .
    . }
    } is not a valid command name
    r(199);

    .
    . end
    command end is unrecognized
    r(199);

    .


    As you can see there seems to be error at the bottom. Once i run the test, i find this on the log

    xtcd test on variables assasse
    Panelvar: id
    Timevar: time
    ------------------------------------------------------------------------------+
    Variable | CD-test p-value average joint T | mean ρ mean abs(ρ) |
    ----------------+--------------------------------------+----------------------|
    <istmt>: 3499 reshapeWideMata() not found




    Can anyone help with this? Is this code error? If it is, then how can i fix it?

    I am desperately in need of help and i will appreciate any assistance on this.

    Luqman







Working...
X