Announcement

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

  • share of one variable based on other variable

    Dear Profs and Colleagues,

    I have three variables:
    Education (categorial), being immigrant or native, year (2010-2019).
    I am going to generate three variables:
    1- share of immigrants in the education group 4. It means that the Edgroup=4 and immigrant=1
    2- share of native in the education group 4. t means that the Edgroup=4 and national=1
    3- share of total workers in the education group 4. It means that the Edgroup=4.
    Afterward, graph line these three variables in a single graph.
    Any ideas really appreciated.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(Edgroup immigrant national) double year
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    1 0 1 2010
    3 0 1 2010
    1 0 1 2010
    2 1 0 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    1 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    1 0 1 2010
    2 0 1 2010
    2 0 1 2010
    1 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    2 0 1 2010
    1 0 1 2010
    3 0 1 2010
    1 0 1 2010
    2 0 1 2010
    1 0 1 2010
    3 0 1 2010
    1 0 1 2010
    1 0 1 2010
    
    . tab national
    
       national |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |  1,355,562        4.89        4.89
              1 | 26,342,630       95.11      100.00
    ------------+-----------------------------------
          Total | 27,698,192      100.00
    
    . tab immi
    
      immigrant |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 | 26,342,630       95.11       95.11
              1 |  1,355,562        4.89      100.00
    ------------+-----------------------------------
          Total | 27,698,192      100.00
    
    . tab Edgroup
    
        Edgroup |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |  3,582,424       12.93       12.93
              2 | 11,419,311       41.23       54.16
              3 |  7,473,149       26.98       81.14
              4 |  5,223,308       18.86      100.00
    ------------+-----------------------------------
          Total | 27,698,192      100.00
    
    
    
    end
    cheers,
    Paris

  • #2
    Show us the result of:

    Code:
    preserve
    collapse (sum) immigrant national if Edgroup==4, by(Edgroup year)
    dataex
    restore

    Comment


    • #3

      . preserve

      .
      . collapse (sum) immigrant national if Edgroup==4, by(Edgroup year)

      .
      . dataex

      ----------------------- copy starting from the next line -----------------------
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input double year float Edgroup double(immigrant national)
      2010 4 11668 391788
      2011 4 13447 442518
      2012 4 12629 444380
      2013 4 12516 456841
      2014 4 12916 482039
      2015 4 13652 508858
      2016 4 15213 535648
      2017 4 17912 568137
      2018 4 22118 603945
      2019 4 28257 628826
      end

      Comment


      • #4
        Thanks. The total group (immigrants + nationals) is dominated by nationals, and that would make it difficult to distinguish their lines. If overlaying the lines, due to the size disparity between immigrants and nationals, you could use a log scale. However, here is an example where each group is graphed in a separate panel.


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input double year float Edgroup double(immigrant national)
        2010 4 11668 391788
        2011 4 13447 442518
        2012 4 12629 444380
        2013 4 12516 456841
        2014 4 12916 482039
        2015 4 13652 508858
        2016 4 15213 535648
        2017 4 17912 568137
        2018 4 22118 603945
        2019 4 28257 628826 
        end
        
        gen total= immigrant+ national
        rename (immigrant national total) freq=
        reshape long freq, i(Edgroup year) j(which) string
        encode which, gen(group)
        xtset group year
        xtline freq, byopts(yrescale ixaxes note("")) xtitle("") ytitle(Frequency) xlab(2010(2)2020)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	39.7 KB
ID:	1745644

        Comment


        • #5
          I really appreciated. In this case, I drop the total one only I need to have both imm and national in one single graph not in separated graphs. is it possible Prof?

          Comment


          • #6
            I would go for the log scale here.


            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input double year float Edgroup double(immigrant national)
            2010 4 11668 391788
            2011 4 13447 442518
            2012 4 12629 444380
            2013 4 12516 456841
            2014 4 12916 482039
            2015 4 13652 508858
            2016 4 15213 535648
            2017 4 17912 568137
            2018 4 22118 603945
            2019 4 28257 628826 
            end
            
            rename (immigrant national) freq=
            reshape long freq, i(Edgroup year) j(which) string
            encode which, gen(group)
            xtset group year
            xtline freq, overlay xtitle("") ytitle(Frequency) xlab(2010(2)2020) saving(gr1, replace)
            gen logfreq= log(freq)
            xtline logfreq, overlay xtitle("") ytitle(Log frequency) xlab(2010(2)2020) saving(gr2, replace)
            gr combine gr1.gph gr2.gph
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.4 KB
ID:	1745647

            Comment


            • #7
              Dear Prof Musau, It's perfect! You really helped me. That's clear and informative. Thank you so much.

              Comment

              Working...
              X