Title: Stata Introduction , Short
1Stata Introduction , Short
- Hein Stigum
- Presentation, data and programs at
- http//folk.uio.no/heins/
Dec-09
H.S.
1
2Why Stata
- Pro
- Aimed at epidemiology
- Many methods, growing
- Graphics
- Structured, Programmable
- Coming soon to a course near you
- Con
- Memorygtfile size
- Copy tables
Dec-09
H.S.
2
3Interface
4Interface
Dec-09
H.S.
4
5Menu
Dec-09
H.S.
5
6Do Editor
- New
- Ctrl-8, doedit or
- Run
- Mark commands, Ctrl-D to do (execute)
Dec-09
H.S.
6
7Do-file example
Dec-09
H.S.
7
8Syntax
- Syntax
- bysort varlist command varlist if exp in
range, opts - Examples
- mean age
- mean age if sex1
- bysort sex summarize age
- summarize age ,detail
Dec-09
H.S.
8
9Data handling
10Import data
- Using SPSS 14.0
- Save as, Stata Version 8 SE
Dec-09
H.S.
10
11Use and save data
- Open data
- set memory 200m
- use C\Course\Myfile.dta, clear
- Describe
- describe describe all variables
- list x1 x2 in 1/20 list obs nr 1 to 20
- Save data
- save C\Course\Myfile.dta ,replace
Dec-09
H.S.
11
12Use data from web
- webuse file use data from Stata homepage
- webuse set http//www.med.uio.no/imb/stat/kursfil
er/ - set homepage
- webuse birth1 data for exercise 1
Dec-09
H.S.
12
13Generate, replace
- Index
- generate index0
- replace index1 if sex1 agegt30
- Young/Old
- generate old(agegt50)
- Serial numbers, lags
- gen id_n
- gen lagage _n-1
if agelt.
Dec-09
H.S.
13
14Dates
- From numeric to date
- ex m12, d2, y1987
- generate bdatemdy(m,d,y)
- format bdate td
- From string to date
- ex bstr01.12.1987
- generate bdatedate(bstr,DMY)
- format bdate td
Dec-09
H.S.
14
15Missing
- Obs!!!
- Missing values are large numbers
- agegt30 will include missing.
- agegt30 if agelt. will not.
- Test
- replace x0 if (x.)
- Remove
- drop if age.
- Change
- replace educ. if educ99
Dec-09
H.S.
15
16Describe missing
- Summarize variables
- Missing in tables
Dec-09
H.S.
16
17Help
- General
- help command
- findit keyword search Statanet
- Examples
- help table
- findit aflogit
Dec-09
H.S.
17
18Summing up
- Use do files
- Mark, Ctrl-D to do (execute)
- Syntax
- command varlist if exp in range , options
- Missing
- agegt30 if agelt.
- generate old(agegt50) if agelt.
- Help
- help describe
Dec-09
H.S.
18
19Descriptive
20Descriptive
summarize weight, det details
tabulate bullied, nolab show coding
21Other descriptives
tabstat mAge, stat( N min p50 mean max)
by(parity)
Dec-09
H.S.
21
22Graphics
23Twoway plots
- Syntax
- twoway (plot1, opts) (plot2, opts), opts
- One plot
- kdensity x Distribution plot
- scatter y x Scatter plot
- Overlaid plots
- twoway (kdensity y if x1) (kdensity y if x2)
- twoway (scatter y x) (fpfitci y x) (lfit y x)
Dec-09
H.S.
23
24twoway ( kdensity weight if sex1, lcolor(blue)
) /// ( kdensity weight if sex2, lcolor(red
) )
Dec-09
H.S.
24
25twoway (scatter y x) (fpfitci y x) (lfit y x)
Dec-09
H.S.
25
26Titles
scatter weight gest, title("title")
subtitle("subtitle") /// xtitle("xtitle")
ytitle("ytitle") note("note")
Dec-09
H.S.
26
27Bivariate analysis
282 independent samples
Do boys and girls have the same mean birth weight?
twoway ( kdensity weight if sex1, lcolor(blue)
) /// ( kdensity weight if sex2,
lcolor(red) )
Equal means? Equal variance?
Dec-09
H.S.
28
292 independent samples test
ttest weight, by(sex) unequal ttest w1 w2, paired
Dec-09
H.S.
29
30Crosstables
Are boys bullied as much as girls?
Dec-09
H.S.
30