Announcement

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

  • How to create the average value "Europe" for my bar chart

    Hello there,

    I apologize in advance if I do not explain my request well. I am new here and have not worked with Stata for a long time.

    I'm using a Eurobarometer dataset and I'm supposed to show with bar charts what percentage of respondents in each country have trust in different institutions (as an example let's take trust in social networks) - see attachment.

    So far, so good.

    I should also include the value "Europe" in the chart. All the countries I am considering are in Europe.

    Unfortunately, this is exactly where I fail. Does anyone have an idea how I can do this?


    countries:

    tab country_id

    #Europe should be a part of this country list!


    country_id | Freq. Percent Cum.
    -----------------+-----------------------------------
    Belgium | 1,005 3.58 3.58
    Bulgaria | 1,051 3.75 7.33
    Denmark | 1,000 3.56 10.89
    Germany East | 520 1.85 12.75
    Germany West | 1,045 3.72 16.47
    Estonia | 1,009 3.60 20.07
    Finland | 1,016 3.62 23.69
    France | 1,072 3.82 27.51
    Greece | 1,008 3.59 31.10
    Great Britain | 1,025 3.65 34.76
    Ireland | 1,001 3.57 38.32
    Italy | 1,034 3.69 42.01
    Croatia | 1,094 3.90 45.91
    Latvia | 1,018 3.63 49.54
    Lithuania | 1,013 3.61 53.15
    Luxembourg | 507 1.81 54.96
    Malta | 503 1.79 56.75
    The Netherlands | 1,034 3.69 60.43
    Northern Ireland | 309 1.10 61.54
    Austria | 1,016 3.62 65.16
    Poland | 1,014 3.61 68.77
    Portugal | 1,076 3.84 72.61
    Romania | 1,062 3.79 76.39
    Sweden | 1,002 3.57 79.96
    Slovakia | 1,044 3.72 83.69
    Slovenia | 1,009 3.60 87.28
    Spain | 1,008 3.59 90.88
    Czech Republic | 1,021 3.64 94.51
    Hungary | 1,039 3.70 98.22
    Cyprus | 500 1.78 100.00
    -----------------+-----------------------------------
    Total | 28,055 100.00


    tab trust_social

    trust_socia |
    l | Freq. Percent Cum.
    ------------+-----------------------------------
    0 | 24,940 76.29 76.29
    1 | 7,753 23.71 100.00
    ------------+-----------------------------------
    Total | 32,693 100.00
    Attached Files
    Last edited by Nadine Englmaier; 08 Jan 2022, 07:55.

  • #2
    It remains unclear

    1. what graph command you used. I guess graph bar but graph hbar would be a better idea.

    2. whether you want to show the mean across people, in whatever country, or the mean across countries giving each country equal weight. The answers are unlikely to differ much, but they aren't guaranteed identical.

    This reproducible example shows some technique

    Code:
    clear
    input str42 country answer
    "Freedonia"   1
    "Freedonia"   1
    "Freedonia"   0
    "Sylvania"    1
    "Sylvania"    0
    end
    
    * mean across people
    su answer, meanonly
    local mean1 = r(mean)
    
    * mean across countries
    egen mean = mean(answer), by(country)
    egen tag = tag(country)
    su mean if tag
    local mean2 = r(mean)
    
    * dataset of results
    collapse answer, by(country)
    
    * add overall mean
    
    insobs 1
    
    * use `mean2' if that is what you prefer
    replace answer = `mean1' in L
    
    replace country = "anywhere" in L
    
    graph hbar (asis) answer, over(country) scheme(s1color)
    Great Britain, Northern Ireland, Germany West, Germany East: Those are puzzles.

    Comment


    • #3
      Thank you very much for your help! I will try to run my code with your suggestions
      Last edited by Nadine Englmaier; 09 Jan 2022, 11:55.

      Comment


      • #4
        I would like to show the mean across countries.

        I tried to implement your code, but unfortunately I still face problems with the command: collapse answer, by(country).

        That's my code. Maybe it helps to understand my issue:

        Code:
        generate country_id =.
         
        replace country_id = 1 if isocntry == "BE"
        replace country_id = 2 if isocntry == "BG"
        replace country_id = 3 if isocntry == "DK"
        replace country_id = 4 if isocntry == "DE-E"
        replace country_id = 5 if isocntry == "DE-W"
        replace country_id = 6 if isocntry == "EE"
        replace country_id = 7 if isocntry == "FI"
        replace country_id = 8 if isocntry == "FR"
        replace country_id = 9 if isocntry == "GR"
        replace country_id = 10 if isocntry == "GB-GBN"
        replace country_id = 11 if isocntry == "IE"
        replace country_id = 12 if isocntry == "IT"
        replace country_id = 13 if isocntry == "HR"
        replace country_id = 14 if isocntry == "LV"
        replace country_id = 15 if isocntry == "LT"
        replace country_id = 16 if isocntry == "LU"
        replace country_id = 17 if isocntry == "MT"
        replace country_id = 18 if isocntry == "NL"
        replace country_id = 19 if isocntry == "GB-NIR"
        replace country_id = 20 if isocntry == "AT"
        replace country_id = 21 if isocntry == "PL"
        replace country_id = 22 if isocntry == "PT"
        replace country_id = 23 if isocntry == "RO"
        replace country_id = 24 if isocntry == "SE"
        replace country_id = 25 if isocntry == "SK"
        replace country_id = 26 if isocntry == "SI"
        replace country_id = 27 if isocntry == "ES"
        replace country_id = 28 if isocntry == "CZ"
        replace country_id = 29 if isocntry == "HU"
        replace country_id = 30 if isocntry == "CY"
        
        tab qa8a_5
        tab qa8a_5, nolabel
        
        gen         trust_social = 0 if       qa8a_5 ==1 | qa8a_5 ==2 | qa8a_5 ==3
        replace        trust_social = 1 if       qa8a_5 ==1
        tab trust_social  
        
        * mean across countries
        egen Europe_social = mean(trust_social), by(country_id)
        egen tag = tag(country_id)
        su Europe_social if tag
        local Europe_social = r(Europe_social)
         
        * dataset of results
        collapse trust_social, by(country_id)
         
        *add overall mean
        insobs 1
         
        replace trust_social = Europe_social in L
        replace country_id = 31 in L
        label define mcountry_id 31 "Europe"
         
        * labeling
        label define mcountry_id 1 "Belgium" 2 "Bulgaria" 3 "Denmark" 4 "Germany East" 5 "Germany West" 6 "Estonia" 7 "Finland" 8 "France" 9 "Greece" 10 "Great Britain" 11 "Ireland" 12 "Italy" 13 "Croatia" 14 "Latvia" 15 "Lithuania" 16 "Luxembourg" 17 "Malta" 18 "The Netherlands" 19 "Northern Ireland" 20 "Austria" 21 "Poland" 22 "Portugal" 23 "Romania" 24 "Sweden" 25 "Slovakia" 26 "Slovenia" 27 "Spain" 28 "Czech Republic" 29 "Hungary" 30 "Cyprus" 31 "Europe"
         
        label value country_id mcountry_id
        
        * Graph
        graph bar trust_social, over(country_id, label(angle(45)) sort(1) descending) scale(*.6) ytitle("% of respondents") bar(1, color(gray)) ylabel(0 "0" 0.2 "20" 0.4 "40" 0.6 "60")

        Comment


        • #5
          Code:
          gen         trust_social = 0 if       qa8a_5 ==1 | qa8a_5 ==2 | qa8a_5 ==3  
          
          replace        trust_social = 1 if       qa8a_5 ==1
          That's legal but puzzling. Simpler to go

          Code:
          gen trust_social = 0 if qa8a_5 ==2 | qa8a_5 ==3
          
          replace trust_social = 1 if qa8a_5 ==1

          Code:
           local Europe_social = r(Europe_social)
          That's a bug in terms of what you want. It is legal code, but you would be putting a missing value into the local macro, as there is no such saved result. My variable was called mean, which was a choice. I picked up r(mean) which is required given what summarize leaves behind.

          Code:
          replace trust_social = Europe_social in L 
          That is another bug. After your collapse, the variable Europe_social is no longer in your dataset. As in my code you need to plug in the local macro containing the mean.

          There may be other bugs but I stop there.

          Comment

          Working...
          X