Stata 8, Programing Hein Stigum Presentation, data and programs at:

advertisement
Stata 8, Programing
Hein Stigum
Presentation, data and programs at:
http://folk.uio.no/heins/
May-16
H.S.
1
Programing
• Programing examples
–
–
–
–
Get and use results from commands
Automate tasks
Define functions
Define new commands
• Two languages
– Stata Macro language
– Mata
May-16
H.S.
For small problems
For large problem
2
Stata Macro Language
May-16
H.S.
3
Scalar
• Scalar (numbers)
scalar a=2
display a
gen x2=a*x
define
display content
use
• Will not work in plots
• Only numbers
May-16
H.S.
4
Macro
• Local Macro (number or string)
local a=2
display `a’
gen x2=`a’*x
define
display content
use
• Global Macro (number or string)
global a=2
display $a
gen x$a=x^2
define
display content
use
• Global has a “longer life” than local
May-16
H.S.
5
Matrix
• Matrix (matrix of numbers)
matrix A=J(2,3,0)
define 2*3 matrix of 0’s
matrix list A
display content
matrix A[2,1]=12
change element
matrix rownames A=mean1 mean2
set names
matrix colnames A=low med high
set names
Only numbers
No vectors
May-16
H.S.
6
Use returned results
Run command:
Look for returned results:
Use returned results:
May-16
Or put in macro:
global m=r(mean)
H.S.
7
Example: Density plot with extra info
min
200
200
w
2040
25%
3180
2820
Weight
Birthweight
50% 75%
w max
3940
3600
3600
4280
5350
5080
5350
N=583
N=583
May-16
H.S.
8
Commands for previous plot
Run command:
summarize weight, detail
Put in macro:
global N=r(N)
global p10=r(p10)
global p50=r(p50)
global p90=r(p90)
Number of observations
10-th percentile
Use macros:
twoway (kdensity weight) ,
note(N=$N) xlabel(minmax $p10 $p50 $p90, format(%5.0f))
May-16
H.S.
9
Get regression results
Run regression:
logistic lbw sex age
Look for returned results:
ereturn list
Put in matrices:
matrix b=e(b)
vector of coefficients, constant last
matrix V=e(V)
variance-covariance matrix
Alternative:
global b1= _b[sex]
sex-coefficient
global se1=_se[sex]
sex-standard error
Use macros:
matrix var=vecdiag(e(V))
variance
global ci1=exp(b[1,1]-1.96*sqrt(var[1,1])) lower CI for sex
May-16
H.S.
10
Loops
• Over variables
foreach var of varlist sex mage gest {
summarize `var'
}
• Over index (for-loop)
forvalues i=1(1) 5 {
display `i'
}
May-16
H.S.
11
Loops example: Crude ORs
• Version 1
foreach var of varlist sex mage gest {
logistic lbw `var'
}
• Version 2, collect results in a matrix
matrix A=J(3,3,0)
local i=1
foreach var of varlist sex mage gest {
logistic lbw `var'
matrix A[`i',1]=exp(_b[`var'])
local ++i
}
May-16
H.S.
12
Mata
May-16
H.S.
13
Mata commands
• Start and stop
– mata
– end
start Mata
stop Mata
• Help
– help mata
– help m4 intro
– help mata max()
May-16
list of functions
given function
H.S.
14
Mata commands cont.
• Vector
x=(1,2,3)
y=(4\5\6)
x; y; y[1]
z=x+y'
x:+1
line vector
column vector
display x and y and y1
x+y-transposed
element by element
Differences from macro language:
May-16
H.S.
15
Mata commands cont.
• Matrix
A=J(2,3,0)
A[1,2]=14
A[1,.]
A[.,1]
A[(1\3),(1,2)]
2 by 3 matrix of 0’s
change element
1. line
1. column
submatrix: line 1 and 3,
col 1 and 2
Strong syntax, direct access to all submatrices
May-16
H.S.
16
Mata read and set
• Read or set
A=st_matrix("r(V)")
st_matrix("X",A)
1. read from Stata
2. set or reset in Stata
s=st_matrixrowstripe("r(V)")
m=st_global(”m")
row names
global macro
st_addvar("double",(“y", “x”))
st_store((1,n),(“y", “x”),(y1,x1))
add variables
store values
where
May-16
Stata
name
Mata
name
H.S.
17
Mata commands cont.
• Run Stata commands
stata(”cmd”)
run Stata commands
stata(”mean x”+strofreal(i))
mean x1
stata(”mean x”+strofreal(i)+”,detail”) mean x1, detail
Remember spaces!
May-16
H.S.
18
Example: plot regression results
Logistic regression on lbw
• Idea
coeff and se as vectors,
calculate CI
store vectors as data
sex
mage
• Plot
scatter and rcap
gest10
• Plot elements
parity
-4 -3 -2 -1 0 1 2 3 4
Estimates with 95% confidence interval
N=536
May-16
_y
_point
_low
_high
1-4
OR or coeff
CI
CI
constant=20.77281
H.S.
19
Example: plot valid and missing
• Idea
id
583
0
sex
549
34
mage
583
0
weight
525
gest
449
134
birthy
583
0
0
tabstat…, stat(N)
missing relative to 1.
store vectors as data
• Plot
58
100
200
300
400
500
bar, rbar,
scatter with mlabel
• Plot elements
600
Valid observations
Missing observations
May-16
H.S.
_yy
_val
_mis
_tot
1-6
valid
missing
total
20
Example: Bi-and multivariable table
May-16
H.S.
21
Download