Announcement

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

  • Stata coefplot: Show Only Main Predictor While Still Adjusting for Covariates


    I'm running Poisson regression models in Stata to estimate the effect of an exposure variable on different outcome measures, while adjusting for several covariates. I want to plot only the effect of exposure on the outcomes and hide the effects of the covariates in the coefplot. The covariates should still be included in the model for adjustment, but they should not appear in the plot.

    This is the plot resulting from my code, and I want to suppress the greyed out part of the forrest plot.
    Click image for larger version

Name:	Screenshot 2025-01-28 at 11.35.47.png
Views:	1
Size:	151.4 KB
ID:	1771665




    Here’s my code. As I plan to include a dozen of different outcome measures, it is implemented as a loop.

    Code:
    clear
    
    ssc install coefplot
    
    input byte(exposure) byte(outcome_primary outcome_secondary outcome_tertiary) byte(covariate_1 covariate_2 covariate_3)
    0 1 0 1 0 1 0
    1 0 1 0 1 1 0
    0 1 1 1 0 0 1
    1 1 0 0 1 0 1
    1 1 0 0 0 1 0
    0 0 0 1 1 0 1
    1 1 0 1 1 1 0
    end
    
    * List of outcome variables
    local outcome_measures outcome_primary outcome_secondary outcome_tertiary
    
    * Compute the models and store them using a loop
    foreach var in `outcome_measures' {
    glm `var' exposure covariate_1 covariate_2 covariate_3, fam(poisson) link(log) nolog vce(robust) eform
    estimates store r_`var'
    }
    
    * Create the coefplot command dynamically
    local coefplot_cmd ""
    foreach var in `outcome_measures' {
    local coefplot_cmd "`coefplot_cmd' r_`var', aseq(`var') \"
    }
    
    * Remove the last "\" (backslash) at the end of the list for clean syntax
    local coefplot_cmd = substr("`coefplot_cmd'", 1, length("`coefplot_cmd'") - 2)
    
    * Forest plot with a logarithmic X-axis
    coefplot (`coefplot_cmd'), ///
    drop(_cons) /// Removes the intercept
    swapnames /// Ensures that the labels are correct
    xline(1, lcolor(black)) /// Reference line at RR = 1
    eform /// Displays exponentiated coefficients (Risk Ratios)
    xtitle("Risk Ratio (log scale)") ///
    xscale(log) /// Sets the X-axis to a logarithmic scale
    title("Risk Ratios for Different Outcome Measures") ///
    name(Plot1, replace)
    The problem: I want to ensure that only the coefficient for exposure is shown, while covariate_1, covariate_2, and covariate_3 are used for adjustment but do not appear in the plot. Is my approach correct, or is there a better way to do this in Stata?

    Thanks in advance for any help!

    Last edited by Tim Wallner; 28 Jan 2025, 04:03.

  • #2
    See the -keep()- or -drop()- options of coefplot. Note that coefplot is from SSC, as you are asked to explain (FAQ Advice #12).

    Comment


    • #3
      Thank you for pointing me in the right direction. The options keep() and drop() were the right solution.

      I have a follow-up question regarding the tabular presentation of my regression data. In the medical literature, it is common to use a format like this:

      Code:
      .------------------------------------------------------------------------------.
      |                         | Exposed        | Control        | RR   | 95% CI    |
      |-------------------------+----------------+----------------+------+-----------|
      | Outcome of interest     | n       | %    | n       | %    |      |           |
      |-------------------------+---------+------+---------+------+------+-----------|
      | Total No. patients      | 6,955   |      | 9,520   |      |      |           |
      | In-hospital death       | 2,418   | 34.8 | 3,944   | 41.4 | 1.25 | 1.17–1.33 |
      | Prolonged intubation    | 514     | 7.4  | 1,149   | 12.1 | 1.54 | 1.38–1.72 |
      | etc etc ...             | ...     | ...  | ...     | ...  | ...  | ...       |
      `------------------------------------------------------------------------------'
      I have explored etable and collect, but these commands typically structure the table in the opposite way, with outcomes/models as columns instead of rows. However, in my field, the convention is to present outcomes as rows while keeping patient subgroups (e.g., cohorts) as columns.

      I am wondering if there is a solution or workaround in Stata to achieve this format efficiently. Would etable be suitable for this, or would another approach be more appropriate?

      Any insights or guidance would be greatly appreciated!

      Comment


      • #4
        It should be possible! However, your question does not align with the topic of this thread, so I suggest starting a new thread with an appropriate title and asking your question there.

        Comment

        Working...
        X