Announcement

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

  • Graphing charlson comorbidity index by group and decade

    Dear listers

    I have a dataset with the following variables
    Id, decade, CCI and group
    Every id can be CCI=0,1,2,3
    I would like to plot by group and decade the fraction of id's that is 0's / 1's/2's/3's

    I would like it to be stacked bars if possible, and with the sum being 100% pr bar.

    Lars

  • #2
    i ended up doing the following and that seemed to work.
    Code:
    gen CCI0=1 if CCI==0
    gen CCI1=1 if CCI==1
    gen CCI2=1 if CCI==2
    gen CCI3=1 if CCI==3
    
    collapse (count) CCI0 CCI1 CCI2 CCI3, by(group decade)
    graph bar CCI0 CCI1 CCI2 CCI3, over (decade, gap(10)) percentages stack by(group)

    Comment


    • #3
      Without knowing more of the data, this seems like a good approach. The problem with the collapse command is that it: (1) renames the variables of interest; and, (2) replaces the data in working memory with the collapsed data. It doesn't sound as if (1) is an issue for your but you may want to use the preserve and restore commands, as below.

      I'd add to your syntax as follows a few other tweaks (see below).
      Good luck!
      - Nate

      Code:
      preserve // keeps data in working memory intact
      
      CCI // number of distinct values of CCI
      loc i_max r(ndistinct) // create local equal to number of distinct values of CCI
      
      * Generate variables using loops instead
      forvalues i = 0(1)`i_max' {
      gen CCI`i' = 1 if CII == `i'
      }
      
      * Collapse
      collapse (count) CCI`i'-CC`i_max', by(group decade)
      
      * Create bar graph
      graph bar CCI`i'-CC`i_max', over (decade, gap(10)) percentages stack by(group)
      graph export graphname, as(png) // export it
      
      restore // returns to data in working memory
      d,s // short description of data in working memory
      Nathan E. Fosse, PhD
      [email protected]

      Comment

      Working...
      X