Announcement

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

  • Create an annual cumulative variables by year and plot them in a time-series graph in Stata.

    Hello Stata users,

    I am working with Stata 13.1 and have the following data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 yearandmonth long(Live_Births_per_Month Deaths_per_Month)
    "1950-05" 76436 45515
    "1950-06" 72030 40095
    "1950-07" 73248 39798
    "1950-08" 70728 38124
    "1950-09" 68156 37075
    "1950-10" 69357 42232
    "1950-11" 65008 44418
    "1950-12" 67156 49860
    "1951-01" 71119 63048
    "1951-02" 66113 58340
    "1951-03" 72954 58959
    "1951-04" 71787 49690
    "1951-05" 74698 46496
    "1951-06" 70322 40359
    "1951-07" 73716 40199
    "1951-08" 68929 37670
    "1951-09" 66254 37288
    "1951-10" 65112 42997
    "1951-11" 60226 42370
    "1951-12" 65492 48413
    "1952-01" 70350 53364
    "1952-02" 67033 51750
    "1952-03" 73414 50111
    "1952-04" 71715 44752
    "1952-05" 72814 41167
    "1952-06" 68354 38144
    "1952-07" 69876 40266
    "1952-08" 68979 36246
    "1952-09" 66843 36278
    "1952-10" 65510 41385
    "1952-11" 62940 42467
    "1952-12" 64376 48901
    "1953-01" 69771 69112
    "1953-02" 64564 73023
    "1953-03" 71538 54100
    "1953-04" 67876 43573
    "1953-05" 71645 42651
    "1953-06" 67705 37468
    "1953-07" 71225 36807
    "1953-08" 68549 36685
    "1953-09" 65045 35488
    "1953-10" 62643 40885
    "1953-11" 59440 43243
    "1953-12" 64695 43948
    "1954-01" 70215 52729
    "1954-02" 64826 53607
    "1954-03" 70406 49423
    "1954-04" 69474 44349
    "1954-05" 72752 43538
    "1954-06" 66940 38096
    "1954-07" 70499 37671
    "1954-08" 69177 36316
    "1954-09" 65781 35450
    "1954-10" 65149 40719
    "1954-11" 61497 41178
    "1954-12" 64038 45816
    "1955-01" 69253 52017
    "1955-02" 63330 46623
    "1955-03" 71800 56974
    "1955-04" 70245 45364
    "1955-05" 72035 41749
    "1955-06" 67817 38477
    "1955-07" 70238 38211
    "1955-08" 67434 37969
    "1955-09" 63842 35745
    "1955-10" 64480 41772
    "1955-11" 60667 43595
    "1955-12" 64776 47826
    "1956-01" 68254 52107
    "1956-02" 66290 61556
    "1956-03" 71586 58113
    "1956-04" 68117 46263
    "1956-05" 72508 44009
    "1956-06" 66428 38974
    "1956-07" 70485 38820
    "1956-08" 67844 36144
    "1956-09" 65080 36638
    "1956-10" 63857 40669
    "1956-11" 61108 44859
    "1956-12" 65359 47548
    "1957-01" 71369 56610
    "1957-02" 65515 45707
    "1957-03" 71804 43656
    "1957-04" 68475 41437
    "1957-05" 70838 40495
    "1957-06" 66762 39094
    "1957-07" 67841 39774
    "1957-08" 68944 36455
    "1957-09" 67702 36454
    "1957-10" 67028 45258
    "1957-11" 63662 52329
    "1957-12" 66527 54838
    "1958-01" 70099 51563
    "1958-02" 64465 43451
    "1958-03" 71624 48476
    "1958-04" 67376 43746
    "1958-05" 72090 40009
    "1958-06" 67490 36288
    "1958-07" 66214 36939
    "1958-08" 64077 35096
    end
    • A monthly time variable ranging from May 1950 to 2025
    • The number of live births per month
    • The number of deaths per month
    I would like to construct annual variables that represent the cumulative sum of monthly live births and monthly deaths over a 12-month period, with each year starting in May.

    Once these annual cumulative variables are created, the goal is to plot them together in a single time-series graph, showing one curve for cumulative live births and one for cumulative deaths for each year.

    Any guidance on how to implement this in Stata would be greatly appreciated.

    Thanks!

  • #2
    One way to do something in the direction of what you want would be:
    Code:
    gen year = real(substr(yearandmonth, 1,4))
    collapse (sum) births = Live_Births_per_Month deaths = Deaths_per_Month, by(year)
    twoway (line births year, sort) (line deaths year, sort)

    Comment


    • #3
      This may help

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str7 yearandmonth long(Live_Births_per_Month Deaths_per_Month)
      "1950-05" 76436 45515
      "1950-06" 72030 40095
      "1950-07" 73248 39798
      "1950-08" 70728 38124
      "1950-09" 68156 37075
      "1950-10" 69357 42232
      "1950-11" 65008 44418
      "1950-12" 67156 49860
      "1951-01" 71119 63048
      "1951-02" 66113 58340
      "1951-03" 72954 58959
      "1951-04" 71787 49690
      "1951-05" 74698 46496
      "1951-06" 70322 40359
      "1951-07" 73716 40199
      "1951-08" 68929 37670
      "1951-09" 66254 37288
      "1951-10" 65112 42997
      "1951-11" 60226 42370
      "1951-12" 65492 48413
      "1952-01" 70350 53364
      "1952-02" 67033 51750
      "1952-03" 73414 50111
      "1952-04" 71715 44752
      "1952-05" 72814 41167
      "1952-06" 68354 38144
      "1952-07" 69876 40266
      "1952-08" 68979 36246
      "1952-09" 66843 36278
      "1952-10" 65510 41385
      "1952-11" 62940 42467
      "1952-12" 64376 48901
      "1953-01" 69771 69112
      "1953-02" 64564 73023
      "1953-03" 71538 54100
      "1953-04" 67876 43573
      "1953-05" 71645 42651
      "1953-06" 67705 37468
      "1953-07" 71225 36807
      "1953-08" 68549 36685
      "1953-09" 65045 35488
      "1953-10" 62643 40885
      "1953-11" 59440 43243
      "1953-12" 64695 43948
      "1954-01" 70215 52729
      "1954-02" 64826 53607
      "1954-03" 70406 49423
      "1954-04" 69474 44349
      "1954-05" 72752 43538
      "1954-06" 66940 38096
      "1954-07" 70499 37671
      "1954-08" 69177 36316
      "1954-09" 65781 35450
      "1954-10" 65149 40719
      "1954-11" 61497 41178
      "1954-12" 64038 45816
      "1955-01" 69253 52017
      "1955-02" 63330 46623
      "1955-03" 71800 56974
      "1955-04" 70245 45364
      "1955-05" 72035 41749
      "1955-06" 67817 38477
      "1955-07" 70238 38211
      "1955-08" 67434 37969
      "1955-09" 63842 35745
      "1955-10" 64480 41772
      "1955-11" 60667 43595
      "1955-12" 64776 47826
      "1956-01" 68254 52107
      "1956-02" 66290 61556
      "1956-03" 71586 58113
      "1956-04" 68117 46263
      "1956-05" 72508 44009
      "1956-06" 66428 38974
      "1956-07" 70485 38820
      "1956-08" 67844 36144
      "1956-09" 65080 36638
      "1956-10" 63857 40669
      "1956-11" 61108 44859
      "1956-12" 65359 47548
      "1957-01" 71369 56610
      "1957-02" 65515 45707
      "1957-03" 71804 43656
      "1957-04" 68475 41437
      "1957-05" 70838 40495
      "1957-06" 66762 39094
      "1957-07" 67841 39774
      "1957-08" 68944 36455
      "1957-09" 67702 36454
      "1957-10" 67028 45258
      "1957-11" 63662 52329
      "1957-12" 66527 54838
      "1958-01" 70099 51563
      "1958-02" 64465 43451
      "1958-03" 71624 48476
      "1958-04" 67376 43746
      "1958-05" 72090 40009
      "1958-06" 67490 36288
      "1958-07" 66214 36939
      "1958-08" 64077 35096
      end
      
      split yearandmonth, parse("-") destring gen(date)
      
      gen ncy = cond(date2 <= 4, date1 - 1, date1)
      gen ncm = cond(date2 <= 4, date2 + 8, date2 - 4)
      
      gen mdate = monthly(yearandmonth, "YM")
      format mdate %tm 
      
      bysort ncy (ncm) : gen cubirths = sum(Live)
      by ncy: gen cudeaths = sum(Deaths_per_Month)
      
      line cu* ncm, c(L) xla(1 "5" 2 "6" 3 "7" 4 "8" 5 "9" 6 "10" 7 "11" 8 "12" 9 "1" 10 "2" 11 "3" 12 "4") legend(order(1 "Cumulative births" 2 "Cumulative deaths") col(1) pos(11) ring(0)) xtitle(Month of year)

      Comment


      • #4
        I rather fear that the second graph here -- that shown by an image as well as code -- is what you're asking for. The scope for this being useful for all 75 or so years appears remote to me.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str7 yearandmonth long(Live_Births_per_Month Deaths_per_Month)
        "1950-05" 76436 45515
        "1950-06" 72030 40095
        "1950-07" 73248 39798
        "1950-08" 70728 38124
        "1950-09" 68156 37075
        "1950-10" 69357 42232
        "1950-11" 65008 44418
        "1950-12" 67156 49860
        "1951-01" 71119 63048
        "1951-02" 66113 58340
        "1951-03" 72954 58959
        "1951-04" 71787 49690
        "1951-05" 74698 46496
        "1951-06" 70322 40359
        "1951-07" 73716 40199
        "1951-08" 68929 37670
        "1951-09" 66254 37288
        "1951-10" 65112 42997
        "1951-11" 60226 42370
        "1951-12" 65492 48413
        "1952-01" 70350 53364
        "1952-02" 67033 51750
        "1952-03" 73414 50111
        "1952-04" 71715 44752
        "1952-05" 72814 41167
        "1952-06" 68354 38144
        "1952-07" 69876 40266
        "1952-08" 68979 36246
        "1952-09" 66843 36278
        "1952-10" 65510 41385
        "1952-11" 62940 42467
        "1952-12" 64376 48901
        "1953-01" 69771 69112
        "1953-02" 64564 73023
        "1953-03" 71538 54100
        "1953-04" 67876 43573
        "1953-05" 71645 42651
        "1953-06" 67705 37468
        "1953-07" 71225 36807
        "1953-08" 68549 36685
        "1953-09" 65045 35488
        "1953-10" 62643 40885
        "1953-11" 59440 43243
        "1953-12" 64695 43948
        "1954-01" 70215 52729
        "1954-02" 64826 53607
        "1954-03" 70406 49423
        "1954-04" 69474 44349
        "1954-05" 72752 43538
        "1954-06" 66940 38096
        "1954-07" 70499 37671
        "1954-08" 69177 36316
        "1954-09" 65781 35450
        "1954-10" 65149 40719
        "1954-11" 61497 41178
        "1954-12" 64038 45816
        "1955-01" 69253 52017
        "1955-02" 63330 46623
        "1955-03" 71800 56974
        "1955-04" 70245 45364
        "1955-05" 72035 41749
        "1955-06" 67817 38477
        "1955-07" 70238 38211
        "1955-08" 67434 37969
        "1955-09" 63842 35745
        "1955-10" 64480 41772
        "1955-11" 60667 43595
        "1955-12" 64776 47826
        "1956-01" 68254 52107
        "1956-02" 66290 61556
        "1956-03" 71586 58113
        "1956-04" 68117 46263
        "1956-05" 72508 44009
        "1956-06" 66428 38974
        "1956-07" 70485 38820
        "1956-08" 67844 36144
        "1956-09" 65080 36638
        "1956-10" 63857 40669
        "1956-11" 61108 44859
        "1956-12" 65359 47548
        "1957-01" 71369 56610
        "1957-02" 65515 45707
        "1957-03" 71804 43656
        "1957-04" 68475 41437
        "1957-05" 70838 40495
        "1957-06" 66762 39094
        "1957-07" 67841 39774
        "1957-08" 68944 36455
        "1957-09" 67702 36454
        "1957-10" 67028 45258
        "1957-11" 63662 52329
        "1957-12" 66527 54838
        "1958-01" 70099 51563
        "1958-02" 64465 43451
        "1958-03" 71624 48476
        "1958-04" 67376 43746
        "1958-05" 72090 40009
        "1958-06" 67490 36288
        "1958-07" 66214 36939
        "1958-08" 64077 35096
        end
        
        split yearandmonth, parse("-") destring gen(date)
        
        gen ncy = cond(date2 <= 4, date1 - 1, date1)
        gen ncm = cond(date2 <= 4, date2 + 8, date2 - 4)
        
        gen mdate = monthly(yearandmonth, "YM")
        format mdate %tm 
        
        
        
        bysort ncy (ncm) : gen cubirths = sum(Live)
        by ncy: gen cudeaths = sum(Deaths_per_Month)
        
        line cu* ncm, c(L) xla(1 "5" 2 "6" 3 "7" 4 "8" 5 "9" 6 "10" 7 "11" 8 "12" 9 "1" 10 "2" 11 "3" 12 "4") legend(order(1 "Cumulative births" 2 "Cumulative deaths") col(1) pos(11) ring(0)) xtitle(Month of year)
        
        separate cubirths, by(ncy) generate(c1)
        separate cudeaths, by(ncy) generate(c2)
        
        unab c1 : c1*
        unab c2 : c2*
        local last : word count `c1' `c2'
        
        gen year = real(substr(yearandmonth, 1, 4)) 
        
        su year, meanonly 
        forval y = `r(min)'(2)`r(max)' { 
            local labels `labels' `=ym(`y', 10)' "`y'"
        }
        
        line c1* mdate, lc(red ..) xla(`labels', noticks) xtitle("") || line c2* mdate, lc(blue ..) legend(order(1 "Cumulative births" `last' "Cumulative deaths") pos(11))
        Click image for larger version

Name:	aziz_cum.png
Views:	1
Size:	148.6 KB
ID:	1783757

        Comment


        • #5
          Nick Cox & Mike Lacy Thank you to both of you!

          Comment

          Working...
          X