Download presentation
Presentation is loading. Please wait.
Published byPriscilla Janis Holt Modified over 9 years ago
1
Making Tables and Figures with Stata Biostatistics 212 Lecture 6
2
Housekeeping List of confusing Lab 5 terms –Confounders –Effect modifiers –Moderators –Mediators –Interaction –Adjust for –Control for
3
Housekeeping List of confusing Lab 5 terms –Confounders –Adjust for = Control for –Effect modification = Interaction Effect modifiers = Modifier = “Interaction factor” –Mediators
4
Housekeeping Does A cause B? –“A is associated with B before adjusting for C, but not afterwards” –“A is associated with B because A causes C and C causes B” A B C ABC confounder mediator
5
Housekeeping Does A cause B? –“A is associated with B, and much more so when C is present” A B C Effect modifier
6
106585 1862165 Binge + - +-+- 89374 118801 CAC Binge + - +-+- 17211 681364 CAC Binge + - +-+- In menIn women (34%)(14%) (15%)(7%) RR = 1.57 (0.94-2.62)RR = 1.50 (1.16-1.93) RR = 1.94 (1.55-2.42) RRadj = 1.51 (1.21-1.89)
7
In men In women RR = 1.57 (0.94-2.62) RR = 1.50 (1.16-1.93) Crude RR = 1.94 (1.55-2.42) RRadj = 1.51 (1.21-1.89) Compare these to look for CONFOUNDING
8
In men In women RR = 1.57 (0.94-2.62) RR = 1.50 (1.16-1.93) Crude RR = 1.94 (1.55-2.42) RRadj = 1.51 (1.21-1.89) Compare these to look for EFFECT MODIFICATION (none here)
9
In htn In no htn RR = 1.69 (0.97-2.95) RR = 3.39 (1.78-6.45) Crude RR = 2.51 (1.69-3.73) RRadj = 2.38 (1.57-3.62) Compare these to look for EFFECT MODIFICATION (maybe here?)
10
Housekeeping Other Lab 5 issues? Lab 4 issues? Final Project questions? –Remember handouts from Lecture 1 merge, egen, dates, loops, reshape
11
Today Organizing your Stata files Making a table Making a figure
12
Today Organizing your Stata files Making a table Lab practice = Final Project Making a figure Lab practice = Lab 6
13
Organizing your Stata files Pitfalls –Proliferating dataset –Can’t remember what you did –Can’t remember why you did it –Can’t easily redo with new data
14
Organizing your Stata files My system (it’s not perfect) 1)Import data into Stata a)Using a Stata command (e.g., insheet or import ) within a do file b)Using other method (e.g., StatTransfer?) outside a do file, then SAVE the “raw” Stata file immediately 2) Write a do file that “cleans” your data, and saves it as a new clean dataset 3) Write do files for each component of your analysis
15
Raw data.xls My organizational scheme
16
Raw data.xls Raw data.dta Import My organizational scheme
17
Raw data.xls Raw data.dta In Stata My organizational scheme Import
18
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.log My organizational scheme Import
19
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.logTable 1.do Table 1.log My organizational scheme Import
20
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.logTable 1.do Table 1.log Table 1.xls Cut and paste My organizational scheme Import
21
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.logTable 1.do Table 1.log Table 1.xls Cut and paste My organizational scheme Table 1.doc Cut and paste Import
22
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.logTable 1.do Table 2.do Table 1.log Table 2.log Table 1.xls Table 2.xls Cut and paste My organizational scheme Table 1.doc Table 2.doc Cut and paste Import
23
Organizing your Stata files You will end up with: –1 or 2 Stata datasets Data, from Excel.dta (only if you import outside your do file) Data.dta –1 do file used for cleaning Data prep.do –1 do file to create each Table and Figure Table 1.do, Figure 1.do, Text data.do, etc –Matching log files (with the same names) for each do file Data prep.log, Table 1.log, Figure 2.log, Text data.log, etc
24
Organizing your Stata files Put them all in one folder called, “Stata files”, sort by file type. Example
26
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.logTable 1.do Table 2.do Table 1.log Table 2.log Table 1.xls Table 2.xls Cut and paste Any questions? Table 1.doc Table 2.doc Cut and paste Import
27
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.logTable 1.do Table 2.do Table 1.log Table 2.log Table 1.xls Table 2.xls Cut and paste My organizational scheme Table 1.doc Table 2.doc Cut and paste Lecture 3 Import
28
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.logTable 1.do Table 2.do Table 1.log Table 2.log Table 1.xls Table 2.xls Cut and paste My organizational scheme Table 1.doc Table 2.doc Cut and paste Lecture 3Lecture 5 Import
29
Raw data.xls Raw data.dta In Stata Clean data.dta Data prep.doData prep.logTable 1.do Table 2.do Table 1.log Table 2.log Table 1.xls Table 2.xls Cut and paste My organizational scheme Table 1.doc Table 2.doc Cut and paste Lecture 3Lecture 5 Lecture 7 Import
30
Tables Two main purposes –Present the facts in a compact format –Provide side-by-side comparisons Six main components: –Data –Title, row heading, column headings –Row names –Footnotes Browner, W. Publishing and Presenting Clinical Research
31
Steps to making a Table Decide what the Table will be about Make the dummy table –Do this FIRST!! Write a do file that will produce each number you need Copy and paste the data in (if possible) Format so it looks nice
32
Steps to making a Table Deciding what the Table will be about –Sketch it out on paper Title, column headings, etc
33
Steps to making a Table Make the dummy table –Excel or Word –Makes you specify what you actually want! Row headings Decide on category cut-offs, labels Decide on reference categories for regression, etc Footnote liberally –Leave data cells blank
34
Steps to making a Table Write a do file that will produce each number you need –Iterative process, as you know
35
Steps to making a Table Copy and Paste the data in –Copy and Paste each number, or –“Copy Table” (under the “Edit” menu) http://www.stata.com/support/faqs/data/copytable.html –Minimize manual retyping, rounding –Use Excel to calculate and round for you
36
Steps to making a Table Format it so it looks nice –Standard, plain style – usually: Horizontal lines, not vertical Double-spaced Footnotes - *, †, ‡, §, ║, ¶ (or a,b,c,d,…) –Create a template for yourself
37
Word vs. Excel for Tables Stata Word –Fewer steps, fewer files –But… Can’t cut and paste full tables Doesn’t do any calculations for you Formatting can become “corrupted”
38
Word vs. Excel for Tables Stata Excel Word –Can cut and paste values or whole tables –Set rounding, do calculations easily –Formatting easier? –Copy and Paste into Word (extra step)
39
Demo Table 1 for “Moderate drinking and coronary calcium in young adults: The CARDIA Study” –Basic content –Sketch –Mock-up in Excel –Generate numbers in Stata –Transfer numbers to Excel –Copy and paste into Word
40
Figures When use a figure? Making a figure with Excel Making a figure with Stata
41
When use a figure? When a graphical display of information more effectively conveys the intended message than words. “A picture is worth a thousand words”
42
Figures “A picture is worth a thousand words” How many words is this picture worth?
43
Figures “A picture is worth a thousand words” How many words is this picture worth? 48% of CARDIA participants consume alcohol moderately. Worth = 7 words
44
Figures “A picture is worth a thousand words” How many words is this picture worth?
45
Figures “A picture is worth a thousand words” How many words is this picture worth? WhiteBlack Drinks/dayn=1935n=1727 040%57% 0.1-0.939%26% 1-1.913%9% 2+8%8% Worth = 1 small table? (and avoid pie charts in general…)
46
Figures “A picture is worth a thousand words” How many words is this picture worth?
47
Figures “A picture is worth a thousand words” How many words is this picture worth? Proportion with CAC AbstainerMod drinker Black women.047.036 White women.054.049 Black men.068.132 White men.180.167 Can you see the interaction in this table without a figure? (Figures are good for illustrating interactions)
48
Figures “A picture is worth a thousand words” How many words is this picture worth?
49
Figures “A picture is worth a thousand words” How many words is this picture worth? Worth = 968 data points? Nice to show actual data points along with main effect, if possible!
50
Making a figure With Excel –First make a TABLE in Excel! Use Stata to generate numbers for the table –Create a figure from the Table using Excel tools With Stata –Use Stata commands to create the figure directly
51
Steps in making an Excel figure Sketch your figure Make a dummy TABLE Write a.do file to fill in the table Copy and paste from the log file or the results window into the Table Use the Chart Wizard to create the Figure Format, format, format until it looks nice
52
Steps in making a Stata figure Sketch your figure Make a dummy TABLE Write a.do file Copy and paste from the log file or the results window into the Table Use the Chart Wizard to create the Figure Format, format, format until it looks nice
53
Steps in making an Excel figure Sketch the Figure, with title –Try several versions –Point should be clear at a glance –Requires some artistic vision…
54
Steps in making an Excel figure Make a dummy TABLE –Contains the data for the figure –Doesn’t have to look nice
55
Steps in making an Excel figure Fill in the Excel table –Write a.do file to generate the data –Copy and paste from log file into the Table
56
Steps in making an Excel figure Use the Chart Wizard to create the Figure
57
Excel examples Figure 2 from lipid paper
58
Pay attention to… Formatting –Make it look nice and professional, but not gaudy Black and white, usually –The time-consuming part of making a figure is usually related to formatting.
59
Pay attention to… Labeling –Your figure should be understandable by itself, without the rest of the manuscript –All axes should be labeled. –Include important p-values
60
Pay attention to… The Figure Legend –Title, explanations, extra p-values, etc –Separate section in manuscript or at bottom of page – depends on journal
61
Stata vs. Excel for Figures Excel –Flexible and intuitive point-and-click figures Easy to create and modify Flexible, more options, error bars, adjusted estimates, good for bar graphs, etc –But… Requires an extra step – copy/pasting to Excel Harder to reproduce Much harder to do scatter plots
62
Stata vs. Excel for Figures Stata –Can create very customizable figures using 1 complex Stata command Easy to recreate – simple do file No error Scatter plots are MUCH easier with Stata –But… Harder to create the first time? - no point and click A little less flexible? Difficult to format: Graphic Editor helps address this
63
Stata vs. Excel for Figures Easiest tool Bar/Line* figuresExcel Confidence intervalsExcel Scatter plotsStata Box plotsStata * Where x-axis is categorical
64
Steps in making a Stata figure Sketch the figure Make a dummy TABLE Write a.do file Copy and paste from the log file or the results window into the Table Use the Chart Wizard to create the Figure Format, format, format until it looks nice
65
Steps in making a Stata figure Sketch the figure
66
Steps in making a Stata figure Sketch the figure Write a.do file –Compose the command using the dialog box Get the syntax down, multiple iterations –Transfer to your do file and edit
67
Steps in making a Stata figure Sketch the figure Write a.do file –Compose the command using the dialog box Get the syntax down, multiple iterations –Transfer to your do file and edit Iterate until it’s perfect –Format/format/format! –May need additional Stata commands for calculating p-values, figure legend, etc
68
Stata commands intro –Pie charts graph pie, over(catvar) –Bar graphs graph bar (mean) yvar1 yvar2, over(catvar1) over(catvar2) asyvars –Box plots graph box contvar1 contvar2, over(catvar1) over(catvar2) –Scatter plots twoway (graphtype yvar xvar) (graphtype yvar xvar) –Graphtypes: scatter, line, connect, lowess, lfit, qfit, etc
69
Stata Demo Scatter plots: bmi vs. lipids –Iterative process of adding commands to do file –Cutting and pasting with substitution –Lowess smoother and linear fit lines –Post-graph editing
70
Final Checklist for Figures 1.Does the figure make its point clearly? 2.Are the axes, line, bars, and points labeled? Are the scales correct? Sig figs appropriate? 3.Does each figure have a legend, not a title? 4.Are the figures numbered, and do they appear in the text in that order? 5.Does the text complement the information in the figures?
71
Key points It’s worth putting thought into your file organization Tables: –First sketch it on paper –Next make your dummy table –Only then write your do file Figures: –Make it with Stata if you can –Use dialog boxes to get the correct syntax, paste into your do file –Make bar charts with Excel Document everything you do!
72
Today’s Lab You will create a pie chart, a box plot, and a scatter plot using stata. The focus will be on bringing the figure to publication grade.
73
twoway (scatter dfev1 cumpy10 if menthol1==1, msymbol(plus) msize(small) mcolor(black)) /// (scatter dfev1 cumpy10 if menthol1==0, msymbol(circle_hollow)) /// (line m cumpy10 if menthol1==1, sort clcolor(black) clpat(dash) clwidth(thick)) /// (line nm cumpy10 if menthol1==0, sort clcolor(black) clpat(solid) clwidth(thick)) ///, ytitle(Change in FEV1 (milliliters), size(large)) yscale(titlegap(5)) /// xtitle(Pack-years of exposure to tobacco, size(large)) /// xscale(titlegap(3)) /// legend(order(1 "Menthol smokers" 2 "Non-menthol smokers" 3 "Menthol regression" /// 4 "Non-menthol regression")) /// scheme(s1mono) /// graphregion(fcolor(none) lcolor(none) ifcolor(none) ilcolor(none)) /// plotregion(fcolor(none) lcolor(none) ifcolor(none) ilcolor(none))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.