Announcement

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

  • How to create variable that show every mean since the first year by country

    Hello everybody, I have the following dataset
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Year str36 Countryoforigin float REC_over_TOTAL
    2000 "Afghanistan"                          .08547009
    2000 "Albania"                              .28148147
    2000 "Algeria"                                      0
    2000 "Angola"                                .6595744
    2000 "Egypt"                                 .3333333
    2000 "Armenia"                                     .5
    2000 "Azerbaijan"                                  .5
    2000 "Burundi"                                      1
    2000 "Benin"                                        1
    2000 "Bangladesh"                            .0909091
    2000 "Belarus"                                      .
    2000 "Bosnia and Herzegovina"                .3703704
    2000 "China"                                        0
    2000 "Cameroon"                                     1
    2000 "Congo"                                  .766129
    2000 "Dem. Rep. of the Congo"               .59302324
    2000 "Colombia"                              .4615385
    2000 "Cuba"                                  .3333333
    2000 "Ecuador"                                      .
    2000 "Eritrea"                               .7222222
    2000 "Ethiopia"                              .6486486
    2000 "Gambia"                                       .
    2000 "Palestinian"                           .2857143
    2000 "Georgia"                              .51724136
    2000 "Ghana"                                 .3333333
    2000 "Guinea-Bissau"                                0
    2000 "Guinea"                                       0
    2000 "Croatia"                                     .5
    2000 "Cote d'Ivoire"                                1
    2000 "India"                                        .
    2000 "Iran (Islamic Rep. of)"                .4243903
    2000 "Iraq"                                 .02648305
    2000 "Kazakhstan"                                   1
    2000 "Liberia"                                     .5
    2000 "Libya"                                        0
    2000 "Lebanon"                                      0
    2000 "Sri Lanka"                            .17857143
    2000 "Mauritania"                                   .
    2000 "North Macedonia"                              0
    2000 "Rep. of Moldova"                              0
    2000 "Nigeria"                                      0
    2000 "Pakistan"                              .3030303
    2000 "Peru"                                         1
    2000 "Romania"                                      0
    2000 "Russian Federation"                          .5
    2000 "Rwanda"                                .4137931
    2000 "Senegal"                                      0
    2000 "Sierra Leone"                          .1891892
    2000 "Somalia"                              .11363637
    2000 "Serbia and Kosovo: S/RES/1244 (1999)"         .
    2000 "Viet Nam"                                     1
    2000 "Sudan"                                 .5535714
    2000 "Syrian Arab Rep."                     .10204082
    2000 "Togo"                                         1
    2000 "Tunisia"                              .58064514
    2000 "Turkey"                               .06204583
    2000 "Unknown "                             .04093092
    2000 "Ukraine"                                     .5
    2000 "Uzbekistan"                                   0
    2001 "Afghanistan"                           .1784141
    2001 "Albania"                              .14666666
    2001 "Algeria"                               .1851852
    2001 "Angola"                                .6153846
    2001 "Egypt"                                        .
    2001 "Armenia"                                      .
    2001 "Azerbaijan"                                   .
    2001 "Burundi"                                      .
    2001 "Bangladesh"                           .18032786
    2001 "Belarus"                                      .
    2001 "Brazil"                                       .
    2001 "Bosnia and Herzegovina"                .3333333
    2001 "Bulgaria"                                     .
    2001 "China"                                        .
    2001 "Cameroon"                              .8148148
    2001 "Congo"                                 .8181818
    2001 "Dem. Rep. of the Congo"                .7657658
    2001 "Colombia"                                   .45
    2001 "Cuba"                                         .
    2001 "Czechia"                                      .
    2001 "Ecuador"                                      .
    2001 "Eritrea"                               .4468085
    2001 "Ethiopia"                              .6842105
    2001 "Gambia"                                      .5
    2001 "Palestinian"                           .1851852
    2001 "Georgia"                                      .
    2001 "Ghana"                                        .
    2001 "Guinea"                                       .
    2001 "Cote d'Ivoire"                                .
    2001 "India"                                        .
    2001 "Iran (Islamic Rep. of)"                .4917127
    2001 "Iraq"                                 .04124836
    2001 "Jordan"                                       .
    2001 "Kazakhstan"                                   .
    2001 "Liberia"                                      .
    2001 "Lebanon"                              .52380955
    2001 "Sri Lanka"                            .08928572
    2001 "Mauritania"                                   .
    2001 "North Macedonia"                              0
    2001 "Rep. of Moldova"                      .13157895
    2001 "Morocco"                                      .
    end
    I need to create a new variable that rapresent the mean of REC_over_TOTAL for each country which change every year based on the new observation.
    After that I want to create a pie chart which gives me the top 5 countries with the highest mean of REC_over_TOTAL for 2020.
    Can someone help me?
    Many thanks for the attentio

  • #2
    Code:
    by Countryoforigin, sort: egen mean_REC_over_TOTAL = mean(REC_over_TOTAL)

    Comment


    • #3
      I am not entire sure what kind of mean was it. Here I show the code to total mean (mean of both years) and cumulative running mean (mean of this and all the previous years, it will get updated when new years are added):

      Code:
      gsort Countryoforigin Year
      
      * This?
      bysort Countryoforigin: egen wanted1 = mean(REC_over_TOTAL)
      
      * Or this?
      bysort Countryoforigin (Year): gen wanted2 = sum(REC_over_TOTAL) / sum(REC_over_TOTAL < .)
      
      list in 1/8
      Results

      Code:
           +-----------------------------------------------------+
           | Year   Countryof~n   REC_ov~L    wanted1    wanted2 |
           |-----------------------------------------------------|
        1. | 2000   Afghanistan   .0854701   .1319421   .0854701 |
        2. | 2001   Afghanistan   .1784141   .1319421   .1319421 |
        3. | 2000       Albania   .2814815   .2140741   .2814815 |
        4. | 2001       Albania   .1466667   .2140741   .2140741 |
        5. | 2000       Algeria          0   .0925926          0 |
           |-----------------------------------------------------|
        6. | 2001       Algeria   .1851852   .0925926   .0925926 |
        7. | 2000        Angola   .6595744   .6374795   .6595744 |
        8. | 2001        Angola   .6153846   .6374795   .6374795 |
           +-----------------------------------------------------+
      Also, please don't use pie chart for this purpose. By confining to only top 5 your pie chart will imply that their share is the global entirety, which can be grossly misleading. Either keep the pie chart idea but add the sum of the rest as the 6th section, or use bar chart.

      Comment


      • #4
        Thank you both, really helpful.
        I used the construnction you made to obtain wanted2.
        Thaks also for the advice regarding the graph, I will keep it mind.

        Comment


        • #5
          Hi, I wasn't able to find how to define the upper mentioned criteria (show only the top 5 higest value for the variable wanted2) for the chart, can you please help me?

          Comment


          • #6
            I agree with Ken Chui in deprecating pie charts. This may help otherwise.


            Code:
            egen rank2001 = rank(REC) if Year == 2001, field 
            
            graph dot (asis) REC if rank <= 5, over(Country, sort(1) descending) linetype(line) lines(lc(gs12) lw(vthin)) ysc(alt) ytitle(insert informative text)

            Comment

            Working...
            X