Title: Stata%208,%20Programing
1Stata 8, Programing
- Hein Stigum
- Presentation, data and programs at
- http//folk.uio.no/heins/
2Programing
- 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
3Stata Macro Language
4Scalar
- Scalar (numbers)
- scalar a2 define
- display a display content
- gen x2ax use
- Will not work in plots
- Only numbers
5Macro
- Local Macro (number or string)
- local a2 define
- display a display content
- gen x2ax use
- Global Macro (number or string)
- global a2 define
- display a display content
- gen xax2 use
- Global has a longer life than local
6Matrix
- Matrix (matrix of numbers)
- matrix AJ(2,3,0) define 23 matrix of 0s
- matrix list A display content
- matrix A2,112 change element
- matrix rownames Amean1 mean2 set names
- matrix colnames Alow med high set names
Only numbers No vectors
7Use returned results
Run command
Look for returned results
Use returned results
Or put in macro
global mr(mean)
8Example Density plot with extra info
9Commands for previous plot
Run command
summarize weight, detail
Put in macro
global Nr(N) Number of observations global
p10r(p10) 10-th percentile global
p50r(p50) global p90r(p90)
Use macros
twoway (kdensity weight) , note(NN)
xlabel(minmax p10 p50 p90, format(5.0f))
10Get regression results
Run regression
logistic lbw sex age
Look for returned results
ereturn list
Put in matrices
matrix be(b) vector of coefficients, constant
last matrix Ve(V) variance-covariance
matrix Alternative global b1
_bsex sex-coefficient global
se1_sesex sex-standard error
Use macros
matrix varvecdiag(e(V)) variance global
ci1exp(b1,1-1.96sqrt(var1,1)) lower CI
for sex
11Loops
- Over variables
- Over index (for-loop)
foreach var of varlist sex mage gest
summarize var'
forvalues i1(1) 5 display i'
12Loops example Crude ORs
- Version 1
- Version 2, collect results in a matrix
foreach var of varlist sex mage gest logistic
lbw var'
matrix AJ(3,3,0) local i1 foreach var of
varlist sex mage gest logistic lbw
var' matrix Ai',1exp(_bvar') local i
13Mata
14Mata commands
- Start and stop
- mata start Mata
- end stop Mata
- Help
- help mata
- help m4 intro list of functions
- help mata max() given function
15Mata commands cont.
- Vector
- x(1,2,3) line vector
- y(4\5\6) column vector
- x y y1 display x and y and y1
- zxy' xy-transposed
- x1 element by element
- Differences from macro language
16Mata commands cont.
- Matrix
- AJ(2,3,0) 2 by 3 matrix of 0s
- A1,214 change element
- A1,. 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
-
17Mata read and set
- Read or set
- Ast_matrix("r(V)") 1. read from Stata
- st_matrix("X",A) 2. set or reset in Stata
- sst_matrixrowstripe("r(V)") row names
- mst_global(m") global macro
- st_addvar("double",(y", x)) add variables
- st_store((1,n),(y", x),(y1,x1)) store values
where
Stata name
Mata name
18Mata commands cont.
- Run Stata commands
- stata(cmd) run Stata commands
- stata(mean xstrofreal(i)) mean x1
- stata(mean xstrofreal(i),detail) mean x1,
detail - Remember spaces!
19Example plot regression results
- Idea
- coeff and se as vectors,
- calculate CI
- store vectors as data
- Plot
- scatter and rcap
- Plot elements
- _y 1-4
- _point OR or coeff
- _low CI
- _high CI
20Example plot valid and missing
- Idea
- tabstat, stat(N)
- missing relative to 1.
- store vectors as data
- Plot
- bar, rbar,
- scatter with mlabel
- Plot elements
- _yy 1-6
- _val valid
- _mis missing
- _tot total
21Example Bi-and multivariable table