Announcement

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

  • Plot cumulative frequency

    I am trying to plot cumulative prevalence of smoke in my dataset. It is a dichotomous outcome (1 0) across months

    I have tried these codes
    Code:
    cumul smoke, generate(c_smoke) freq
    line month c_smoke
    but not getting the right result.

    Does anyone advise where I've got it wrong? Please see sample data below

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(sID month smoke)
      1 7 1
      2 1 0
      3 4 1
      4 2 0
      5 6 1
      6 6 1
      7 7 1
      8 4 1
      9 2 0
     10 1 .
     11 6 1
     12 2 0
     13 4 1
     14 2 0
     15 7 1
     16 4 1
     17 1 0
     18 7 1
     19 6 1
     20 1 0
     21 7 0
     22 7 0
     23 6 1
     24 4 0
     25 4 0
     26 1 0
     27 2 0
     28 2 0
     29 1 0
     30 6 1
     31 6 1
     32 1 .
     33 7 1
     34 2 0
     35 1 .
     36 4 1
     37 2 0
     38 6 1
     39 7 1
     40 4 1
     41 7 1
     42 4 1
     43 7 1
     44 2 .
     45 6 1
     46 6 1
     47 1 0
     48 1 0
     49 4 1
     50 2 .
     51 1 0
     52 2 0
     53 4 1
     54 6 1
     55 7 1
     56 4 1
     57 6 1
     58 1 0
     59 7 1
     60 2 0
     61 1 0
     62 1 0
     63 2 .
     64 7 0
     65 4 1
     66 2 .
     67 4 1
     68 6 1
     69 6 1
     70 7 0
     71 2 .
     72 1 .
     73 6 1
     74 7 .
     75 4 1
     76 7 1
     77 7 0
     78 6 0
     79 2 .
     80 4 1
     81 1 0
     82 2 0
     83 1 0
     84 6 1
     85 4 1
     86 6 1
     87 1 0
     88 4 1
     89 7 1
     90 2 0
     91 7 1
     92 7 0
     93 6 1
     94 2 .
     95 4 0
     96 7 0
     97 1 0
     98 2 .
     99 6 1
    100 4 0
    end
    label values month monthlb
    label def monthlb 1 "1 month", modify
    label def monthlb 2 "2 month", modify
    label def monthlb 4 "4 month", modify
    label def monthlb 6 "6 month", modify
    label def monthlb 7 "7 month", modify
    Thanks,

    Madu

  • #2
    I don't know what cumulative prevalence means here. Please clarify what that means and what the desired output should look like.

    Comment


    • #3
      Hi Dimitriv,

      Something like this, but the x-axis will be months
      Thanks
      Attached Files

      Comment


      • #4
        Unfortunately that is not much clearer. Hopefully this will get you started:

        Code:
        collapse (sum) smoke, by(month)
        sort month
        gen rsum   = sum(smoke)
        egen total  = total(smoke)
        gen cumul_prev = rsum/total
        tw line cumul_prev month, xlab(, value)
        This calculates the sum of smoke events that take place in each month, gets a running sum by month, and normalizes that by the total number of smoke events to get a cumulative fraction of the total.
        Last edited by Dimitriy V. Masterov; 22 Feb 2018, 22:52.

        Comment


        • #5
          Hi Dimittiy,

          Thanks for your help. One more please.

          How to I plot this data:
          clear
          input float(group month rsum total cumul_prev)
          1 1 60 1440 .04166667
          2 1 117 1440 .08125
          3 1 179 1440 .12430555
          1 2 261 1440 .18125
          2 2 330 1440 .22916667
          3 2 412 1440 .28611112
          1 4 519 1440 .3604167
          2 4 625 1440 .4340278
          3 4 737 1440 .51180553
          1 6 857 1440 .5951389
          2 6 969 1440 .6729167
          3 6 1084 1440 .7527778
          1 7 1205 1440 .8368056
          2 7 1318 1440 .9152778
          3 7 1440 1440 1
          end
          label values group grouplb
          label def grouplb 1 "A", modify
          label def grouplb 2 "B", modify
          label def grouplb 3 "C", modify
          label values month monthlb
          label def monthlb 1 "1 month", modify
          label def monthlb 2 "2 month", modify
          label def monthlb 4 "4 month", modify
          label def monthlb 6 "6 month", modify
          label def monthlb 7 "7 month", modify
          [/CODE]

          so that the three groups will be on same graph?

          I have tried this code below but its plotting on three seperate graphs.

          Code:
          global xlab247 `"xlab(1 "1 month" 2 "2 months" 4 "4 months" 6 "6 months" 7 "7 months", labsize(vsmall)) xscale(range(1))"'
          line cumul_prev month, by(group) $xlab247
          Thanks again

          Comment


          • #6
            Sorry, the correct data here:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(group month rsum total cumul_prev)
            1 1   60 1440 .04166667
            2 1  117 1440    .08125
            3 1  179 1440 .12430555
            1 2  261 1440    .18125
            2 2  330 1440 .22916667
            3 2  412 1440 .28611112
            1 4  519 1440  .3604167
            2 4  625 1440  .4340278
            3 4  737 1440 .51180553
            1 6  857 1440  .5951389
            2 6  969 1440  .6729167
            3 6 1084 1440  .7527778
            1 7 1205 1440  .8368056
            2 7 1318 1440  .9152778
            3 7 1440 1440         1
            end
            label values group grouplb
            label def grouplb 1 "A", modify
            label def grouplb 2 "B", modify
            label def grouplb 3 "C", modify
            label values month monthlb
            label def monthlb 1 "1 month", modify
            label def monthlb 2 "2 month", modify
            label def monthlb 4 "4 month", modify
            label def monthlb 6 "6 month", modify
            label def monthlb 7 "7 month", modify

            Comment


            • #7
              Easiest to declare your data to be a panel and plot:

              Code:
              xtset group month 
              xtline cumul_prev, overlay
              Or you can do my hand like this:

              Code:
              tw ///
              (line cumul_prev month if group =="A":grouplb) ///
              (line cumul_prev month if group =="B":grouplb) ///
              (line cumul_prev month if group =="C":grouplb) ///
              , legend(label(1 "A") label(2 "B") label(3 "C"))

              Comment

              Working...
              X