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