01.05.2013 Views

Introduction to Stata 8

Introduction to Stata 8

Introduction to Stata 8

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Intro'()tion <strong>to</strong> +tata -<br />

+.en' 0((1<br />

Department o5 Epi'emio1og8 an' +o)ia1 9e'i)ine: Uni.er(


Content<<br />

DE In


Pre5a)e<br />

+tata i< a ing< a1moere t>e 'o)(mentation<br />

o5 I>at 8o( 'i' ] in)1('ing t>e ome page< IIIEe grap>i)< (inge


Potation in t>i< ?ooY1et<br />

+tata )omman'< are oIn 1iYe t>ie o(tp(t eQamp1e< 8o( Ii11 o5ten e )omman'< pre)e'e' ?8 a perio'W<br />

. tabulate agegr sex , chi2<br />

X>i< i< >oI )omman'< 1ooY in o(tp(t: ?(t 8o( o(1' not t8pe t>e perio' 8o(ren<br />

entering a )omman'E<br />

[ptiona1 part< o5 )omman'< are oIn in 1ig>t t8pe5a)e an' en)1oat )ae8 are oIn in<br />

t>e (e eQamp1e< I>en opening dusec or ro(g>o(t t>e teQt I (


DE IntZ e 'eate.er 8o(r perU: 'ataU an' 'oU5i1e< in 5o1'er< organia< at 1ea program 5i1e<<br />

U \o( )an


X>e pro5i1eE'o 5i1e gG+Hh AK<br />

I5 8o( p(t a profile.do 5i1e in t>e c:\ado\personal 5o1'er @ t>e )omman'< Ii11 ?e<br />

eQe)(te' a(<strong>to</strong>mati)a118 ea)> time 8o( open +tataE Hrite 8o(r profile.do (e replace option<br />

o.erIrite< o1' o(tp(tE X>e 5o1'er c:\tmp m(e append option Yeep< t>e<br />

)omman' 1og 5rom pre.io(< e Reoig>1ig>t error mei< p1a)e i<


@E Hin'oI< in +tata gG+Hh @<br />

I +(ggee main Iin'oI< a< oIn ?e1oIE [n)e 8o( ma'e 8o(r )>oi)e


OieIer Iin'oI gG+Hh G<br />

X>e main (i< Iin'oI i< .ieIing >e1p 5i1e< dat: oI <strong>to</strong> (e edit )omman' <strong>to</strong> maYe )orre)tion< 'ire)t18<br />

in t>e 'ata Iin'oIE Vor reae DoU?(t<strong>to</strong>nE I pre5er PoteXa? Sig>t <strong>to</strong> t>e 'oU5i1e e'i<strong>to</strong>rZ


GE +(ggee re)ommen'ation< in t>i< at i< re)ommen'e' in t>e gG+Hh<br />

man(a1Z I tr8 <strong>to</strong> eQp1ain I>8: I>en m8 re)ommen'ation< 'i55erE<br />

GEDE Ie )omman'< in a 'oU5i1e ?e5ore eQe)(tionE<br />

X>e 'ia1og< dmen( e e 'oU5i1eE<br />

! X>e 'oU5i1e e )(rrent 5i1e<br />

?8 )1i)Ying t>e 'oU?(t<strong>to</strong>n dn(m?er tIo 5rom t>e rig>tcE X>e 'iat t>e name o5 8o(r 'oU5i1e i< not re51e)te' in t>e o(tp(tE I re)ommen' ie 'oU5i1e: 5or rea


GE@E _an'1ing o(tp(t<br />

+tataf< o(tp(t 5a)i1itie< are 1ean optima1E In t>i< oI 8o( )an (e t>ir'U<br />

part8 program PoteXa? <strong>to</strong> >an'1e o(tp(t 5or e'iting an' printingE<br />

X>e Ree Re <strong>to</strong>o in51eQi?1eE X>e +9CSU<br />

5ormatte' o(tp(t i< a miQe' ?1ee gG+Hh man(a1 (e nt )omman' gi.e< 8o( rapi' a))ee 5i1e: I>i)> i< not o.erIritten neQt time +tata i< opene'E<br />

\o( ma8 (e tri)Y )orre)t18: Hin'oI< m(


BE Getting >e1p gG+Hh B: DM: @AZ gUh @: -<br />

BEDE X>e man(a1<<br />

+ee re)ommen'ation< in Appen'iQ DE<br />

BE@E [n1ine >e1p gG+Hh BZ gUh -: G@<br />

[n1ine >e1p i< ?ee OieIer Iin'oI de +tata )omman'<br />

1ineE keep 8o(r +tata (p'ate' ] t>en 8o(r on1ine >e1p i< (p'ate' <strong>to</strong>oE<br />

help an' whelp<br />

I5 8o( YnoI t>e )omman' name deEgE tabulatec e >e1p 5i1e dta?(1ateE>1pc ?8W<br />

help tabulate 5rom t>e OieIer )omman' 1ineW or<br />

whelp tabulate 5rom +tatal< )omman' 1ine Iin'oI<br />

X>e >e1p 5i1e i< 'ie net 5or in5ormation on goo'ne


JE +tata 5i1e t8pe< an' name< gUh DBEN<br />

E'ta 5i1ei)< man(a1c<br />

+tata grap>< )an ?e


NE Oaria?1e<<br />

A +tata 'ata ere i< one Iit> 5i.e o?e n(m?er<<br />

0-9 an' _ d(n'er


P(meri) 5ormat< gUh DJEJED<br />

X>e 'e5a(1t i< Genera1 5ormat: prein' t>e mi


KE Comman'


In )omman'< t>at >a.e a 'epen'ent .aria?1e: it i< t>e 5ire varlistW<br />

oneway bmi sex bmi i< t>e 'epen'ent .aria?1e<br />

regression bmi sex age bmi i< t>e 'epen'ent .aria?1e<br />

scatter weight height t i< 8UaQi<<br />

tabulate expos case X>e 5ir


y an' bysort pre5iQ gUh DBEJ<br />

9aYe< a )omman' 'i *<br />

! teQt i< ?ooY1etc<br />

X>e p(rpoan<br />

-A )>ara)tere pro?1em i<


-E Getting 'ata in<strong>to</strong> +tata gUh @BZ gG+Hh K<br />

[n eQ)>ange o5 'ata Iit> ot>er programer in5ormation in appen'iQ @E<br />

Enter 'ata a< )omman'< or in a 'oU5i1e gRh input<br />

Very small data sets. De5ine t>e .aria?1e< Iit> t>e input )omman' an' enter t>e .a1(e endE It )an ?e 'one intera)ti.e18 5rom t>e )omman' 1ine or in a 'oU5i1eE +ee<br />

more eQamp1e< in eQamp1e


Rea'ing A+CII 'ata gUh @B<br />

Rea'ing ta?U or )ommaUara)ter: >ere 'ie 5o11oIing eQamp1e<<br />

t>e .a1(e o5 type in o?o(t .aria?1e name< in roI D t>e )omman' ie )omman' i<<br />

infile id type sold price using c:\dokumenter\p1\a.txt<br />

infile 'oe< not ae<br />

5o11oIing 'ata are t>e e 'ata a?o.eW<br />

1 2 47 51.23 2 . 793 199.70<br />

Rea'ing 5iQe' 5ormat 'ata gRh infixZ gRh infile d5iQe' 5ormatc<br />

In 5iQe' 5ormat 'ata t>e in5ormation on ea)> .aria?1e i< 'etermine' ?8 t>e poe 1ineE<br />

X>e ?1anY type in o?


ME Do)(mentation )omman'< gG+Hh -<br />

+tata 'oe< not nee' 'o)(mentation )omman'e<br />

o(tp(t ?e)ome< more 1egi?1e: an' t>e rien interpreting t>e o(tp(t i< re'()e'E<br />

Data e )reation 'ate: <strong>to</strong> enat 8o( ana18e moi< i< a tIoUe 1a?e1 d'o(?1e ^(ote< aro(n' teQt Iit> em?e''e' ?1anYe .aria?1e sexW<br />

label values sex sexlbl<br />

Ue e .a1(e 1a?e1: <strong>to</strong> a.oi' mie 1a?e1i< >appen< a(<strong>to</strong>mati)a118 Iit>W<br />

numlabel _all , add<br />

D-


+ee 1a?e1 'e5inition<<br />

+ee t>e .a1(e 1a?e1 'e5inition< ?8W<br />

label list or<br />

labelbook<br />

+ee t>e .aria?1e 1a?e1 'e5inition< ?8W<br />

describe<br />

+ee a 5(11 )o'e?ooY ?8W<br />

codebook<br />

Pote< gRh notes<br />

\o( ma8 a'' note< <strong>to</strong> 8o(r 'ata


DAE 9o'i58ing 'ata<br />

Donft mie tit1e o5 t>i< e 1ae 'oU5i1e o(1' ?e f)1eanf: ieE not in)1('e )omman'< irre1e.ant <strong>to</strong> t>e mo'i5i)ation


generate; replace gRh generate<br />

Generate a neI .aria?1e ?8W<br />

generate bmi=weight/(height^2)<br />

I5 t>e target .aria?1e dbmic a1rea'8 eQie 'ata


ecode gRh recode<br />

C>ange< a .aria?1ef< .a1(ee neI .aria?1e ma8 ?e in)1('e' at on)eW<br />

recode age (55/max=4 "55+")(35/55=3 "35-54")(15/35=2 "15-34") ///<br />

(min/15=1 "-14") , generate(agegr)<br />

Very importantW X>e generate option )reate< a neI .aria?1e Iit> t>e re)o'e'<br />

in5ormationZ Iit>o(t generate t>e origina1 in5ormation in age Ii11 ?e 'ee CPR n(m?er<<br />

Iere


DAE@E +e1e)tion<<br />

+e1e)ting o?e 5ir


DAEBE +orting 'ata<br />

sort gRh sort: gRh gsort<br />

Xo


DAENE Com?ining 5i1e< gUh @J<br />

Appen'ing 5i1e< gRh append<br />

Xo )om?ine t>e in5ormation 5rom tIo 5i1e< Iit> t>e e e eQamp1e a?o.ec: an'<br />

t>e mat)>ing Ye8 m(a.e t>e 'ata ing Ye8 t>e<br />

.aria?1e name< o(1' ?e 'i55erentE `e1oI A an' ` e .aria?1e e inp(t<br />

5i1ee .aria?1e _merge I>i)> taYe< t>e .a1(e D i5 on18 'ata ere Iere tIo o?e ree in5ormation 5rom filb ae '(p1i)ate lbnr B Ia< an errorv Xo )>e)Y 5or '(p1i)ate i'f< ?e5ore merging:<br />


DAEKE Reaping 'ata<br />

collapse gRh collapse<br />

\o( Iant <strong>to</strong> )reate an aggregate' 'ata t>e )>ara)teri in'i.i'(a1: ?(t<br />

o5 gro(p< o5 in'i.i'(a1ara)teriFe p>8


DDE De eQamp1e< o5 o(tp(tE<br />

summarize gRh summarize<br />

summarize gi.e< an o.er.ieI o5 .aria?1ee 9in an'<br />

9aQ )o1(mne 'ie ni)e mo'i5i)ation summvl: a1e)Y t>e reer t>an .a1(e 1a?e1<<br />

are 'i


. tab1 rating type dta?1e 5or type not oInc<br />

-> tabulation of rating<br />

quality |<br />

rating | Freq. Percent Cum.<br />

--------------+-----------------------------------<br />

1. poor | 6 17.14 17.14<br />

2. acceptable | 9 25.71 42.86<br />

3. good | 16 45.71 88.57<br />

4. excellent | 4 11.43 100.00<br />

--------------+-----------------------------------<br />

Total | 35 100.00<br />

tab2 gRh tabulate<br />

tab2 Iit> tIo .aria?1e< gi.e a tIoUIa8 ta?1e d)roe )omman'W<br />

tab2 rating type nation<br />

gi.e< t>ree tIoUIa8 ta?1e o5 t>e )om?ination< o5 t>e .aria?1ee re<br />

DD .aria?1e< 8o( get JJ tIoUIa8 ta?1ee 1o)a1 ma)ro Q i< a e )omman'< generate DA )omman'


tabi gRh tabulate<br />

tabi i< an fimme'iatef )omman' dort


DDE@E Contin(o(< .aria?1e<<br />

oneway gRh oneway<br />

)ompare< mean< ?etIeen tIo or more gro(p< dana18 F<br />

------------------------------------------------------------------------<br />

Between groups 780.660217 3 260.220072 0.98 0.4162<br />

Within groups 8257.34954 31 266.366114<br />

------------------------------------------------------------------------<br />

Total 9038.00975 34 265.823816<br />

Bartlett's test for equal variances: chi2(3) = 2.3311 Prob>chi2 = 0.507<br />

X>e ta?1e: ?(t not t>e tean'1e< a 1ot o5 )omp1eQ e 'ie<br />


ttest gRh ttest<br />

XUte


D@E Regre


D@E@E Sogie xi: pre5iQ app1ie< a< 'e


DGE +(r.i.a1 ana18e 'ata e 'ata in memor8 <strong>to</strong> ?e e timeUo5UeQit .aria?1eW<br />

. // c:\dokumenter\proj1\gen.st.cancer1.do<br />

. use c:\dokumenter\proj1\cancer1.dta , clear<br />

.<br />

stset risktime , failure(died==1) id(lbnr)<br />

id: lbnr<br />

failure event: died == 1<br />

obs. time interval: (risktime[_n-1], risktime]<br />

exit on or before: failure<br />

-------------------------------------------------------------------------<br />

48 <strong>to</strong>tal obs.<br />

0 exclusions<br />

-------------------------------------------------------------------------<br />

48 obs. remaining, representing<br />

48 subjects<br />

31 failures in single failure-per-subject data<br />

62 <strong>to</strong>tal analysis time at risk, at risk from t = 0<br />

earliest observed entry t = 0<br />

last observed exit t = 3.25<br />

GB


. summarize<br />

Variable | Obs Mean Std. Dev. Min Max<br />

-------------+-----------------------------------------------------<br />

lbnr | 48 24.5 14 1 48<br />

.... |<br />

risktime | 48 1.291667 .8546908 .0833333 3.25<br />

died | 48 .6458333 .4833211 0 1<br />

_st | 48 1 0 1 1<br />

_d | 48 .6458333 .4833211 0 1<br />

_t | 48 1.291667 .8546908 .0833333 3.25<br />

_t0 | 48 0 0 0 0<br />

. save c:\dokumenter\proj1\st.cancer1.dta<br />

Vo(r neI .aria?1e< Iere )reate': an' t>e stfe' 'ata erIiW g+Xh sts graph<br />

sts graph , by(drug)<br />

Xo o?tain a )(m(1ati.e in)i'en)e dD]+c grap> 'ie inter.a1 AUAE@J at t>e 8UaQi


In)1('ing age in t>e ana18


poisson gRh poisson<br />

X>e stsplit.cancer2.dta 'ata


DBE Grap><<br />

DBEDE Intro'()tion<br />

X>e p(rpoi< e1p 8o( (n'eroi)ean [OK]Z t>i< gi.e< t>e opport(nit8 <strong>to</strong> mo'i58 8o(r )>oi)e< a5ter >a.ing<br />

1ooYe' at t>e rei< Ia< t>e eae p(rpoe 5irere i< 'i55erent 5rom t>e man(a1 i< i


DBE@E X>e ana<strong>to</strong>m8 o5 grap><<br />

Vig(re D oI< t>e moE X>e graph area i< t>e entire 5ig(re:<br />

in)1('ing e.er8t>ing: I>i1e t>e plot area i< t>e )entra1 part: 'e5ine' ?8 t>e aQee o.era11 )omman'< ii< i< t>e


H>en 1etting t>e 'ia1og generate a e grap> t8peZ scatter 'e5ine< a p1ot in t>e grap>E \o( )o(1' enter t>e<br />

e )omman' Iin'oI: ?(t +tata a1i< ort .ere .aria?1e 1ii)> )ome< 1a )omman'< ma8 >a.e optioner +tata )omman'< a )omma pre)e'e< t>e<br />

optione twoway grap> )omman'W<br />

twoway (scatter mpg weight) , title("74 car makes")<br />

P1ot a.e optione p1ot ara)tercW<br />

title"("74 car makes")<br />

X>e error meee entire grap> e ae p1ot area dt>e 8eQ aQi< ratioc ?8 t>e<br />

aspect() optionE Xo o?tain a


Xi)Ye QUaQi maaor inter.a1 in tIoE X>e 8UaQi< >a< a 1og e aQee 'e5a(1t i< >oriFonta1 gri'1ine< an'<br />

no .erti)a1 gri'1inear'18 a goo' i'ea in<br />

t>i< )aer t>an perio'e +tata )omman'W<br />

set dp comma<br />

P1otregion margin<br />

`8 'e5a(1t tIoIa8 grap>< in)1('e a margin ?etIeen t>e eQtreme p1ot .a1(e< an' t>e aQe e1ement<<br />

X>e p1a)ement o5 grap> e1emente 1egen': i< 'e5ine' ?8 1o)ation re1ati.e <strong>to</strong> t>e p1ot<br />

area dring poe eme app1ie' d


A teQt ?1o)Y i< p1a)e' in t>e p1ot area ?8 gi.ing it< 8 an' Q )oor'inatee<br />

'e5a(1tc mean< t>at t>e )oor'inate< app18 <strong>to</strong> t>e )enter o5 t>e teQt ?1o)YZ place(se) t>at<br />

t>e8 app18 <strong>to</strong> t>e ?1o)Yf< ea +8m?o1 +iFe<br />

Segen' et)E color()<br />

5i11 fcolor()<br />

o(t1ine lcolor() lpattern() lwidth()<br />

`ar


X>e 'e5a(1t )onne)tUe )o1o(r o5 t>e bar fill i< 'e5ine' ?8 t>e bfcolor() optionW<br />

... , bar(1, bfcolor(gs9)) bar(2, bfcolor(gs14))<br />

DBEKE EQamp1e<<br />

[n t>e 5o11oIing page< 8o( 5in' i11(at generate' t>e grap>e appearan)e i< 'i55erent 5rom t>e man(a1f< grap>at ma'e it: in)1('ing t>e 'ata 5or t>e grap> or a use<br />

)omman'E I e i11(e grap> e xsize() an' ysize() optionioIe.er: 1ea'< <strong>to</strong> <strong>to</strong>o ?oQ an' grap> pieE<br />

his<strong>to</strong>gram<br />

N of children<br />

80<br />

60<br />

40<br />

20<br />

0<br />

1000 1500 2000 2500 3000 3500 4000 4500 5000<br />

Birthweight, grams<br />

A >i


c:\dokumenter\...\gph.birthweight.do<br />

u se "C:\dokumenter\...\newborns.dta" , clear<br />

set<br />

scheme lean2<br />

his<strong>to</strong>gram bweight ///<br />

, ///<br />

frequency ///<br />

normal ///<br />

start(750) width(250) ///<br />

xlabel(1000(500)5000) ///<br />

xmticks(##2) ///<br />

xtitle("Birthweight, grams") ///<br />

ytitle("N of children") ///<br />

plotregion(margin(b=0)) ///<br />

xsize(4) ysize(2.3) scale(1.4)<br />

graph bar<br />

Prevalence (per cent)<br />

10<br />

8<br />

6<br />

4<br />

2<br />

0<br />

16-24 25-44 45-66 67-79 80+<br />

Age<br />

/ / c:\dokumenter\...\gph.diabetes prevalence.do<br />

clear<br />

input str5 age m f<br />

16-24 .9 .2<br />

25-44 .8 .8<br />

45-66 3.8 2.9<br />

67-79 8.2 5.4<br />

80+ 9.1 7.2<br />

end<br />

set<br />

scheme lean2<br />

Males<br />

Females<br />

graph bar m f ///<br />

, ///<br />

over(age) ///<br />

b1title("Age") ///<br />

ytitle("Prevalence (per cent)") ///<br />

legend( label(1 "Males") label(2 "Females") ) ///<br />

xsize(4) ysize(2.3) scale(1.4)<br />

Vor


In ?ar grap>< t>e QUaQi< i< )ategori)a1: t>e 8UaQi< )ontin(o(e eQamp1e .aria?1e< m an'<br />

f 'e5ine' t>e >eig>t< o5 t>e ?are )omman' Iere dIit> one o?e ree t>e au<strong>to</strong>.dta 'ata 8o( )o(1' generate ?ar< 5or t>e n(m?er o5 'omee n(m?er o5 nonUmi Iit> one p1ot >a< no 1egen'Z t>i< one Iit> tIo p1ot< >ae 'e5a(1t 1egen' teQt<<br />

o5ten nee' <strong>to</strong> ?e rep1a)e' ?8 ort: 'ie xtitle() an'<br />

ytitle() option< Iere not e .aria?1e 1a?e1< a< aQi< tit1ee 'e5a(1t 5ir


twoway line<br />

Per cent surviving<br />

100<br />

80<br />

60<br />

40<br />

20<br />

0<br />

Females<br />

Males<br />

1840-49<br />

1901-05<br />

1999-2000<br />

0 20 40 60 80 100<br />

Age<br />

/ / c:\dokumenter\...\gph.DKsurvival.do<br />

u se c:\dokumenter\...\DKsurvival.dta , clear<br />

sort<br />

age // Data must be sorted by the x-axis variable<br />

list in 1/3, clean // List <strong>to</strong> show the data structure<br />

age m1840 k1840 m1901 k1901 m1999 k1999<br />

1. 0 100.00 100.00 100.00 100.00 100.00 100.00<br />

2. 1 84.47 86.76 86.93 89.59 99.16 99.37<br />

3. 2 80.58 83.11 85.22 87.89 99.08 99.32<br />

set scheme lean1<br />

twoway ///<br />

(line m1840-k1999 age , clpattern( - l - l – l )) ///<br />

, ///<br />

plotregion(margin(zero)) ///<br />

xtitle("Age") ///<br />

ytitle("Per cent surviving") ///<br />

legend(label(1 "Males") label(2 "Females") order(2 1) ///<br />

ring(0) pos(8)) ///<br />

text(91 72 "1999-2000") ///<br />

text(77 48 "1901-05") ///<br />

text(49 40 "1840-49") ///<br />

xsize(3.3) ysize(2.3) scale(1.4)<br />

A 1ine p1ot i< a .ariation o5 o(t marYer )onne)ting 1inei< grap><br />

in)1('e< e<br />

clpattern() option 'e5ine< t>e erIie reoI <strong>to</strong> poe 1egen' Iit>in t>e<br />

p1ot area d


twoway connected; twoway rcap<br />

Mean score<br />

100<br />

80<br />

60<br />

40<br />

20<br />

PF RP BP GH VT SF RE MH<br />

SF-36 subscale<br />

/ / c:\dokumenter\...\gph.SF36a.do<br />

clear<br />

input scale n obs sd norm<br />

1 139 60.81 27.35 70.77<br />

2 139 37.65 42.06 62.01<br />

...<br />

8 139 73.06 21.54 79.99<br />

end<br />

generate se=sd/sqrt(n)<br />

generate ci1=obs+1.96*se<br />

generate<br />

ci2=obs-1.96*se<br />

Observed<br />

Expected<br />

95% CI<br />

label define scale 1 "PF" 2 "RP" 3 "BP" 4 "GH" 5 "VT" 6 "SF" 7 "RE" 8 "MH"<br />

label<br />

values scale scale<br />

set scheme lean1<br />

twoway ///<br />

(connected obs scale , msymbol(O) clpattern(l)) ///<br />

(connected norm scale , msymbol(O) mfcolor(white) clpattern(-)) ///<br />

(rcap ci1 ci2 scale) ///<br />

, ///<br />

ytitle("Mean score") ///<br />

xtitle("SF-36 subscale") ///<br />

xlabel(1(1)8 , valuelabel noticks) ///<br />

xscale(range(0.5 8.5)) ///<br />

legend(label(1 "Observed") label(2 "Expected") label(3 "95% CI")) ///<br />

xsize(4) ysize(2.3) scale(1.4)<br />

X>i< grap> in)1('e< t>ree p1ota.e a )ategori)a1 .aria?1e dPV: RP et)Ec at t>e QUaQii< grap><br />

e 'io(t )ap


twoway rspike<br />

Cross-sectional study<br />

/ / c:\dokumenter\...\gph.length_bias.do<br />

clear<br />

set obs 20<br />

gen x=_n<br />

gen y1=x<br />

gen y2=y1+2<br />

replace<br />

y2=y1+8 if mod(x,2)==0<br />

set<br />

scheme lean2<br />

twoway (rspike y1 y2 x , horizontal blwidth(*1.5)) ///<br />

, ///<br />

yscale(off) ylabel(, nogrid) ytitle("") ///<br />

xlabel(none) xtitle("Cross-sectional study") ///<br />

xline(14.5) ///<br />

xsize(3.7) ysize(2.3) scale(1.4)<br />

X>e p(rpoi< grap> i< <strong>to</strong> i11( ?ia


twoway dropline<br />

Patient number<br />

20<br />

15<br />

10<br />

5<br />

1<br />

Deaths<br />

Censorings<br />

0 1 2 3 4 5 6 7<br />

Years after diagnosis<br />

/ / c:\dokumenter\...\gph.obstime.do<br />

use<br />

"c:\dokumenter\...\cohort1.dta" , clear<br />

list patient time died in 1/5 , clean<br />

patient time died<br />

1. 1 0.578 1<br />

2. 2 0.867 1<br />

3. 3 1.235 1<br />

4. 4 1.374 0<br />

5.<br />

5 1.437 1<br />

set<br />

scheme lean2<br />

twoway ///<br />

(dropline time patient if died==1, horizontal msymbol(S)) ///<br />

(dropline time patient if died==0, horizontal ///<br />

msymbol(S) mfcolor(white)) ///<br />

, ///<br />

plotregion(margin(zero)) ///<br />

ytitle("Patient number") ///<br />

yscale(range(0 22)) ///<br />

ylabel(1 5 10 15 20 , nogrid) ///<br />

xtitle("Years after diagnosis") ///<br />

xlabel(0(1)7) ///<br />

legend(label(1 "Deaths") label(2 "Censorings") ring(0)) ///<br />

xsize(3.7) ysize(2.5) scale(1.3)<br />

In a 'rop1ine p1ot a 1ine f'ropeir appearan)e i< )ontro11e' ?8<br />

blpattern(): blcolor() an' blwidth()E<br />

X>e marYer 5or )enite 5i11: not a >o11oI e marYerE<br />

BM


twoway function<br />

-3.00 -1.96 -1.00 0.00 1.00 1.96 3.00<br />

Standard deviations from mean<br />

/ / c:\dokumenter\...\gph.normal.do<br />

set<br />

scheme lean2<br />

twoway ///<br />

(function y=normden(x) , range(-3.5 3.5) ///<br />

droplines(-1.96 -1 0 1 1.96)) ///<br />

, ///<br />

plotregion(margin(zero)) ///<br />

yscale(off) ylabel(, nogrid) ///<br />

xlabel(-3 -1.96 -1 0 1 1.96 3 , format(%4.2f)) ///<br />

xtitle("Standard deviations from mean") ///<br />

xsize(3) ysize(2.3) scale(1.4)<br />

twoway function gi.e< 8o( t>e opport(nit8 <strong>to</strong> .iemati)a1 5(n)tionE X>e<br />

rea< no re1ation <strong>to</strong> t>e a)t(a1 'ata in memor8E X>e range() option i< ne)e


graph matrix<br />

40<br />

30<br />

20<br />

10<br />

250<br />

200<br />

150<br />

Price<br />

5,000 10,000 15,000<br />

10 20 30 40<br />

Mileage<br />

(mpg)<br />

graph matrix<br />

// c:\dokumenter\...\gph.matrix.do<br />

sysuse au<strong>to</strong> , clear<br />

set scheme lean1<br />

Weight<br />

(lbs.)<br />

2,0003,0004,0005,000<br />

graph matrix price mpg weight length ///<br />

, ///<br />

title(graph matrix) ///<br />

mlwidth(*0.7) ///<br />

xsize(5) ysize(4)<br />

150 200 250<br />

Length<br />

(in.)<br />

15,000<br />

10,000<br />

9atriQ e (pper rig>t )e11< are re'(n'ant rotate' image< o5 t>e 1oIer 1e5t )e11em ?8W<br />

graph matrix price mpg weight length , half<br />

JD<br />

5,000<br />

5,000<br />

4,000<br />

3,000<br />

2,000


DBE-E +a.ing: 'ie asis option : it i< 'iat i< not t>e )aer grap>E Remem?er <strong>to</strong> in)1('e t>e 'ata<br />

or a use )omman' rea'ing t>e 'ata (e grap> )omman' Iit> a grap> 'ia1ogE<br />

[pen a ma8 ?e 'i<br />

ReU'ie )(rrent grap> ?8W<br />

graph display [, scale(1.2) ysize(3) xsize(5) scheme(lean2)]<br />

X>e scale() option i< ( area darg(ment< in in)>eat Ii11 ?e tran


+e1e)t Enhanced Metafile (EMF) or Windows Metafile (WMF)Z I>i)> one IorY< ?ee s2color emeE<br />

X>i< note (e eme< lean1 an' lean2Z t>e8 are mo'i5i)ation< <strong>to</strong> s1mono an'<br />

s2monoE 9oe 'i55eren)e ?etIeen t>e tIo i< t>at lean1 >a< a 5rame' p1ot area: ?(t no gri'1inei1e<br />

t>e oppoe )a 8o(r oIn pre5eren)e< (er teQt e'i<strong>to</strong>r<br />

<strong>to</strong> enter t>e option< 8o( Iant in 8o(r oIn per )omman'<br />

termino1og8Z 'o)(mentation i< 5ort>)omingE G<br />

G 0((1 +E Sean maini)e +tata 0o(rna1 @AAGZ GW @MJUGADE<br />

JG


DJE 9ie<br />

memor8 a11o)ate'E X>e memor8 m(ange t>e pat> 5ie1' teQt <strong>to</strong>W<br />

c:\stata\wstata.exe /m15E<br />

I5 t>e memor8 a11o)ate' i< ine mee p>8i< ma8 re'()e t>e memor8 nee' ?8 -AuE<br />

_an'1ing >(ge 'ata an'1ing >(ge 'ata e 'ata: )ompree neQt part o5 t>e 'ata: )ompree partia1 'ata


DJE@E +tring .aria?1e< gUh DJEBZ gUh @N<br />

X>ro(g>o(t t>i< teQt I >a.e 'emon


Numbers <strong>to</strong> strings<br />

\o( Iant t>e n(meri) .aria?1e cprnum )on.erte' <strong>to</strong> a e n(meri) .aria?1e cprnumE _oIe.er: 5or per a<br />

1ea'ing 0 in t>e CPR n(m?er t>e i< )an ?e<br />

reme'ie' ?8W<br />

replace cprstr = subinstr(cprstr," ","0",1)<br />

X>e upper 5(n)tion )on.ert< 1oIer )aa' ?een entere' in)on=10 & ncode4e e infix<br />

)omman' rea' t>e i< Ia8W<br />

replace diab=1 if scode>="E10" & scode


DJEGE Datee n(m?er o5 'a8< : C 5or )ent(r8: Y 5or tIoU'igit 8earcE EQamp1eW<br />

format bdate %dD.N.CY<br />

Rea'ing 'ate .aria?1e< gUh @KE@ED<br />

A 'ate ma8 ?e inp(t a< t>ree .aria?1ee 'ate a< a e date 5(n)tion f(n'eri< e )ent(r8E<br />

Ca1)(1ation< Iit> 'ate< gUh @KE@EB<br />

Xo eQpree 1engt> o5 a time inter.a1 in 8ear< 8o( m(e 'ate in5ormation 5rom t>e


[r 8o( )an eQtra)t Ye8 in5ormation 5rom a CPR n(m?er rea' a< one Z e r(1e< ?e1oIW<br />

generate century = 19<br />

replace century = 20 if pos7 >= 4 & byear = 5 & pos7 = 58<br />

replace byear = 100*century + byear<br />

generate bdate = mdy(bmon,bday,byear)<br />

X>e in5ormation on e 5iret>er t>e e)Y<<br />

t>e .a1i'it8 o5 a CPR n(m?erE Vin' an' 'oIn1oa' it ?8W<br />

findit cprcheck<br />

J-


DJEBE Ran'om


DJEJE Imme'iate )omman'< gUh @@<br />

An fimme'iatef )omman' re^(ire< ta?(1ar or aggregate' inp(tZ 'ata in memor8 are not<br />

a55e)te'E X>e imme'iate )omman'< tabi: cci: csi: iri an' ttesti are mentione' in<br />

e genera1 )omman' ci an' t>e fimme'iate )omman' cii )a1)(1ate )on5i'en)e inter.a1ere oI t>e (


DJENE +amp1e


DJEKE a'oU5i1e< gUh @AU@D: gPh dProgramming man(a1c<br />

An a'oU5i1e i< a programE 9oee on1ine >e1p dwhelp<br />

foreachcE A1in t>e ?ra)e< {} I>i)> m(e opening ^(ote i< ` da))ent gra.ec: an'<br />

t>e en'ing ^(ote t>e


DJE-E EQ)>ange o5 'ata Iit> ot>er program<<br />

`eIareW Xrane)Y )are5(118 egE ?8<br />

)omparing t>e o(tp(t 5rom +P++f DESCRIPTIVES an' +tataf< summarizeE Ee n(m?er o5 .a1i' .a1(e< 5or ea)> .aria?1e an' taYe )are Iit> mi


Vre^(ent18 (


DNE DoU5i1e eQamp1e<<br />

_ere 5o11oI ort eQamp1e< o5 'oU5i1e< 'oing t8pi)a1 t>inger t>an ?8 entering<br />

e e 5ir 'oU5i1e< are part o5 t>e 'o)(mentation an' t>e8 o(1' ?e e8 o(1' >a.e name< te11ing I>at t>e8 'oE 98 prin)ip1e i< t>ii< i< 'i55erent 5rom eQamp1e G I>ere no neI 'ata are<br />

generate': on18 o(tp(tE X>i< 'oU5i1e i< not .ita1 in t>e e 'oU5i1e e'i<strong>to</strong>r 5or )reating<br />

'oU5i1ee 1a


Example 3. Analyse <strong>Stata</strong> data<br />

// winedes.do Descriptive analysis of the wine data 14.5.2001<br />

use c:\dokumenter\wines\wine.dta<br />

describe<br />

codebook<br />

summarize<br />

tab1 type rating<br />

tabulate type rating , chi2 exact<br />

oneway price rating , tabulate<br />

Pote t>at in eQamp1e D an' @ t>e ange< Iere 'o)(mente' Iit> a<br />

'oU5i1e Iit> a gen. pre5iQE<br />

In eQamp1e G t>e


Appendix 1<br />

P(r)>a epi'emio1ogi)a1 atE<br />

I5 8o( Iant <strong>to</strong> Irite 8o(r oIn program< da'oU5i1ee Ue +)an'ina.ian (< Uni.er


Appendix 2<br />

EpiData GEA IIIEepi'ataE'Y<br />

EpiData i< an ea t>ree 5i1ee 'e5inition 5i1e I>ere 8o( 'e5ine .aria?1e name< an' entr8 5ie1'e 'ata 5i1e in EpiIn5o N 5ormatE<br />

first.chk i< t>e )>e)Y5i1e 'e5ining .aria?1e 1a?e1ere 8o( 'e5ine .aria?1e namee .aria?1e name: t>e 5o11oIing teQt ?e)ome< t>e .aria?1e 1a?e1E<br />

! ## in'i)ate< a tIoU'igit n(meri) 5ie1':<br />

! ##.# a 5o(rU'igit n(meri) 5ie1' Iit> one 'e)ima1E<br />

! ___ a t>ree )>ara)ter e o?


Oaria?1e name< )an >a.e (p <strong>to</strong> - )>ara)ter< a-z d?(t not nopc an' 0-9Z t>e8 m(t gi.e it t>e<br />

name sex ] ?(t eEgE a 1a?e1 'e5inition n0y1 dA PoZ D \et 'e5ine a )ommon 1a?e1 5or man8 .aria?1e<<br />

Ue sexlbl 1a?e1 'e5inition 5or sexE [t>er entrie< t>an<br />

D: @: an' not>ing Ii11 ?e reae)te'E<br />

I5 8o( enter D 5or sex: 8o( Ii11 a(mp <strong>to</strong> t>e .aria?1e<br />

bdateZ e men( ?e1oIE<br />

X>e meaning o5 t>e 9en( 'ia1og ?oQ i< not o?.io(< at 5ire<br />

option<


Appendix 3<br />

PoteXa? Sig>t IIIEnoteta?E)om<br />

`ot> t>e Ree OieIer Iin'oI >a.e 1imitation< in t>e a?i1it8 <strong>to</strong> >an'1e<br />

o(tp(t: an' 8o( Ii11 ?ene5it 5rom a goo' teQt e'i<strong>to</strong>rE I (t: a.ai1a?1e 5or 5ree<br />

5rom IIIEnoteta?E)omE I 5in' PoteXa? e (e 5o11oIing I ai1e r(nning +tata 8o(<br />

mig>t not e 1atea.e a))ee (p'ate' o(tp(tE [r rig>tU)1i)Y t>e 5i1ef< ta? an'


In'eQ<br />

A<br />

a'oU5i1e< EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE N@<br />

Aggregating 'ataEEEEEEEEEEEEEEEEEE @N<br />

anova EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE GA<br />

append EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE @J<br />

Arit>meti) opera<strong>to</strong>r< EEEEEEEEEEEE @A<br />

A+CII 'ata EEEEEEEEEEEEEEEEEEEEEEEEEEE DK<br />

AQi< 1a?e1< dgrap> option< EEEEEEEEEEEEEEEEEEEEEEEEEEEE BB<br />

`art1ettf< te


N<br />

newlog.ado d(

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!