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.
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)
Thanks in advance for any help!

Comment