Announcement

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

  • Replace base level in collection estimate results with text ("Ref.")

    Hi,
    Is there a way to use collect commands to replace "1" values for base levels in a table of estimate results with the text "Ref." ?

    I have tried with collect recode and collect remap, but can't see how to replace a result value. Maybe this is meant to be impossible. But if so, how could I make a table where the base levels show up as "Ref." (without a confidence interval)? (Tired of doing it in Word later...)
    OR (95% CI)
    Sex
    Male 1.00 (1.00, 1.00)
    Female 2.50 (2.00, 3.00)
    Would instead be:
    OR (95% CI)
    Sex
    Male Ref.
    Female 2.50 (2.00, 3.00)
    Thanks,
    Scott

  • #2

    Without code showing us what you have tried (with dataex data), we are left to make assumptions on the steps you used to generate the given example.

    Here is what I came up with, using some publicly available data from the logistic help file.
    Code:
    webuse lbw
    collect: logit low i.race, or
    
    * define, style, and label a composite result for the CIs
    collect composite define ci = _r_lb _r_ub, delimiter(", ") trim
    collect style cell result[ci], sformat("(%s)")
    collect label levels result ci "__LEVEL__% CI"
    
    * custom label for the odds ratios we collected
    collect label levels result _r_b "OR", modify
    
    * define and style the composite result we want to show in the table
    collect composite define show = _r_b ci
    collect style cell result[show], nformat(%9.2f)
    
    * look at levels in dimension colname 
    collect levels colname
    
    * see what we get
    collect layout (colname[i.race]) (result[show])
    
    * show factor variable's label in the row header
    collect style header race, title(label)
    * use the stack row header style, but do not bind factor variable names
    * to their levels in each header cell
    collect style row stack, nobinder
    
    * recode the base level of i.race in dimension colname to be ignored
    collect recode colname 1.race = ignore
    
    * define new result for the base level of i.race in dimension colname
    collect get _r_b = "Ref.", tags(colname[1.race])
    
    collect preview
    Here is the resulting table.
    Code:
    ---------------------------
            |       OR (95% CI)
    --------+------------------
    Race    |
      White |              Ref.
      Black | 2.33 (0.94, 5.77)
      Other | 1.89 (0.96, 3.74)
    ---------------------------

    Comment


    • #3
      and, a search/replace might work:
      Code:
      clear all
      sysuse cancer
      qui collect : logit died i.drug
      
      collect layout (drug)(result[_r_b _r_ci])  
      collect style cell, nformat(%5.2f) 
      collect style cell result[_r_ci], ///
          cidelimiter(,) ///
          sformat("[%s]")
      
      tempfile do do2 do3
      collect export test.docx, replace dofile("`do'")
      
      * 1) filefilter
      
      filefilter "`do'" "`do2'", from(`"(`"0.00"')"') to(`"(`"ref."')"')          
      erase test.docx
      run "`do2'" 
      
      copy test.docx test1.docx, replace
      
      * 2) or using subinstr()
      
      di filewrite(                      ///
              "`do2'",                   ///
              subinstr(                  ///
                  fileread("`do'"),      /// 
                  `"(`"0.00"')"',        /// nformat(%5.2f) 
                  `"(`"ref."')"',        ///
                  1                      /// first
              )                          ///
              , 1                        /// replace file
         )                               ///
      
      erase test.docx   
      run "`do2'"
      
      copy test.docx test2.docx, replace
      
      * 3) or maybe regex - but not needed here
      
      di filewrite(                      ///
              "`do3'",                   ///
              regexreplace(              ///
                  fileread("`do'"),      ///
                  `"(`"0.00"')"',        /// nformat(%5.2f) 
                  `"(`"ref."')"'         ///
              )                          ///
              , 1                        ///
         )                               ///
      
      erase test.docx
      run "`do3'"
      
      copy test.docx test3.docx, replace
      
      * alternative use -file-
      
      exit

      Comment


      • #4
        Thanks for both suggestions -- great options.
        The use of -collect get- in that way would not have occurred to me.
        Best,
        Scott

        Comment

        Working...
        X