Announcement

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

  • Plot number of events per month, over time (simple)

    Hi Folks. Sorry Stata beginner here, but wondered if others had a good approach to what I am trying to achieve.

    I would like to graph the number of events or deaths per month, over a set period of time (e.g. a bar chart showing Jan, Feb, March from 2013 to 2016 and sum of events per month - not cumulative). I have the data currently set up as per example below - working with a date/time (DMY) and event variable (dead/alive) per participant (id). I would basically like to assess/visualise whether there are any changes in death rate over certain months across this time period (e.g. due to less reporting over certain months, etc).

    Is there a good way to code this up and plot in Stata using these simple variables? I thought it would be easy, but was drawing blanks on a good coding method that wasn't cumulative (i.e. just adding up the events over time).

    id death death_date
    1 1 27oct2013
    2 1 01jan2014
    3 1 12jan2014
    4 1 17feb2015
    5 1 04mar2015
    6 1 02apr2015
    7 1 19apr2016
    8 1 22apr2016
    9 1 24may2016
    etc.... but in much larger numbers

    Many thanks!
    Last edited by patrick handcock; 01 Feb 2021, 07:59.

  • #2
    Please note our request on how to give a data example at https://www.statalist.org/forums/help#stata 12.2 -- which as flagged is particularly important when you have date variables.

    The following example may help.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id death) float death_date
    1 1 19658
    2 1 19724
    3 1 19735
    4 1 20136
    5 1 20151
    6 1 20180
    7 1 20563
    8 1 20566
    9 1 20598
    end
    format %td death_date
    
    gen mdate = mofd(death_date)
    format mdate %tm
    egen wanted = total(death), by(mdate)
    twoway bar wanted mdate, base(0)
    Last edited by Nick Cox; 01 Feb 2021, 11:13.

    Comment


    • #3
      Apologies for that Nick Cox - I did initially attempt the dataset example via dataex, but it confused me a bit so went with simple option. Will read it again. I already have the time variable as %tm.

      That example is great - many thanks! I was just wondering if you knew if there was a simple way to get the months to display (as able) on the x-axis by any chance? For example when I do via your approach in Stata I get the below - but was after something like what I have also done in Excel (both attached below) with months included.

      Click image for larger version

Name:	image (16).png
Views:	1
Size:	44.0 KB
ID:	1592399


      Click image for larger version

Name:	image (15).png
Views:	1
Size:	35.9 KB
ID:	1592400

      Comment


      • #4
        From


        Code:
        . di ym(2013, 10)
        645
        
        . di ym(2020, 8)
        727
        and

        help datetime display formats
        it seems that you should add an extra option something like

        Code:
        xla(645/727, ang(v) format(%tmMon_CCYY))
        and you'll probably need to adjust the label size too. It looks pretty busy to me, and I'd recommend something more like

        https://www.stata-journal.com/articl...article=gr0030

        Comment


        • #5
          id year of visit month of visit visit type Sex
          1 2019 4 Initial Male
          2 2020 6 Initial Female
          2 2021 7 Revisit Female
          3 2021 7 Revisit Male
          4 2022 9 Initial Female
          5 2022 3 Initial Male
          6 2022 4 Initial Male
          6 2022 6 Revisit Male
          7 2022 3 Initial Female
          8 2022 3 Revisit Male
          9 2020 6 Initial Female
          9 2021 3 Revisit Female
          Please help. I have my data in the format above. I am trying to plot a line graph to show the trends of the visit type by sex over time. The x axis should display months and year. Your advice will be of great help. Thanks.

          Comment


          • #6
            I think you need a monthly date variable to make progress here. What else you need or want isn't so clear, but here is some technique.

            See also https://www.stata-journal.com/articl...article=gr0030 -- as otherwise an axis label for every month will just be overkill.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte id int yearofvisit byte monthofvisit str7 visittype str6 sex
            1 2019 4 "Initial" "Male"  
            2 2020 6 "Initial" "Female"
            2 2021 7 "Revisit" "Female"
            3 2021 7 "Revisit" "Male"  
            4 2022 9 "Initial" "Female"
            5 2022 3 "Initial" "Male"  
            6 2022 4 "Initial" "Male"  
            6 2022 6 "Revisit" "Male"  
            7 2022 3 "Initial" "Female"
            8 2022 3 "Revisit" "Male"  
            9 2020 6 "Initial" "Female"
            9 2021 3 "Revisit" "Female"
            end
            
            gen mdate = ym(yearofvisit, monthofvisit)
            
            bysort mdate visittype : gen count = _N 
            label var count "Number of visits"
            
            su year, meanonly 
            forval y = `r(min)'/`r(max)' { 
                local middle = ym(`y', 6) + 0.5 
                local end = ym(`y', 12) + 0.5 
                local xli `xli' `end'
                local xla `xla' `middle' "`y'"
            }
            
            su mdate, meanonly 
            local xticks `r(min)'/`r(max)'
            
            su count, meanonly 
            local ymax = r(max)
            
            set scheme s1color 
            
            separate count, by(visittype) veryshortlabel 
            
            twoway bar `r(varlist)' mdate, by(visittype, note("") col(1) legend(off)) base(0) ///
            subtitle(, pos(9) fcolor(none) nobox nobexpand) barw(0.9) /// 
            ytitle("Number of visits") ///  
            yla(0/`ymax', ang(h)) xla(`xla', tlc(none)) xli(`xli', lc(black) lw(thin)) xtitle("") xticks(`xticks')

            Click image for larger version

Name:	visits2.png
Views:	1
Size:	17.8 KB
ID:	1725337


            Notes: If you want line plots instead, you need to ensure that months with zeros are present in the data.

            If your numbers of visits is very much larger than 2 in either case, you can probably just omit the yla() option.

            Comment

            Working...
            X