2 Graphics 3 h Hein Stigum Presentation data

  • Slides: 36
Download presentation
2 Graphics 3 h Hein Stigum Presentation, data and programs at: https: //www. med.

2 Graphics 3 h Hein Stigum Presentation, data and programs at: https: //www. med. uio. no/helsam/forskning/aktuelt/arran gementer/andre/stata-course-uio. html

PLOT TYPES Oct-20 H. S. 2

PLOT TYPES Oct-20 H. S. 2

Plot types • Twoway – Scatter, line – Density, line-fit, fractional polynomial fit –

Plot types • Twoway – Scatter, line – Density, line-fit, fractional polynomial fit – Range cap, function • Bar, dot, box • Pie • Regression diagnostic plots Oct-20 H. S. 3

Plottypes May overlay twoway plots Working plots Presentation plots Oct-20 H. S. 4

Plottypes May overlay twoway plots Working plots Presentation plots Oct-20 H. S. 4

Making plots • Making plots (2 ways) – Menu – Syntax • Editing plots,

Making plots • Making plots (2 ways) – Menu – Syntax • Editing plots, Graph Editor – In a graph: File>Start Graph Editor Read in the data Demonstrate a scatterplot from the menu Add fit curve Change marker type and color Oct-20 H. S. 5

Plots of individual data TWOWAY PLOTS Oct-20 H. S. 6

Plots of individual data TWOWAY PLOTS Oct-20 H. S. 6

Twoway density • Syntax – graph twoway (plot 1, opts) (plot 2, opts), opts

Twoway density • Syntax – graph twoway (plot 1, opts) (plot 2, opts), opts • One plot – kdensity bw black, solid line Oct-20 H. S. 7

Twoway options, lines • Syntax – graph twoway (plot 1, opts) (plot 2, opts),

Twoway options, lines • Syntax – graph twoway (plot 1, opts) (plot 2, opts), opts • Options – lcolor(red) – lpattern(“. #-”) – lwidth(*2) Oct-20 line color line pattern line width *2 H. S. 8

Density overlaid tw (kdensity bw if sex==0, lcolor(blue) lpattern(solid)) (kdensity bw if sex==1, lcolor(red

Density overlaid tw (kdensity bw if sex==0, lcolor(blue) lpattern(solid)) (kdensity bw if sex==1, lcolor(red ) lpattern("-#") ) /// legend label 1 Oct-20 legend label 2 H. S. 9

Twoway options, legend • Syntax – graph twoway (plot 1, opts) (plot 2, opts),

Twoway options, legend • Syntax – graph twoway (plot 1, opts) (plot 2, opts), opts • Options – legend( order(1 2) label(1 “text”) label(2 “text”) ring(0) pos(2) col(1) ) – legend(off) Oct-20 label for plot 1 and 2 legend label plot 1 legend label plot 2 legend inside plot legend at 2 o’clock position legends in 1 column no legend H. S. 10

Legend tw…, /// legend( label(1 ”Boys, N=283”) label(2 ”Girls, N=270”) ring(0) pos(11) col(1) )

Legend tw…, /// legend( label(1 ”Boys, N=283”) label(2 ”Girls, N=270”) ring(0) pos(11) col(1) ) Oct-20 H. S. 11

Twoway scatter + fit • Syntax – twoway || plot 1, opts || plot

Twoway scatter + fit • Syntax – twoway || plot 1, opts || plot 2, opts ||, opts • Examples – scatter y x – fpfitci y x – lfit yx Oct-20 scatter smoother (fractional polynomial) with CI line fit H. S. 12

tw Scatter + smooth + fitline (scatter bw gest) (fpfitci bw gest, clcol(black)) (lfit

tw Scatter + smooth + fitline (scatter bw gest) (fpfitci bw gest, clcol(black)) (lfit bw gest, lcol(red )) , legend(off) scale(1. 3) Oct-20 /// /// H. S. /* scatter */ /* smoother with CI */ /* fitline */ 13

Titles scatter weight gest, title("title") subtitle("subtitle") xtitle("xtitle") ytitle("ytitle") note("note") /// round, blue marker symbols

Titles scatter weight gest, title("title") subtitle("subtitle") xtitle("xtitle") ytitle("ytitle") note("note") /// round, blue marker symbols Oct-20 H. S. 14

Twoway scatter options, markers ++ • Syntax – twoway || plot 1, opts ||

Twoway scatter options, markers ++ • Syntax – twoway || plot 1, opts || plot 2, opts ||, opts • Options – msymbol(S) – mcolor(red) – msize(*0. 5) – mlabel(id) square marker symbols (S, s, Sh) marker color red marker size 0. 5*larger marker label =variable id – xline(24) – scale(1. 5) line at x=24 all elements 1. 5*larger Oct-20 H. S. 15

Scatter with outliers marked regress bw gest i. educ sex dfbeta(gest), stub(Dbeta) tw (scatter

Scatter with outliers marked regress bw gest i. educ sex dfbeta(gest), stub(Dbeta) tw (scatter Dbeta 1 id if abs(Dbeta 1)>0. 2 , legend(off) scale(1. 3) , msize(*0. 5)) , mcol(red) mlab(id)) /// xscale xlabel Oct-20 H. S. 16

Twoway options, axis options • Syntax – graph twoway (plot 1, opts) (plot 2,

Twoway options, axis options • Syntax – graph twoway (plot 1, opts) (plot 2, opts), opts • Options – – – – Oct-20 xlabel(10(5)100) from 10 -100 in steps of 5 xlabel(#8) appr. 8 “nice” values xlabel(minmax 64) min, 64, max ylabel(1 2 3, valuelabel) labels of the y-variable (cat) yscale(range(0 6)) range from 0 to 6 yscale(log) log-scale xtick(…) ticks and grid H. S. 17

Scatter and scatteri tabstat bw, stat(mean) by(educ) tw ||scatter educ bw, ylabel(1 2 3,

Scatter and scatteri tabstat bw, stat(mean) by(educ) tw ||scatter educ bw, ylabel(1 2 3, valuelabel angle(0)) ||scatteri 1 3254 2 3416 3 3533 , msymbol(O) msize(*2) mcolor(red) ||, legend(off) xtitle("Birth weight") xline(3432) scale(1. 3) Immediate arguments: scatteri pcarrowi Oct-20 H. S. 18 ///

Density with mean sum bw tw (kdensity bw) , yscale(off) scale(1. 5) xtitle("Birth weight")

Density with mean sum bw tw (kdensity bw) , yscale(off) scale(1. 5) xtitle("Birth weight") xlabel(minmax 3432, format(%5. 0 f)) xtick(3432, tposition(cross) tlcol(red) tlwidth(*3) ) Oct-20 H. S. /// /// 19

As inspiration: density with box info Oct-20 H. S. 20

As inspiration: density with box info Oct-20 H. S. 20

Twoway, + text • Syntax – twoway (plot 1, opts) (plot 2, opts), opts

Twoway, + text • Syntax – twoway (plot 1, opts) (plot 2, opts), opts • Options – text(y x “text”, t_ops) • • • text at (y, x) place(0) size(*2) color(red) box bcolor(white) centered text size*2 text color box around text background and border color • Alternative – scatter y x, msymb(i) mlab(lab) Oct-20 H. S. invisible symbol, text lab 21

Function plots with text function y=f(x) , range(0 5) tw (function y=normalden(x, 0, 1),

Function plots with text function y=f(x) , range(0 5) tw (function y=normalden(x, 0, 1), range(-4 4) col(red )) /// (function y=normalden(x, 2, 2), range(-4 8) col(blue)) /// , text(0. 3 0 "Normal(0, 1)" 0. 15 2 "Normal(2, 2)” box bcol(white)) Oct-20 H. S. 22

Marking lines using scatter see syntax … scatter y x, msymb(i) mlab(lab) Oct-20 H.

Marking lines using scatter see syntax … scatter y x, msymb(i) mlab(lab) Oct-20 H. S. 23

Plotting Confidence Intervals twoway (connect weight year, col(blue)) (rcap ll ul year , col(blue))

Plotting Confidence Intervals twoway (connect weight year, col(blue)) (rcap ll ul year , col(blue)) /// means /// 95% CI input year weight ll ul 2001 60 50 70 2002 75 65 85 2003 75 65 85 end Oct-20 H. S. 24

Coefficients with CI from regression, extra regress bw gest boy educ mat R=r(table)' svmat

Coefficients with CI from regression, extra regress bw gest boy educ mat R=r(table)' svmat R gen y=-_n if R 1<. // linear regression // result matrix transposed // save matrix as data: R 1, R 2, . . . // y values for plot twoway (scatter y R 1 in 1/3, col(blue)) (rcap R 5 R 6 y in 1/3, col(blue) horizontal) , ylabel(-1 "gest" -2 “boy" -3 "educ", angle(0)) leg(off) ysize(1) scale(4) Oct-20 H. S. /// coefficients /// 95% CI /// y-labels 25

Plots of summary measures (mean, fractiles, …) TWOWAY PLOTS Oct-20 H. S. 26

Plots of summary measures (mean, fractiles, …) TWOWAY PLOTS Oct-20 H. S. 26

Bar graphs, and alternatives N=10 000, 4 variables Nor, Swe and Den: Psycho measures

Bar graphs, and alternatives N=10 000, 4 variables Nor, Swe and Den: Psycho measures on some continuous scale Year: 2001 to 2004 graph bar nor swe den, over(year) plotting means Alternative twoway: individual data aggregate data to means Oct-20 H. S. 27

Aggregating data (collapse) • Aggregate collapse (mean) nor swe den, by(year) • Data after

Aggregating data (collapse) • Aggregate collapse (mean) nor swe den, by(year) • Data after aggregate • Typical use preserve collapse (mean) nor swe den, by(year) … restore Oct-20 H. S. run together! 28

Comparing means/prop. better collapse (mean) nor swe den, by(year) tw (connect nor year, col(blue

Comparing means/prop. better collapse (mean) nor swe den, by(year) tw (connect nor year, col(blue ) msize(*2) ) (connect swe year, col(red ) msize(*2) ) (connect den year, col(green) msize(*2) ) /* aggregate */ /// /// , legend(off) /// text(3. 7 2001. 3 "Norway" 2. 5 2001. 3 "Sweden" 1. 3 2001. 3 "Denmark", box bcol(white)) Oct-20 H. S. 29

Blood pressure by time and sex, 3 variables collapse (mean) meanbp=bp, by(sex time) twoway

Blood pressure by time and sex, 3 variables collapse (mean) meanbp=bp, by(sex time) twoway (scatter meanbp time, connect(ascending)) , yline(140) text(120 3. 5 "Males") text(108 5 "Females") legend(off) Oct-20 H. S. 30

Binary with rug and smooth, lbw gest gen yy=lbw-0. 02 /* rug data */

Binary with rug and smooth, lbw gest gen yy=lbw-0. 02 /* rug data */ tw (rspike yy lbw gest, col(red)) (fpfit lbw gest, lw(*2) lcol(blue)) /// , legend(off) scale(1. 2) ytitle("Proportion with low birth weight") /// xscale(range(230. )) text(1. 02 230 "Low bw" 0 230 "Normal bw", place(3) col(red)) Oct-20 H. S. 31

Prediction from linear regression regress bw c. gest##sex i. educ // regression model with

Prediction from linear regression regress bw c. gest##sex i. educ // regression model with interaction term margins sex, at(gest=(30(1)40)) marginsplot, scale(1. 2) // predicted birth weight by gest and sex no estimated interaction Oct-20 H. S. 32

Prediction from logistic regression logistic lbw c. gest##sex i. educ // regression model with

Prediction from logistic regression logistic lbw c. gest##sex i. educ // regression model with interaction term margins sex, at(gest=(30(1)40)) marginsplot, scale(1. 5) // probability of low birth weight by gest and sex Oct-20 H. S. 33

Graphics in journals set scheme s 2 color set scheme s 1 mono Oct-20

Graphics in journals set scheme s 2 color set scheme s 1 mono Oct-20 /* default */ /* monochrome */ H. S. 34

Keep plots during session • Set “tabbed” graphics set autotabgraphs on, permanently • Give

Keep plots during session • Set “tabbed” graphics set autotabgraphs on, permanently • Give each plot a name twoway …, name(“scatter”, replace) Oct-20 H. S. 35

Figure and table Crude and adjusted coefficients with 95% CI Oct-20 H. S. 36

Figure and table Crude and adjusted coefficients with 95% CI Oct-20 H. S. 36