Announcement

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

  • Plot pairwise correlation results sorted by country

    Dear Statalist-Users,

    I am a new statalist user, I have searched for similar posts but was not able to find a solution.
    I have panel data and as part of my analysis, I am running pairwise correlation command to get the correlation coefficients of two variables as cyclical components of government spending (govex_c) and cyclical component of real gdp (gdp_c) for each country, together with statistical significance of each one. I am interested in a comparison where positive correlation indicates pro-cyclical fiscal policy, negative indicates counter-cyclical fiscal policy.

    I used this command:

    bysort Country_: pwcorr govex_c gdp_c, sig

    Now I would like to plot the results using a bar graph. How do I do this using the generated results? Your assistance will be greatly appreciated.

    Below is my data

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int time long Country_ float(govex_c gdp_c)
    1990 2   .011350883    -.07908263
    1991 2    .00659353   -.033921205
    1992 2  .0017016873   .0010122942
    1993 2  -.003504089     .04036338
    1994 2 -.0092164455     .06060138
    1995 2  -.012442048      -.000992
    1996 2 -.0024984265     .02394262
    1997 2   .017003657     .07705756
    1998 2    .03724848     .09383197
    1999 2    .04968325     .04136691
    2000 2    .04085464     .01656813
    2001 2  .0032650575    -.04648871
    2002 2   -.06899465     -.1833036
    2003 2   -.07950079    -.12552877
    2004 2   -.08326602    -.07153885
    2005 2   -.02534282   -.023587003
    2006 2   -.02944014    .014402892
    2007 2   .001895623     .06079369
    2008 2   .004842874      .0622157
    2009 2   .013179167   -.034293994
    2010 2    .02069565     .03023103
    2011 2    .02193479     .06125034
    2012 2   .011415195    .029049907
    2013 2    .02717735    .036487628
    2014 2    .02490082  .00012626531
    2015 2   .066003814     .02106589
    2016 2    .04015058  -.0015581055
    2017 2    .05058253     .02832256
    2018 2    .02003054    .006874908
    2019 2   -.05478692   -.006259934
    2020 2   -.08141206     -.1029607
    2021 2   -.02010573   .0039504524
    1990 3    .03779359      .3676322
    1991 3     .0390835     .29583892
    1992 3    .03957776     -.1964875
    1993 3    .03848663     -.2499539
    1994 3    .03462838     -.1736925
    1995 3   .026434535    -.10045266
    1996 3  -.026049843    -.05452102
    1997 3   -.05025107      -.049931
    1998 3    -.0838124   -.023449617
    1999 3      -.09064    -.04924206
    2000 3   -.08310694    -.06260721
    2001 3    -.0908683    -.05171913
    2002 3   -.10073877    -.01563287
    2003 3   -.05097878     .02386316
    2004 3  -.017154658    .032182146
    2005 3    .08472703     .07422057
    2006 3    .14581904     .11658592
    2007 3    .13567527     .17219207
    2008 3    .06724464      .1761364
    2009 3   .010198107     -.0291728
    2010 3    .00715362    -.05358527
    2011 3   .027888507    -.04869892
    2012 3  -.011671047   -.016566077
    2013 3    .03435069   -.018839294
    2014 3 -.0019805485   -.016387098
    2015 3    .02142506   -.016621573
    2016 3   -.02487399     -.0456508
    2017 3  -.069022074    -.00396077
    2018 3    -.1245033    .016642733
    2019 3   -.03078692     .06052865
    2020 3    .02776669    -.04281265
    2021 3     .0781856     -.0158381
    1990 4   .006224633     .03937645
    1991 4  .0082617635    .003623082
    1992 4  .0038908985   -.024410194
    1993 4 -.0007423895   -.017866837
    1994 4   -.02006977   -.012972097
    1995 4  -.008582773    -.00999641
    1996 4  .0015030332   -.008208838
    1997 4  -.009283016   -.006551502
    1998 4  -.001462841    .001456143
    1999 4   .010499284    .012739487
    2000 4   .009185876     .01427589
    2001 4 -.0041343407  -.0016620937
    2002 4  -.002659272   .0020229856
    2003 4  -.006798137  -.0020384453
    2004 4   .005665166    .005417557
    2005 4    .00482012   .0035790405
    2006 4   .004601004  -.0012652038
    2007 4    .00514954    .004952235
    2008 4   .006357351    .010190278
    2009 4   .018019022 -.00008307908
    2010 4   .005655128   -.006171379
    2011 4   .007838704   -.009688137
    2012 4   .016354285   .0021428466
    2013 4  -.007733536   .0018904695
    2014 4  -.022510983    .002385404
    2015 4  -.033539053  -.0005474293
    2016 4   -.02204284     .00286818
    2017 4   -.01142113    .002590842
    2018 4  -.015592095     .00879699
    2019 4  -.007187673    .008605916
    2020 4    .01925954   -.013197917
    2021 4    .04047451   -.012254238
    1990 5   -.01439393  .00016931596
    1991 5  -.011082835    .010718115
    1992 5  -.007238703    .008144569
    1993 5   .004685029    -.01000242
    end
    format %ty time
    label values Country_ Country_
    label def Country_ 2 "Argentina", modify
    label def Country_ 3 "Armenia", modify
    label def Country_ 4 "Australia", modify
    label def Country_ 5 "Austria", modify

  • #2
    I'm sure there's a better way, but this might get you started:

    Code:
    forv i = 2/5 {
        pwcorr govex_c gdp_c if Country_ == `i', sig
        g corr`i' = r(C)[2,1] if Country_==`i'
    }
    graph bar corr2 corr3 corr4 corr5

    Comment


    • #3
      This may help. rangestat is community-contributed from SSC. However, the P-value that comes out of pwcorr is unlikely to be worth much, as the assumption of independent observations is way off for a correlation between time series. You have the ingredients here to repeat the calculation, but I don't recommend it.


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int time long Country_ float(govex_c gdp_c)
      1990 2   .011350883    -.07908263
      1991 2    .00659353   -.033921205
      1992 2  .0017016873   .0010122942
      1993 2  -.003504089     .04036338
      1994 2 -.0092164455     .06060138
      1995 2  -.012442048      -.000992
      1996 2 -.0024984265     .02394262
      1997 2   .017003657     .07705756
      1998 2    .03724848     .09383197
      1999 2    .04968325     .04136691
      2000 2    .04085464     .01656813
      2001 2  .0032650575    -.04648871
      2002 2   -.06899465     -.1833036
      2003 2   -.07950079    -.12552877
      2004 2   -.08326602    -.07153885
      2005 2   -.02534282   -.023587003
      2006 2   -.02944014    .014402892
      2007 2   .001895623     .06079369
      2008 2   .004842874      .0622157
      2009 2   .013179167   -.034293994
      2010 2    .02069565     .03023103
      2011 2    .02193479     .06125034
      2012 2   .011415195    .029049907
      2013 2    .02717735    .036487628
      2014 2    .02490082  .00012626531
      2015 2   .066003814     .02106589
      2016 2    .04015058  -.0015581055
      2017 2    .05058253     .02832256
      2018 2    .02003054    .006874908
      2019 2   -.05478692   -.006259934
      2020 2   -.08141206     -.1029607
      2021 2   -.02010573   .0039504524
      1990 3    .03779359      .3676322
      1991 3     .0390835     .29583892
      1992 3    .03957776     -.1964875
      1993 3    .03848663     -.2499539
      1994 3    .03462838     -.1736925
      1995 3   .026434535    -.10045266
      1996 3  -.026049843    -.05452102
      1997 3   -.05025107      -.049931
      1998 3    -.0838124   -.023449617
      1999 3      -.09064    -.04924206
      2000 3   -.08310694    -.06260721
      2001 3    -.0908683    -.05171913
      2002 3   -.10073877    -.01563287
      2003 3   -.05097878     .02386316
      2004 3  -.017154658    .032182146
      2005 3    .08472703     .07422057
      2006 3    .14581904     .11658592
      2007 3    .13567527     .17219207
      2008 3    .06724464      .1761364
      2009 3   .010198107     -.0291728
      2010 3    .00715362    -.05358527
      2011 3   .027888507    -.04869892
      2012 3  -.011671047   -.016566077
      2013 3    .03435069   -.018839294
      2014 3 -.0019805485   -.016387098
      2015 3    .02142506   -.016621573
      2016 3   -.02487399     -.0456508
      2017 3  -.069022074    -.00396077
      2018 3    -.1245033    .016642733
      2019 3   -.03078692     .06052865
      2020 3    .02776669    -.04281265
      2021 3     .0781856     -.0158381
      1990 4   .006224633     .03937645
      1991 4  .0082617635    .003623082
      1992 4  .0038908985   -.024410194
      1993 4 -.0007423895   -.017866837
      1994 4   -.02006977   -.012972097
      1995 4  -.008582773    -.00999641
      1996 4  .0015030332   -.008208838
      1997 4  -.009283016   -.006551502
      1998 4  -.001462841    .001456143
      1999 4   .010499284    .012739487
      2000 4   .009185876     .01427589
      2001 4 -.0041343407  -.0016620937
      2002 4  -.002659272   .0020229856
      2003 4  -.006798137  -.0020384453
      2004 4   .005665166    .005417557
      2005 4    .00482012   .0035790405
      2006 4   .004601004  -.0012652038
      2007 4    .00514954    .004952235
      2008 4   .006357351    .010190278
      2009 4   .018019022 -.00008307908
      2010 4   .005655128   -.006171379
      2011 4   .007838704   -.009688137
      2012 4   .016354285   .0021428466
      2013 4  -.007733536   .0018904695
      2014 4  -.022510983    .002385404
      2015 4  -.033539053  -.0005474293
      2016 4   -.02204284     .00286818
      2017 4   -.01142113    .002590842
      2018 4  -.015592095     .00879699
      2019 4  -.007187673    .008605916
      2020 4    .01925954   -.013197917
      2021 4    .04047451   -.012254238
      1990 5   -.01439393  .00016931596
      1991 5  -.011082835    .010718115
      1992 5  -.007238703    .008144569
      1993 5   .004685029    -.01000242
      end
      format %ty time
      label values Country_ Country_
      label def Country_ 2 "Argentina", modify
      label def Country_ 3 "Armenia", modify
      label def Country_ 4 "Australia", modify
      label def Country_ 5 "Austria", modify
      
      rangestat (corr) govex_c gdp_c, i(Country_ 0 0)
      
      tabdisp Country_ , c(corr*)
      
      ------------------------------------------------------
       Country_ |            corr_nobs                corr_x
      ----------+-------------------------------------------
      Argentina |                   32             .66532397
        Armenia |                   32             .27445914
      Australia |                   32            -.03357754
        Austria |                    4            -.70310013
      ------------------------------------------------------

      Comment


      • #4
        Thank you for the help, I really appreciate. I have tried the first approach suggested by George Ford and it seems to work, however, I need clarification on how I can extend the range to cover up to corr106?

        I am yet to try the second approach with rangestat command

        Comment


        • #5
          forv i = 1/106 { //or whatever numbers they have. you may want to create a new group variable (egen cid = group(Country_) to get them ordered properly.


          graph bar corr*

          That's going to be big chart that's hard to read.

          Comment


          • #6
            am still stuck on this issue George Ford the range is simple not picking up, it gives me missing observations after egen cid = group(Country_) . Nick Cox I tried your way using the rangestat command, but how do I construct a graph from the generated table?

            Comment


            • #7
              do you have missing data for Country_?

              Or is not in order?

              could use a local levels of Country_ to do the same

              Comment


              • #8
                What kind of graph do you want? For example, if you have about 200 countries, then no graph identifying each will be legible, although you could draw a histogram or a quantile plot. If you have around 20 countries, then graphs identifying countries are possible.

                Comment


                • #9
                  Click image for larger version

Name:	Screenshot 2023-08-22 101443.png
Views:	1
Size:	148.6 KB
ID:	1724578

                  My apologies for the late response.

                  I am looking for a graph similar to this one above to compare how many countries have graduated from procyclical fiscal policy to date or fallen back to it form countercyclcical. George Ford no I do not have the missing data. Am guessing this may not be possible then?

                  Comment


                  • #10
                    Here is some technique to get you started. I've an aversion to vertical text labels.

                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input int time long Country_ float(govex_c gdp_c)
                    1990 2   .011350883    -.07908263
                    1991 2    .00659353   -.033921205
                    1992 2  .0017016873   .0010122942
                    1993 2  -.003504089     .04036338
                    1994 2 -.0092164455     .06060138
                    1995 2  -.012442048      -.000992
                    1996 2 -.0024984265     .02394262
                    1997 2   .017003657     .07705756
                    1998 2    .03724848     .09383197
                    1999 2    .04968325     .04136691
                    2000 2    .04085464     .01656813
                    2001 2  .0032650575    -.04648871
                    2002 2   -.06899465     -.1833036
                    2003 2   -.07950079    -.12552877
                    2004 2   -.08326602    -.07153885
                    2005 2   -.02534282   -.023587003
                    2006 2   -.02944014    .014402892
                    2007 2   .001895623     .06079369
                    2008 2   .004842874      .0622157
                    2009 2   .013179167   -.034293994
                    2010 2    .02069565     .03023103
                    2011 2    .02193479     .06125034
                    2012 2   .011415195    .029049907
                    2013 2    .02717735    .036487628
                    2014 2    .02490082  .00012626531
                    2015 2   .066003814     .02106589
                    2016 2    .04015058  -.0015581055
                    2017 2    .05058253     .02832256
                    2018 2    .02003054    .006874908
                    2019 2   -.05478692   -.006259934
                    2020 2   -.08141206     -.1029607
                    2021 2   -.02010573   .0039504524
                    1990 3    .03779359      .3676322
                    1991 3     .0390835     .29583892
                    1992 3    .03957776     -.1964875
                    1993 3    .03848663     -.2499539
                    1994 3    .03462838     -.1736925
                    1995 3   .026434535    -.10045266
                    1996 3  -.026049843    -.05452102
                    1997 3   -.05025107      -.049931
                    1998 3    -.0838124   -.023449617
                    1999 3      -.09064    -.04924206
                    2000 3   -.08310694    -.06260721
                    2001 3    -.0908683    -.05171913
                    2002 3   -.10073877    -.01563287
                    2003 3   -.05097878     .02386316
                    2004 3  -.017154658    .032182146
                    2005 3    .08472703     .07422057
                    2006 3    .14581904     .11658592
                    2007 3    .13567527     .17219207
                    2008 3    .06724464      .1761364
                    2009 3   .010198107     -.0291728
                    2010 3    .00715362    -.05358527
                    2011 3   .027888507    -.04869892
                    2012 3  -.011671047   -.016566077
                    2013 3    .03435069   -.018839294
                    2014 3 -.0019805485   -.016387098
                    2015 3    .02142506   -.016621573
                    2016 3   -.02487399     -.0456508
                    2017 3  -.069022074    -.00396077
                    2018 3    -.1245033    .016642733
                    2019 3   -.03078692     .06052865
                    2020 3    .02776669    -.04281265
                    2021 3     .0781856     -.0158381
                    1990 4   .006224633     .03937645
                    1991 4  .0082617635    .003623082
                    1992 4  .0038908985   -.024410194
                    1993 4 -.0007423895   -.017866837
                    1994 4   -.02006977   -.012972097
                    1995 4  -.008582773    -.00999641
                    1996 4  .0015030332   -.008208838
                    1997 4  -.009283016   -.006551502
                    1998 4  -.001462841    .001456143
                    1999 4   .010499284    .012739487
                    2000 4   .009185876     .01427589
                    2001 4 -.0041343407  -.0016620937
                    2002 4  -.002659272   .0020229856
                    2003 4  -.006798137  -.0020384453
                    2004 4   .005665166    .005417557
                    2005 4    .00482012   .0035790405
                    2006 4   .004601004  -.0012652038
                    2007 4    .00514954    .004952235
                    2008 4   .006357351    .010190278
                    2009 4   .018019022 -.00008307908
                    2010 4   .005655128   -.006171379
                    2011 4   .007838704   -.009688137
                    2012 4   .016354285   .0021428466
                    2013 4  -.007733536   .0018904695
                    2014 4  -.022510983    .002385404
                    2015 4  -.033539053  -.0005474293
                    2016 4   -.02204284     .00286818
                    2017 4   -.01142113    .002590842
                    2018 4  -.015592095     .00879699
                    2019 4  -.007187673    .008605916
                    2020 4    .01925954   -.013197917
                    2021 4    .04047451   -.012254238
                    1990 5   -.01439393  .00016931596
                    1991 5  -.011082835    .010718115
                    1992 5  -.007238703    .008144569
                    1993 5   .004685029    -.01000242
                    end
                    format %ty time
                    label values Country_ Country_
                    label def Country_ 2 "Argentina", modify
                    label def Country_ 3 "Armenia", modify
                    label def Country_ 4 "Australia", modify
                    label def Country_ 5 "Austria", modify
                    
                    rangestat (corr) govex_c gdp_c, i(Country_ 0 0)
                    
                    egen tag = tag(Country_)
                    
                    egen rank = rank(-corr_x) if tag, unique
                    gen labelpos = cond(corr_x <= 0, 9, 3)
                    twoway bar corr_x rank if tag, horizontal barw(0.95) ///
                    || scatter rank corr_x if tag, ms(none) mlabel(Country_) mlabvpos(labelpos) ytitle("") legend(off) xsc(alt) xtitle(Correlation) yla(none)

                    Comment


                    • #11
                      Nick Cox it worked perfectly! thank you very much, I really appreciate this.

                      Comment

                      Working...
                      X