Announcement

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

  • Putdocx and incidence rate

    I want to use putdocx table to output the first two columns and the first three rows from ir but when I type help I don’t see any scalars for them. I also tried using r(table)[1,1], but that doesn’t seem to work either. Can I get that info from the table or will I need to use something like total

    webuse irxmpl
    ir cases exposed time
    putdocx table table1(3,2) = ("r(table)[1,1]")
    Last edited by Laura Daniel; 20 Jun 2022, 16:27.

  • #2
    -ir- is not one of those commands that produces a table tabular matrix of results and posts them to the saved results. You can see what is returned from -ir- by immediately typing -returrn list- right after this command (and also referring to the end of the help page at -help ir-).

    If you do this, you will immediately notice two difficulties with your request and this example dataset.
    1) none of the information you want is returned from -ir-
    2) your dataset neatly has the information needed to compute these values, but it is not set up for a general solution if data have not already been summarized.

    Let's work in reverse, and you can entirely skip this step to get to your particular solution. In general, you are unlikely to have data that already summarize the total number of events/cases and exposure time for each of the exposed and unexposed groups. There are many ways you can achieve this, but this is one solution if you happen to have data arranged in a similar way, with one observation per subject, listing their event status, time at risk and and exposure status.

    For output to pudocx, again there are many ways to do this. You could opt to roll all the results up into a matrix and then add table/row column labels, but this should illustrate the fundamental process.

    Code:
    clear *
    cls
    
    webuse irxmpl
    ir cases exposed time
    
    * Optional: compute total cases and time at risk
    bys exposed : egen tot_cases = total(cases)
    bys exposed : egen tot_time = total(time)
    bys exposed : keep if _n==1
    list
    
    * output incidence rate info to table
    putdocx begin
    putdocx table Tbl = (4,3)
    
    local col = 0
    foreach status in 1 0 {
      summ tot_cases if exposed==`status', meanonly
      local cases `r(sum)'
      summ tot_time if exposed==`status', meanonly
      local time = `r(sum)'
      local ir = `cases' / `time'
    
      putdocx table Tbl(2,`=`col'+2') = (`cases')
      putdocx table Tbl(3,`=`col'+2') = (`time')
      putdocx table Tbl(4,`=`col'+2') = (`ir')
      local ++col
    }
    putdocx table Tbl(1,2) = ("Exposed")
    putdocx table Tbl(1,3) = ("Unexposed")
    putdocx table Tbl(2,1) = ("Cases")
    putdocx table Tbl(3,1) = ("Time")
    putdocx table Tbl(4,1) = ("Incidence rate")
    
    putdocx save "ir.docx", replace

    Comment


    • #3
      Thank you. I thought something like this might be necessary. I was hoping for a simpler solution. I have other data I want to put into my table (e.g. OR). I started off using the collect function and it was working nicely, but when I realized the IR wouldn't be as straightforward forward I changed over to the putdocx table.

      Comment


      • #4
        Following the strategy from the second example of the excellent FAQ How can I create table layouts from scratch by using collect get?
        Code:
        clear all
        
        webuse irxmpl
        
        ir cases exposed time
        
        collect clear
        
        collect get row1 = cases[2] , tags(col[Exposed])    
        collect get row1 = cases[1] , tags(col[Unexposed])
        
        collect get row2 = time[2]  , tags(col[Exposed])    
        collect get row2 = time[1]  , tags(col[Unexposed])
        
        collect get row3 = cases[2]/time[2] , tags(col[Exposed])    
        collect get row3 = cases[1]/time[1] , tags(col[Unexposed])    
        
        collect label levels result row1 "Cases" row2 "Time" row3 "Incidence rate"
                
        collect layout  (result)(col)
        Code:
        -----------------------------------
                       |  Exposed Unexposed
        ---------------+-------------------
        Cases          |       15        41
        Time           |    19017     28010
        Incidence rate | .0007888  .0014638
        -----------------------------------
        Last edited by Bjarte Aagnes; 21 Jun 2022, 10:28.

        Comment


        • #5
          problem solved:
          My data wasn't organized like the example dataset. I had one row per person.

          Code:
          * Compute total cases and time at risk for drug1
          quietly summ(followup_year) if `drug1'==1
          local fu : display %6.0f r(sum)
          putdocx table table2(`row',2) = ("`fu'")
          
          quietly summ(outcome) if `drug1'==1
          putdocx table table2(`row',3) = ("`r(sum)'")
          local ir = r(sum)/`fu'*100
          local ir : display %4.2f `ir'
          putdocx table table2(`row',4) = ("`ir'")
          
          * Compute total cases and time at risk for drug2
          quietly summ(followup_year) if `drug1'==0
          local fu : display %6.0f r(sum)
          putdocx table table2(`row',6) = ("`fu'")
          
          quietly summ(outcome) if `drug1'==0
          putdocx table table2(`row',7) = ("`r(sum)'")
          local ir = r(sum)/`fu'*100
          local ir : display %4.2f `ir'
          putdocx table table2(`row',8) = ("`ir'")

          Comment


          • #6
            Nice. Complementing #4 with example using individual st data (webuse page2):
            Code:
            clear all
            
            webuse page2, clear
            
            qui {
                
            collect clear
            
            collect get row1 =  r(N) , tags(col[Exposed]) : count if ( group == 2 & _d )
            scalar N2 = r(N)
              
            collect get row1 =  r(N) , tags(col[Unexposed]) : count if ( group == 1 & _d )  
            scalar N1 = r(N)
            
            collect get row2 = r(sum) , tags(col[Exposed]) : su time if group == 2 , meanonly
            collect get row2 = r(sum) , tags(col[Unexposed]) : su time if group == 1 , meanonly
            
            collect get row3 = N2/r(sum) , tags(col[Exposed]): su time if group == 2 , meanonly
            collect get row3 = N1/r(sum) , tags(col[Unexposed]) : su time if group == 1 , meanonly
            
            collect label levels result row1 "Cases" row2 "Time" row3 "Incidence rate"
            
            }
                    
            collect layout (result)(col)
            Code:
            -----------------------------------
                           |  Exposed Unexposed
            ---------------+-------------------
            Cases          |       19        17
            Time           |     5023      4095
            Incidence rate | .0037826  .0041514
            -----------------------------------
            
            . stir group
            
                    Failure _d: dead
              Analysis time _t: time
            
            Incidence-rate comparison
            
            Exposed:   group = 2
            Unexposed: group = 1
            
                             |         group          |
                             |   Exposed   Unexposed  |      Total
            -----------------+------------------------+-----------
                    Failures |        19          17  |         36
                        Time |      5023        4095  |       9118
            -----------------+------------------------+-----------
                             |                        |
              Incidence rate |  .0037826    .0041514  |   .0039482

            Comment

            Working...
            X