//OFF
cd "C:\Users\au24026\Documents\stata\StataHacks\docs\Coding Stata\coefplot"
use stan3, clear
//ON
/***
# Introduction
To create a CI plot of crude estimates using Mata matrices and coefplot.
In this example we use hazard ratio estimates for the coefplot.
Further it is shown how to add the estimates and CI limits as labels in the
ciplot.
## The command coefplot
Note that coefplot might be installed on your version of Stata.
If coefplot is not installed, run the command:
`ssc install coefplot`
To see if coefplot is installed see if the command below returns a help page:
`help coefplot`
## The data
We use the Stata example dataset stan3 (Heart transplant survival data from
Stanford).
`webuse stan3, clear`
***/
/**/label variable posttran "After tranplantation"
/**/label define posttran 0 "No" 1 "Yes"
/**/label values posttran posttran
metadata id year age died stime surgery transplant wait posttran
/***
The dataset is already set for Cox regression:
***/
stset
/***
The dataset will be used without taking the case story of the dataset into
account. So the results as such are pure rubish.
# Howto create a ciplot using coefplot
Before creating the ciplot we gather (crude) estimates and CI limits in the
Mata matrix estimates.
Also we gather the variable names for which estimates are found in the Stata
local rownames.
In order to use coefplot a Stata matrix is needed so at the end the Mata matrix
and the local rownames are transformed into a Stata matrix.
Then the coefplot command is used to generate a ciplot.
Finally it is shown how a label with estimates can be added the coefplot.
## Step 1: Reset and start
Both the local and the Mata matrix has to be reset before start:
***/
local rownames ""
mata: estimates = J(0,3,.)
/***
## Step 2: Gather estimates in local "rownames" and Mata matrix "estimates"
A foreach loop is used to loop through the variables for which the estimates
are needed.
In the loop the regression of choice is made. The regression is formulated
referring to Stata local var.
All Stata regression returns a table of regression estimates in the Stata
named matrix r(table).
In the r(table) regression estimates are in row 1 and lower and upper CI limits
are in rows 5 and 6, respectively.
It is easier to extract data from Mata matrices, so the values of r(table)
are copied into a Mata matrix (st_matrix("r(table)")), transposed (') and
columns 1, 5 and 6 are added to the matrix estimates.
Since estimates from the regression comes in the order of the independent
regression variables ending with constant estimates only the first row of the
transposed r(table).
The variable names are gathered in the local rownames. They are needed as labels
on the y-scale:
foreach var of varlist year surgery transplant wait posttran {
stcox `var'
mata: estimates = estimates \ st_matrix("r(table)")'[1, (1,5,6)]
local rownames `rownames' `var'
}
***/
//OFF
foreach var of varlist year surgery transplant wait posttran {
stcox `var'
mata: estimates = estimates \ st_matrix("r(table)")'[1, (1,5,6)]
local rownames `rownames' `var'
}
//ON
/***
## Step 3: Move estimates back into a Stata matrix "estimates"
The values from the Mata matrix are moved into the Stata matrix with the same
name.
Rownames (from Stata local rownames) and column names (HZ, LL, and UL) are added
to make the coefplot more readable.
***/
mata: st_matrix("estimates", estimates)
matrix rownames estimates = `rownames'
matrix colnames estimates = HZ LL UL
/***
The Stata matrix "estimates" looks like
***/
matprint estimates, d(3)
/***
## Step 4: Doing the ciplot
To make a ciplot based on the Stata matrix "estimates" using coefplot it is
needed to tell in which column the estimates are [matrix(estimates[,1])]
and in which columns the ci limits are [ci((estimates[,2] estimates[,3]))].
***/
coefplot matrix(estimates[,1]), ci((estimates[,2] estimates[,3])) xline(1)
//OFF
graph export simple_ciplot.png, width(1600) height(1200) replace
//ON
/***
## Step 5: Adding estimates and ci limits as labels
This has become much easier from version 1.8.1.
In option **mlabel** it is specified how the confidence interval should look
based on temporary internal variables.
Then telling how the confidence interval is to appear options like
**mlabposition** and **mlabsize**:
***/
/**/coefplot matrix(estimates[,1]), ci((estimates[,2] estimates[,3])) xline(1, lcolor(red%40)) ///
mlabel(string(@b, "%5.2f") + " (" + string(@ll, "%5.2f") + "; " + string(@ul, "%5.2f") + ")") ///
mlabposition(12) mlabsize(vsmall)
//OFF
graph export simple_ciplot_lbls.png, width(1600) height(1200) replace
graph drop _all
//ON
/***
![A simple ciplot using coefplot with added labels](simple_ciplot_lbls.png)
***/