Announcement

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

  • How to make a graph with confident intervals?

    Dear all,

    I would like to make a graph of several concentration indices and their confident intervals against countries. I first use -conindex- command written by O'Donnell and his colleagues to produce values of CIs and confident intervals but I don't how to make a graph like an example below. I would be grateful if someone could help me.

    Click image for larger version

Name:	Screen Shot 2021-03-03 at 17.03.15.png
Views:	1
Size:	172.2 KB
ID:	1595947

    Data
    Code:
    clear
    input float(id country y) double inc
    5440 2  3  318841.3935546875
    5441 2  4    54898.208984375
    5442 2  3    54898.208984375
    5443 2  1 45175.817321777344
    5444 2  0 45175.817321777344
    5445 2  0   121893.837890625
    5446 2  5   121893.837890625
    5447 2  4  71314.00281524658
    5448 2  7  71314.00281524658
    5449 2  0  38337.68078613281
    5450 2  6 11104.256591796875
    5451 2  0 11104.256591796875
    5452 2  1 26280.073486328125
    5453 2  0 26280.073486328125
    5454 2  2  28671.24493408203
    5455 2  0   787.013916015625
    5456 2  3  19524.98480606079
    5457 2  0  32819.24786376953
    5458 2  5  22196.94139099121
    5459 2  0         9848.46875
    5460 2  0  34413.07556152344
    5461 2  0  34413.07556152344
    5462 2  0  23345.85076904297
    5463 2  4  23345.85076904297
    5464 2  0  25786.55157470703
    3050 4  3 13227.870353221893
    3051 4  5  6960.977005004883
    3052 4  6  6960.977005004883
    3053 4  7 3582.0121116638184
    3054 4  0  3096.338333129883
    3055 4  0  2604.194833278656
    3056 4  0 6452.1861572265625
    3057 4  2    2447.9052734375
    3058 4  0  266.6192626953125
    3059 4  3  266.6192626953125
    3060 4  0   7081.40771484375
    3061 4  1    6782.7939453125
    3062 4  0    6782.7939453125
    3063 4  0   7038.74853515625
    3064 4  6   7038.74853515625
    3065 4  0  15041.03581237793
    3066 4  7  15041.03581237793
    3067 4  0   5154.63916015625
    3068 4  3   44727.2001953125
    3069 4  0   7891.93017578125
    3070 4  2   7891.93017578125
    3071 4  3  8306.671447753906
    3072 4  4 10622.414672851563
    3073 4  1     7380.021484375
    3074 4  0     7380.021484375
     800 3  9              16800
     801 3  2              12060
     802 3 10 14492.137939453125
     803 3  2              13200
     804 3  0              13200
     805 3  3              41192
     806 3  0   22124.1123046875
     807 3  5              22450
     808 3  6              22450
     809 3  7  37675.12646484375
     810 3  0              34440
     811 3  0              34440
     812 3  1    104402.56640625
     813 3  0    104402.56640625
     814 3  4              28360
     815 3  0              28360
     816 3  0              10120
     817 3  2  35118.61358642578
     818 3  0  35118.61358642578
     819 3  0      20531.8828125
     820 3  9              12570
     821 3  0              16176
     822 3  0              16176
     823 3  1 38801.344957351685
     824 3  0 34039.176513671875
       1 1  1              23300
       2 1  0              23300
       3 1  8              13200
       4 1  0              23610
       5 1 10              23610
       6 1  8               6586
       7 1  8              30000
       8 1  0              15650
       9 1  1    21901.494140625
      10 1  1 25350.987548828125
      11 1  3              39350
      12 1  5              39350
      13 1  6              26330
      14 1  7              26330
      15 1  0              14800
      16 1  9  21299.12939453125
      17 1  0 18023.751762390137
      18 1  0    19643.787109375
      19 1 10    19643.787109375
      20 1  0                600
      21 1  0                600
      22 1  8       95854.078125
      23 1  2       95854.078125
      24 1  3              23400
      25 1  4              23400
    end
    label values country countryn
    label def countryn 1 "Austria", modify
    label def countryn 2 "Switzerland", modify
    label def countryn 3 "Belgium", modify
    label def countryn 4 "Czech Republic", modify
    Code to compute CIs and 95% CI
    Code:
    forval i = 1/4 {
            qui {
                conindex y if country==`i', rankvar(inc) truezero robust    
                    gen CI`i' = r(CI)
                    gen SE`i' = r(CIse)
                    gen df`i' = e(df_r)
                    gen lb`i' = CI`i' - invttail(df`i',0.025)*SE`i'
                    gen ub`i' = CI`i' + invttail(df`i',0.025)*SE`i'
            }
        }
    Thank you

  • #2
    The usual term is confidence interval, not confident interval.

    Given your results I would combine the 4 result variables for the index

    Code:
    gen CI = min(CI1, CI2, Ci3, Ci4)

    and similarly for ub and lb.

    Then you can plot

    Code:
    scatter CI country || rcap ub lb country, xla(1/4, valuelabel noticks)
    except it may be more readable to go

    Code:
    scatter country CI || rcap ub lb country, horizontal yla(1/4, ang(h) valuelabel noticks)

    Comment


    • #3
      .
      Last edited by Dung Le; 03 Mar 2021, 07:17.

      Comment


      • #4
        DearNick Cox,

        Thank you for your useful code, they produced nice graph but it seems slightly different from what I am looking for. Since CIs are just values, i.e. CI1= 0.1391489 so I don't know why we need to use its means. Furthermore, a new variable, CI, takes on a negative value while the original values of CI1-CI4 are either positive or negative. As a result, when I made a graph as suggested in #2, the graph does not depict the actual CI values in each country. Is there any other ways you might think of to make a similar graph shown in #1?

        This is how a graph looks like using code in #2.
        Click image for larger version

Name:	Screen Shot 2021-03-03 at 23.12.21.png
Views:	1
Size:	667.0 KB
ID:	1596005

        P/s: thanks for your correction regarding the confidence interval term.

        Comment


        • #5
          The graph looks wrong. That is my fault, because I misread your code in #1. Without revising the code in #1 (I would have advised differently, but no matter) better code would be

          Code:
          gen CI = .  
          gen ub = .  
          gen lb = .  
          forval j = 1/4 {      
                replace CI = CI`j' if country == `j'      
                replace ub = ub`j' if country == `j'      
                replace lb = lb`j' if country == `j'  
          }
          Then you can plot
          Code:
           scatter CI country || rcap ub lb country, xla(1/4, valuelabel noticks)
          except it may be more readable to go

          Code:
          scatter country CI || rcap ub lb country, horizontal yla(1/4, ang(h) valuelabel noticks)

          Comment


          • #6
            DearNick Cox,

            Sorry for my late response and thank you so much for your help. The code in #5 works perfectly. Many thanks for that.

            Relevant to this thread, do you have any ideas how to make a graph like this? In my data example, I would like to plot combination (stack) of values of CI of age and gender for each country (two countries) in one graph. I would be grateful if you can help me out.
            Click image for larger version

Name:	Screen Shot 2021-03-12 at 12.14.06.png
Views:	1
Size:	240.6 KB
ID:	1597396


            Data
            Code:
            clear
            input float(country age gender y wealth)
            0 73 1 4    1.395582
            0 81 0 5    1.220415
            0 62 1 4   1.1294894
            0 75 1 6   1.6639395
            0 74 1 6     .949276
            0 77 1 4   1.6236353
            0 61 1 6    .9107408
            0 61 1 0   1.2131945
            0 72 1 6   1.3741723
            0 64 1 1    3.169981
            0 75 0 3   1.0510343
            0 60 0 0    .8520967
            0 68 1 3    .4315759
            0 62 1 0   1.5155405
            0 65 1 0    .7244153
            0 65 1 2   1.1795194
            0 71 0 4   2.3465064
            0 61 0 0   .12656227
            0 77 0 1    .5998135
            0 81 0 2    1.048896
            0 81 0 3    .6379281
            0 66 0 4    .7880797
            0 82 0 4    3.020302
            0 62 1 3   .20386316
            0 85 1 4   .56215984
            0 70 1 5   .04049543
            0 65 1 6    .9321245
            0 67 0 6   .52764887
            0 64 1 5    .6974842
            0 81 1 0   2.8395345
            0 81 1 4    .7161424
            0 60 1 4    1.298246
            0 81 1 1   2.3377597
            0 80 0 6  -.27008873
            0 70 1 0   -.2315653
            0 60 0 8   -.7111181
            0 64 1 8   -.8657792
            0 65 1 8   .14117612
            0 67 0 2   1.4108043
            0 70 1 4   -.2053529
            0 74 0 5   -.2418515
            0 92 1 3 -.011776933
            0 65 1 5   .10739726
            0 72 0 5   -.6856656
            0 87 1 4    .2986597
            0 70 0 2  -.23860455
            0 67 0 8   -.7783912
            0 85 0 6   .11952727
            0 70 0 2  -.58055866
            0 68 0 0   .21524943
            0 65 1 2  -.26209623
            0 77 0 0   -.8062942
            0 70 1 4    .3648115
            0 62 0 4   .04204537
            0 65 1 5   -.4745604
            0 71 1 5    .5936369
            0 80 1 4   .13920976
            0 63 1 2    .7774609
            0 85 1 9   1.6758046
            0 75 0 3    .5708973
            0 72 1 6    .1936778
            0 62 1 1   1.0680408
            0 71 1 2    .7706921
            0 69 1 6   -.1298459
            0 61 0 4   .14453334
            0 62 0 2   .40860075
            0 63 0 4   .09376184
            0 66 0 2    .8223537
            0 66 0 6     2.49656
            0 61 0 4   2.0658643
            0 70 1 3   2.6074834
            0 65 0 3    2.422002
            0 72 1 6    2.410795
            0 69 1 1    2.497866
            0 60 0 3    .9886292
            0 71 0 4   1.4120265
            0 82 1 0   .26632148
            0 60 1 8   1.2974056
            0 73 0 4    .8859935
            0 87 1 6   .58373255
            0 60 1 4   2.8880665
            0 71 0 1   2.0435274
            0 82 0 5   2.2385416
            0 75 0 3    2.115495
            0 71 0 1    2.151605
            0 62 0 1    .4529214
            0 62 1 3   -.3073213
            0 62 1 4    .5955912
            0 64 0 1   -.3127069
            0 78 0 0   -.4978501
            0 62 0 2  .023028206
            0 61 0 2    -.422088
            0 61 1 4   -.4419667
            0 63 1 4   -.3328851
            0 67 0 5    .3818175
            0 63 0 4   -.2458737
            0 73 1 6  -1.3920542
            0 69 1 5   -.4999814
            0 79 1 6   -.9744884
            0 70 0 2   -.4932708
            end
            label values country country
            label def country 0 "A", modify
            label values gender gender
            label def gender 0 "Male", modify
            label def gender 1 "Female", modify
            Code to compute CI values for each country
            Code:
            global X "age gender"
            
            foreach x of global X {
                forval i = 0/1 {
                    qui {
                        conindex `x' if country==`i', rankvar(wealth) truezero robust
                            sca CI_`x'_`i' = r(CI)*1000
                    }
                            sca di CI_`x'_`i'
                }
            }
            Results
            Code:
            CI_age_0 =  1.0900621
              CI_age_1 = -6.8635584
            CI_gender_0 = -6.0384654
            CI_gender_1 = -27.653923
            Thank you.

            Comment


            • #7
              Dear Nick Cox,

              I hope you are doing great!

              I am writing this because I am not sure if you have missed information in #6.

              Thank you.

              Comment


              • #8
                Either I missed the ping or I didn't understand the question in #5, which refers to a community-contributed program conindex from the Stata Journal. I suggest that you use statsby to put results in new variables, but even then I don't see any connection between the graph in your image and the results you might get.

                I'd guess very many readers here would interpret CI as confidence interval. It seems that you mean "concentration index".

                Comment


                • #9
                  Dear Nick Cox,

                  Thank you for your response as well as your suggestions. You are right, CI in #6 refers to concentration index estimated from -conindex- command which I mentioned in #1.
                  Let me explain what in #6 meant. I was calculating the values of concentration index (CI) for age and gender in each country and then I wanted to plot those values for each country in one graph as illustrated by an image example in #6. For example, the total CI of country 1 would equal age1 + gender 1; country2 = age2 + gender2 and so on. I hope that this is clear to you.
                  Data
                  Code:
                  clear
                  input float(id y wealth) int age byte gender float country
                  5795  4 4 67 0 2
                  5796  5 4 67 1 2
                  5797  6 2 63 1 2
                  5798  0 5 61 1 2
                  5799  7 5 73 0 2
                  5800  8 3 56 1 2
                  5801  9 2 64 0 2
                  5803 10 1 52 1 2
                  5804 11 1 61 1 2
                  5805  0 1 56 0 2
                  5806  0 5 64 0 2
                  5807  0 5 62 0 2
                  5808 12 5 56 1 2
                  5809 13 3 63 1 2
                  5810  3 2 73 0 2
                  5811  0 1 64 1 2
                  5812  1 1 58 0 2
                  5813  0 4 64 0 2
                  5814  1 2 51 1 2
                  5815  1 3 55 1 2
                  5816  2 3 59 1 2
                  5817  3 2 64 1 2
                  5818  0 3 73 0 2
                  5819  0 3 75 1 2
                  5820  0 2 57 0 2
                  3696 12 4 66 1 4
                  3697  2 4 73 0 4
                  3698  4 5 57 1 4
                  3699  5 3 70 0 4
                  3700  6 3 56 1 4
                  3701  7 5 60 1 4
                  3702  8 4 58 0 4
                  3703  9 4 51 1 4
                  3704  0 5 61 1 4
                  3705  0 4 64 0 4
                  3706 10 4 65 1 4
                  3707  2 2 60 0 4
                  3708  1 2 59 1 4
                  3709 11 2 64 1 4
                  3710  0 1 67 1 4
                  3711  2 4 53 1 4
                  3712 13 4 54 0 4
                  3713  3 3 57 1 4
                  3714  0 3 64 0 4
                  3715  3 5 60 1 4
                  3716  1 5 61 0 4
                  3717  0 5 50 1 4
                  3718  3 5 53 1 4
                  3719  1 5 56 0 4
                  3720  2 5 71 1 4
                  1161  4 3 52 1 3
                  1162  5 2 54 1 3
                  1163  6 1 51 0 3
                  1164  7 4 56 0 3
                  1165  8 4 53 1 3
                  1166  9 1 63 1 3
                  1167 10 4 60 0 3
                  1168 11 4 50 1 3
                  1169  0 2 52 1 3
                  1170  4 1 54 0 3
                  1171  0 1 54 1 3
                  1172  0 3 52 1 3
                  1173 12 1 61 1 3
                  1174  0 1 57 0 3
                  1175  0 4 73 1 3
                  1176  0 4 75 0 3
                  1177 13 3 52 0 3
                  1178  3 1 51 1 3
                  1179  0 1 53 0 3
                  1180  0 1 50 1 3
                  1181 13 2 68 0 3
                  1182  2 2 61 1 3
                  1183  0 5 70 0 3
                  1184  0 5 65 1 3
                  1185  0 3 51 0 3
                     1  2 2 54 0 1
                     2  1 2 51 1 1
                     3  4 5 58 0 1
                     4  5 2 73 1 1
                     5  6 1 56 0 1
                     6  7 1 53 1 1
                     7  8 3 71 1 1
                     8  9 5 74 1 1
                     9 10 5 69 0 1
                    10 11 4 67 0 1
                    11 12 1 70 0 1
                    12  1 1 67 1 1
                    13  0 3 64 1 1
                    14 13 3 69 0 1
                    15  3 3 65 1 1
                    16  0 4 65 0 1
                    17  2 5 61 0 1
                    18  0 3 66 0 1
                    19  5 3 62 1 1
                    20  0 2 63 0 1
                    22  0 5 54 1 1
                    23  0 3 66 0 1
                    24  0 3 60 1 1
                    25  0 1 68 0 1
                    26  0 4 60 1 1
                  end
                  label values country countryn
                  label def countryn 1 "Austria", modify
                  label def countryn 2 "Switzerland", modify
                  label def countryn 3 "Belgium", modify
                  label def countryn 4 "Czech Republic", modify
                  Code
                  Code:
                  global X "age gender"
                  gen age1=.
                  gen age2=.
                  gen age3=.
                  gen age4=.
                  
                  gen gender1=.
                  gen gender2=.
                  gen gender3=.
                  gen gender4=.
                  
                  foreach x of global X {
                      forval i = 1/4 {
                          qui {
                              conindex `x' if country==`i', rankvar(wealth) truezero robust
                                  sca CI_`x'`i' = r(CI)*1000
                          }
                                  sca di CI_`x'_`i'
                                  replace `x'`i' = CI_`x'`i' if country==`i'
                      }
                  }
                  Thank you

                  Comment


                  • #10
                    Too late now, but this from #6 onwards should have been a different thread

                    My guess is that graph hbar with stacking is what you need.

                    Comment


                    • #11
                      Confidence Interval with Graph Dot

                      Dear all,
                      I am dealing with a similar issue. I want to add confidence intervals to a graph dot showing the mean value of anti-immigrant attitudes for each country.

                      Based on cross-sectional data including 28 countries, i generated the following example dataset consisting of the mean value of anti-immigrant attitudes for each country.
                      ----------------------- copy starting from the next line -----------------------
                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input float antmi str2 cntry
                       5.179465 "AT"
                      4.4687862 "BE"
                       5.980606 "BG"
                       3.984998 "CH"
                       5.756306 "CY"
                       6.139078 "CZ"
                       4.171445 "DE"
                       4.239356 "DK"
                       5.185469 "EE"
                      3.9809716 "ES"
                       3.886236 "FI"
                       4.790751 "FR"
                      4.1734457 "GB"
                       4.947642 "HR"
                       6.218821 "HU"
                       3.810525 "IE"
                      2.7447665 "IS"
                       5.511042 "IT"
                       4.952845 "LT"
                       4.521533 "LV"
                      4.1681414 "NL"
                       3.972714 "NO"
                       4.316811 "PL"
                       3.811007 "PT"
                       5.703016 "RS"
                       3.627834 "SE"
                       5.487343 "SI"
                       6.264863 "SK"
                      end
                      ------------------ copy up to and including the previous line ------------------

                      The graph dot attached shows the mean value of anti-immigrant attitudes (dots) over countries (categorial axis presented vertically) and is based on the following command

                      Click image for larger version

Name:	Graph.jpg
Views:	3
Size:	172.6 KB
ID:	1618629


                      HTML Code:
                      . graph dot antmi, over (cntry, sort(1)descending) ///
                      >         yline (5.09, lcolor(gray) lpattern(dash)) ///
                      >         ytitle ("Anti-Immigrant Attitudes over country")
                      I want to add confidence intervals to the graph dot for each country mean of anti-immigrant attitudes similar to Figure 1 in #1.

                      I would be very grateful for any help.

                      Best regards
                      Amelie
                      Attached Files
                      Last edited by Amelie Nickel; 13 Jul 2021, 03:39.

                      Comment

                      Working...
                      X