Announcement

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

  • Twoway scatter + rcap graph

    Dear All

    I ma trying to make a graph with mean and CF of 3 vars by country and product.
    I have generated the following code
    Code:
    
    foreach v in mean ub lb {
        gen `v'1 = .
        gen `v'2 = .
        gen `v'3 = .
        gen `v'4 = .
    }
    
    forval j = 1/3 {
        ci mean mSPS_`j' if product== 1 & coun==2  
        quietly {
            replace mean1 = r(mean) in `j'
            replace ub1 = r(ub) in `j'
            replace lb1 = r(lb) in `j'
        }
        
        ci mean mSPS_`j' if product== 2 & coun==2  
        quietly {
            replace mean2 = r(mean) in `j'
            replace ub2 = r(ub) in `j'
            replace lb2 = r(lb) in `j'
        }
        
        ci mean mSPS_`j' if product== 1 & coun==3  
        quietly {
            replace mean3 = r(mean) in `j'
            replace ub3 = r(ub) in `j'
            replace lb3 = r(lb) in `j'
        }
        
        ci mean mSPS_`j' if product== 2 & coun==3  
        quietly {
            replace mean4 = r(mean) in `j'
            replace ub4 = r(ub) in `j'
            replace lb4 = r(lb) in `j'
        }
        local call `call' `j' "mSPS_`j'"  
    }
    
    
    
    
    twoway scatter mean1 prod, mc(blue)  ms(O) || rcap lb1 ub1 prod, lc(blue) ///
    || scatter mean2 prod, mc(red) ms(Th) || rcap lb2 ub2 prod, lc(red) ///
    || scatter mean3 prod, mc(red) ms(Th) || rcap lb3 ub3 prod, lc(red) ///
    || scatter mean3 prod, mc(red) ms(Th) || rcap lb4 ub4 prod, lc(red) ///
    xla(`call', tlc(none)) ytitle(means and 95% confidence intervals) by(country)
    I would like to get a graph similar to this one (though i prefer to group by country and then for each country display the two products.
    Click image for larger version

Name:	Stata_gr.png
Views:	1
Size:	33.9 KB
ID:	1759498

    Could you please help me to produce the right graph?
    thanks
    Federica

  • #2
    You may need to arrange offsets.

    We need a data example to give good advice on code.

    Code:
    dataex mean? lb? ub? in 1/3
    but also details on what are products 1 and 2 and countries 2 and 3, etc., etc.

    Please show the results of


    Code:
    tab product, nola
    
    tab product
    
    tab coun, nola
    
    tab coun
    
    describe MSPS_?

    Comment


    • #3
      Hi Nick,
      here an example from my dataset. mSPS_* are scores from 0 to 10.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int id byte country float(product mSPS_1 mSPS_2 mSPS_3)
       1 2 1  6  7 10
       1 2 1  4  2 10
       1 2 2  7 10  4
       1 2 2  4  7  8
       2 2 1  9  1  4
       2 2 1  5  5  1
       2 2 2  6  8  2
       2 2 2  3  2  6
       3 2 1  8  7  3
       3 2 1  6  5  4
       3 2 2  9  6  3
       3 2 2  2  7  3
       4 2 1  3  6  5
       4 2 1  3  2  4
       4 2 2  6  5  7
       4 2 2  1  1  2
       5 2 1  6  9  5
       5 2 1  5  7  3
       5 2 2  8  2  4
       5 2 2  6  7  3
       6 2 1  2  3  2
       6 2 1  2  2  3
       6 2 2  2  5  2
       6 2 2  2  2  3
       7 2 1  8  3  5
       7 2 1  3  1  8
       7 2 2  9  7  4
       7 2 2  5 10  1
       8 2 1  6  8  7
       8 2 1  6  4  8
       8 2 2  6  9  7
       8 2 2  4  6  5
       9 2 1  7  7  7
       9 2 1  9  3  8
       9 2 2  9  9  9
       9 2 2  9  9  9
      10 2 1  6  5  7
      10 2 1  5  6  4
      10 2 2  8  5  8
      10 2 2  4  3  5
      11 2 1  8  2  4
      11 2 1  5  1  2
      11 2 2  8  6  2
      11 2 2  7  2  4
      12 2 1  5  5  8
      12 2 1  5  6  8
      12 2 2  7  4  6
      12 2 2  7  3  8
      13 2 1  6  8  5
      13 2 1  3  6  7
      13 2 2  6  4  7
      13 2 2  7  5  6
      14 3 1  8  6  8
      14 3 1  7  5  7
      14 3 2  5  4  6
      14 3 2  5  5  6
      15 3 1  8  4  6
      15 3 1  3  3  6
      15 3 2 10  7  5
      15 3 2  6  9  7
      16 3 1  9 10  9
      16 3 1  8  6  9
      16 3 2  6  6  8
      16 3 2  7  8  7
      17 3 1  2  6  3
      17 3 1  4  2  1
      17 3 2  7  7  4
      17 3 2  5  6  3
      18 3 1  8  9 10
      18 3 1 10  9 10
      18 3 2  9  9  9
      18 3 2  9  9 10
      19 3 1  5  5  3
      19 3 1  3  4  4
      19 3 2  6  7  6
      19 3 2  8  8  9
      20 3 1  5  7  6
      20 3 1  4  5  6
      20 3 2  4  5  6
      20 3 2  7  4  1
      21 3 1  5  6  9
      21 3 1  8  5  6
      21 3 2  4  7  9
      21 3 2  9  7  5
      22 3 1  7  9  7
      22 3 1  8  5 10
      22 2 2 10  8  9
      22 3 2  8  8  7
      23 3 1  5  5  5
      23 3 1  5  3  6
      23 3 2  5  6  3
      23 3 2  4  5  5
      24 3 1  6  9  7
      24 3 1  5  6  7
      24 3 2  5  6  8
      24 3 2  7  5  6
      25 3 1  3  5  2
      25 3 1  3  2  6
      25 3 2 10  9  9
      25 3 2  5  3  7
      end
      label values country country
      label def country 2 "DE" 3 "HU", modify
      label values product product
      label def product 1 "Prod 1", modify
      label def product 2 "Prod 2", modify
      product Freq. Percent Cum.
      1 1,600 50.00 50.00
      2 1,600 50.00 100.00
      Total 3,200 100.00
      product Freq. Percent Cum.
      Prod 1 1,600 50.00 50.00
      Prod 2 1,600 50.00 100.00
      Total 3,200 100.00
      Co Freq. Percent Cum.
      Germany 1,600 50.00 50.00
      Hungury 1,600 50.00 100.00
      Total 3,200 100.00
      storage display value
      variable name type format label variable label
      mSPS_1 float %9.0g mSPS_DE
      mSPS_2 float %9.0g mSPS_HU
      mSPS_3 float %9.0g mSPS_LI
      Thanks for helping
      F

      Comment


      • #4
        Could you confirm that mSPS_1 refers to DE (Germany) even though that is country 2 and similarly that mSPS_2 refers to Hungary (not Hungury, by the way) even though that is country 3?

        Comment


        • #5
          This may help. Using #1 as well as #3 I have made some guesses, including about what you ask for but don't really want and vice versa.

          See also https://journals.sagepub.com/doi/pdf...867X1001000112


          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int id byte country float(product mSPS_1 mSPS_2 mSPS_3)
           1 2 1  6  7 10
           1 2 1  4  2 10
           1 2 2  7 10  4
           1 2 2  4  7  8
           2 2 1  9  1  4
           2 2 1  5  5  1
           2 2 2  6  8  2
           2 2 2  3  2  6
           3 2 1  8  7  3
           3 2 1  6  5  4
           3 2 2  9  6  3
           3 2 2  2  7  3
           4 2 1  3  6  5
           4 2 1  3  2  4
           4 2 2  6  5  7
           4 2 2  1  1  2
           5 2 1  6  9  5
           5 2 1  5  7  3
           5 2 2  8  2  4
           5 2 2  6  7  3
           6 2 1  2  3  2
           6 2 1  2  2  3
           6 2 2  2  5  2
           6 2 2  2  2  3
           7 2 1  8  3  5
           7 2 1  3  1  8
           7 2 2  9  7  4
           7 2 2  5 10  1
           8 2 1  6  8  7
           8 2 1  6  4  8
           8 2 2  6  9  7
           8 2 2  4  6  5
           9 2 1  7  7  7
           9 2 1  9  3  8
           9 2 2  9  9  9
           9 2 2  9  9  9
          10 2 1  6  5  7
          10 2 1  5  6  4
          10 2 2  8  5  8
          10 2 2  4  3  5
          11 2 1  8  2  4
          11 2 1  5  1  2
          11 2 2  8  6  2
          11 2 2  7  2  4
          12 2 1  5  5  8
          12 2 1  5  6  8
          12 2 2  7  4  6
          12 2 2  7  3  8
          13 2 1  6  8  5
          13 2 1  3  6  7
          13 2 2  6  4  7
          13 2 2  7  5  6
          14 3 1  8  6  8
          14 3 1  7  5  7
          14 3 2  5  4  6
          14 3 2  5  5  6
          15 3 1  8  4  6
          15 3 1  3  3  6
          15 3 2 10  7  5
          15 3 2  6  9  7
          16 3 1  9 10  9
          16 3 1  8  6  9
          16 3 2  6  6  8
          16 3 2  7  8  7
          17 3 1  2  6  3
          17 3 1  4  2  1
          17 3 2  7  7  4
          17 3 2  5  6  3
          18 3 1  8  9 10
          18 3 1 10  9 10
          18 3 2  9  9  9
          18 3 2  9  9 10
          19 3 1  5  5  3
          19 3 1  3  4  4
          19 3 2  6  7  6
          19 3 2  8  8  9
          20 3 1  5  7  6
          20 3 1  4  5  6
          20 3 2  4  5  6
          20 3 2  7  4  1
          21 3 1  5  6  9
          21 3 1  8  5  6
          21 3 2  4  7  9
          21 3 2  9  7  5
          22 3 1  7  9  7
          22 3 1  8  5 10
          22 2 2 10  8  9
          22 3 2  8  8  7
          23 3 1  5  5  5
          23 3 1  5  3  6
          23 3 2  5  6  3
          23 3 2  4  5  5
          24 3 1  6  9  7
          24 3 1  5  6  7
          24 3 2  5  6  8
          24 3 2  7  5  6
          25 3 1  3  5  2
          25 3 1  3  2  6
          25 3 2 10  9  9
          25 3 2  5  3  7
          end
          label values country country
          label def country 2 "DE" 3 "HU", modify
          label values product product
          label def product 1 "Prod 1", modify
          label def product 2 "Prod 2", modify
          
          * my code starts here 
          
          save product_country, replace 
          
          forval j = 1/3 { 
              use product_country, clear 
              statsby , by(country product) : ci mean mSPS_`j'
              gen version = `j'
              save results`j', replace 
          }
          
          append using results2 results1 
          
          list, sepby(version country)
          
          label def version 1 "DE version" 2 "HU version" 3 "LI version"
          label val version version
          
          foreach v in mean ub lb { 
              separate `v', by(version) veryshortlabel 
          }
          
          gen productL = product - 0.2 
          gen productR = product + 0.2 
          
          
          twoway scatter mean1 productL, by(country, note("")) mc(blue)  ms(O) || rcap lb1 ub1 productL, lc(blue) ///
          || scatter mean2 product, mc(red) ms(Th) || rcap lb2 ub2 product, lc(red) ///
          || scatter mean3 productR, mc(black) ms(Dh) || rcap lb3 ub3 productR, lc(black) ///
          xla(1 2, tlc(none) valuelabel) xtitle(product) ytitle(means and 95% confidence intervals) legend(order(1 3 5))
          Click image for larger version

Name:	country_product.png
Views:	1
Size:	68.9 KB
ID:	1759765

          Comment


          • #6
            Hi Nick,

            thanks a lot. This is exactly what I was trying to do.
            For the readers I confirm that your assumptions are correct. There are two countries DE and HU and two products (let's say product 1 and 2). For each product we have 3 scores representing a rating for the DE version (mSPS_1), HU version (mSPS_2) and LI version (mSPS_3).

            Thanks
            Federica

            Comment


            • #7
              Good!

              On offsets see also https://journals.sagepub.com/doi/pdf...867X0700700112

              Comment


              • #8
                Ok thanks
                F

                Comment

                Working...
                X