Announcement

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

  • Correlation between countries in Panel Data

    Hi everyone! I'm using the World Bank database to get data on real GDP (NY.GDP.MKTP.KD) and Total Consumption (NE.CON.TOTL.KD). I want to analyse business cycle synchronization among EMU countries and to do so I need to do the following:
    1. estimate the cyclical component of the GDP through a HP filter
    2. calculate the average correlation coefficient of each country’s cyclical component of real GDP with that of the other EMU countries (Italy with Germany/France/Spain/etc, France with Germany/Italy/Spain/etc)
    The result I should have is something like this from Furceri, Loungani and Pizzuto [2019]:
    Click image for larger version

Name:	bcs emu.png
Views:	1
Size:	25.6 KB
ID:	1752338


    The first point is super-easy, but I don't know how to do the second point. The only thing I managed to do is calculating the correlation among countries by using reshape wide. However, the professor told me that I should use a loop, so I'm sure this is not the correct way. ChatGPT has not been of much help so here I am, I would like to thanks in advance anyone who will try to help!

    This is my code until now, you need to install the wbopendata package to run it.
    Code:
    clear all
    
    wbopendata, language(en - English) country() topics() indicator(NE.CON.TOTL.KD; NY.GDP.MKTP.KD) clear long
    format ne_con_totl_kd ny_gdp_mktp_kd %20.10f
    set more off
    
    egen CountryID = group(countrycode)
    
    drop if missing(ne_con_totl_kd, ny_gdp_mktp_kd)
    drop if regionname == "Aggregates" 
    
    rename ny_gdp_mktp_kd GDP
    rename ne_con_totl_kd Consumption
    gen lnConsumption = ln(Consumption)
    gen lnGDP = ln(GDP)
    
    xtset CountryID year
    gen EMU = 0
    replace EMU = 1 if inlist(CountryID, 15, 18, 100, 54, 72, 76, 78, 56, 90, 112, 117, 146, 144, 145, 160, 177, 195, 222, 223, 71)
    
    tsfilter hp cyclical_GDP=lnGDP, trend(trend_GDP) smooth(6.25)
    keep if EMU == 1
    The "wrong way" of doing it is:
    Code:
    reshape wide cyclical_GDP countrycode countryname region regionname adminregion adminregionname incomelevel incomelevelname lendingtype lendingtypename Consumption GDP lnConsumption lnGDP EMU EU lnGDP_EMU lnGDP_nonEMU lnGDP_EU lnGDP_nonEU lnGDP_COVID19 lnGDP_nonCOVID19 time_trend GDP_int lnGDP_EMU_t lnGDP_nonEMU_t trend_GDP, i(year) j(CountryID)
    correlate cyclical_GDP*
    How can I do it through a loop? Thanks!


  • #2
    Maybe.

    Code:
    levelsof countrycode, local(levels)
    foreach c of local levels {
        qui capture drop base
        qui egen base = mean(cond(countrycode=="`c'", cyclical_GDP,.)) , by(year)
        qui correl base cyclical_GDP
        di "`c'" _col(10) %5.3f r(rho)    
    }

    Comment

    Working...
    X