cd "/Users/tiff/Box Documents/TICR/Biostat 212-TA/Mini Lecture/" version 13 set more off capture log close log using "MiniLecture.smcl", replace ** How to Create Bar Graphs With Error Bars ** use "/Users/tiff/Box Documents/TICR/Biostat 212-TA/Mini Lecture/NF1 QOL Dataset Demo.dta", clear **This dataset looks at patients with NF1 and plexiform neurofibromas randomized to receive either placebo or a targeted drug. ** Quality of Life outcome measures utilize an NF1-specific QOL tool using scores from three sections: ** Physical Score= questions regarding pain, weakness, fatigue ** Emotional Score= questions regarding depression, anger, shame (patients often disfigured) ** Cognitive Score= patients with NF1 have varying degrees of cognitive deficits **Look at data, may be nice to evaluate data in treatment groups and by disease severity** list **Use the collapse command to make the mean and standard deviation of Physical Score by Treatment and Grade** collapse (mean) meanphysical= Physical (sd) sdphysical=Physical (count) n=Physical, by(Treatment Grade) **Generate the upper and lower values of the 95% CI assuming a normal distribution** generate hiphysical = meanphysical + 1.96*(sdphysical / sqrt(n)) generate lowphysical = meanphysical - 1.96*(sdphysical / sqrt(n)) **Use graph bar command to create skeleton bar graph without error bars** graph bar meanphysical, over(Treatment) over(Grade) **We can make the graph look a bit prettier by adding the asyvars command** graph bar meanphysical, over(Treatment) over(Grade) asyvars **Use twoway command to generate graph with error bars** graph twoway (bar meanphysical Treatment) (rcap hiphysical lowphysical Treatment), by(Grade) **Use twoway bar command to make a graph that resembles the graph bar command and then combine that with error bars **First, we need to make a variable GradeTreatment that will be a single variable that combines the Grade and Treatment information. **Note how sesrace has a gap between the levels of ses (at 3 and 6)** generate GradeTreatment = Treatment if Grade == 1 replace GradeTreatment = Treatment+3 if Grade == 2 replace GradeTreatment = Treatment+6 if Grade == 3 **Sort and Separate into thirds by Grade 1,2,3** sort GradeTreatment list GradeTreatment Grade Treatment, sepby(Grade) **Use twoway bar command with new combination variable** twoway (bar meanphysical GradeTreatment) **Overlay the error bars by overlaying a rcap graph "range plot with capped spikes"** twoway (bar meanphysical GradeTreatment) (rcap hiphysical lowphysical GradeTreatment) **Make it pretty: add titles, fix axes and legend** twoway (bar meanphysical GradeTreatment if Treatment==0) /// (bar meanphysical GradeTreatment if Treatment==1) /// (rcap hiphysical lowphysical GradeTreatment), /// legend(row(1) order(1 "Placebo" 2 "Drug") ) /// xlabel( 0.5 "Severe" 3.5 "Intermediate" 6.5 "Mild", noticks) /// xtitle("Disease Severity Grade") ytitle("Physical QOL Score") // save "NF1 QOL Dataset Demo Bar Graph.dta", replace log close set more on