cd "G:\ray\fent_morph\" cap qui log c set linesize 70 translator set smcl2txt linesize 80 *log using descriptive, replace qui log using descriptive, replace set more off * * Read in the provided data set. * use "Fentanyl limited data set two variables.dta", clear describe * * Calculate log odds of ps_2 * gen logodds=logit(ps_2 ) * * Draw histograms of those receiving Morphine and * those receiving Fentanyl. * histogram logodds, by(fent_morph) xlabel(-2(.5)0) percent histogram logodds, percent by(, caption(all data)) by(fent_morph) xlabel(-2(.5)0) qui graph export graph0.png, replace // ![ ](./graph0.png) * * Get some descriptive information of logodds * by treatment. * by fent_morph , sort: summ logodds, detail * * Display same descriptives over all patients. * summ logodds, detail * * Set a patient identifier to be the record * number in the file. * gen id=_n * * Group the data set into deciles of logodds. * Set the variable decile to indicate into which * group each patient falls. * xtile decile = logodds, nquantiles(10) * * For each decile calculate variables * nf=number of fentanyl patients * nm=number of morphine patients * by decile , sort: egen nf=total(fent_morph ==1) by decile , sort: egen nm=total(fent_morph ==0) * * The variable "select" will tell us which records * to keep for the analysis data set. * * We want to keep all fentanyl patients so set * select=1 for these. * gen select=fent_morph * * Now we want to randomly select one morphine * patient for each fentanyl patient within each decile group. * * Using the method of Gould taken from * http://blog.stata.com/2012/08/03/using-statas-random-number-generators-part-2-drawing-without-replacement/ * * Place observations in random order... * set seed 55512121 sort id generate double u1=runiform() generate double u2=runiform() sort decile fent_morph u1 u2 * * The data set is now arranged so that, within * each decile, we have the morphine patients followed * by the fentanyl patients. Within the morphine/fentanyl * groups the patients are arranged randomly. * * Now select the correct number of morphine patients * within each decile group. * by decile fent_morph : replace select=1 if _n<=nf[1] & fent_morph==0 table select if decile==1 & fent_morph ==0 * * Lets see if things make sense. * tab decile fent_morph, missing tab decile select, missing keep if select==1 histogram logodds, percent by(, caption(after randomly selecting one morpine patient for each fentanyl patient within deciles)) by(fent_morph) xlabel(-2(.5)0) qui graph export graph1.png, replace // ![ ](./graph1.png) histogram logodds, bin(10) percent by(, caption(binned by decile)) by(fent_morph) xlabel(-2(.5)0) qui graph export graph2.png, replace // ![ ](./graph2.png) qui log c markdoc descriptive, replace export(pdf)